Skip to content

Commit

Permalink
auto-fill 'valid_start' date for first element
Browse files Browse the repository at this point in the history
'saving' event is hit before 'creating' event
  • Loading branch information
julesjanssen committed Jan 24, 2017
1 parent 04eae35 commit ebcfe8c
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/Temporal.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ public static function bootTemporal()
*/
public static function registerEvents()
{
static::creating(function ($item) {
$item->startCannotBeInThePast();
$item->endCurrent();
});

static::saving(function ($item) {
if (! $item->exists) {
$item->startCannotBeInThePast();
$item->endCurrent();
}

$item->startCannotBeAfterEnd();
$item->removeSchedulingConflicts();
});
Expand Down Expand Up @@ -72,6 +72,10 @@ public function isValid(Carbon $validTime = null)
*/
protected function startCannotBeInThePast()
{
if (is_null($this->valid_start)) {
$this->valid_start = Carbon::now();
}

if ($this->valid_start < Carbon::now()->subSeconds(5)) {
throw new InvalidDateRangeException;
}
Expand Down

0 comments on commit ebcfe8c

Please sign in to comment.