Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
402 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace Spatie\ValidationRules\Exceptions; | ||
|
||
use Exception; | ||
|
||
class InvalidDate extends Exception | ||
{ | ||
public static function withFormat(string $format, string $value): InvalidDate | ||
{ | ||
return new self("Invalid date {$value} for format {$format}"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace Spatie\ValidationRules; | ||
|
||
use Carbon\Carbon; | ||
use InvalidArgumentException; | ||
use Spatie\ValidationRules\Exceptions\InvalidDate; | ||
|
||
trait IsDateRule | ||
{ | ||
/** @var string */ | ||
protected $format; | ||
|
||
protected function createDate(string $value): Carbon | ||
{ | ||
try { | ||
return Carbon::createFromFormat($this->format, $value); | ||
} catch (InvalidArgumentException $exception) { | ||
throw InvalidDate::withFormat($this->format, $value); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
namespace Spatie\ValidationRules\Rules; | ||
|
||
use Carbon\Carbon; | ||
use Spatie\ValidationRules\IsDateRule; | ||
use Illuminate\Contracts\Validation\Rule; | ||
|
||
class DateBetween implements Rule | ||
{ | ||
use IsDateRule; | ||
|
||
/** @var \Carbon\Carbon */ | ||
protected $start; | ||
|
||
/** @var \Carbon\Carbon */ | ||
protected $end; | ||
|
||
/** @var bool */ | ||
protected $orEquals = true; | ||
|
||
public function __construct(Carbon $start, Carbon $end, string $format = 'Y-m-d') | ||
{ | ||
$this->start = $start->copy(); | ||
|
||
$this->end = $end->copy(); | ||
|
||
$this->format = $format; | ||
} | ||
|
||
public function includeBoundaries(): DateBetween | ||
{ | ||
$this->orEquals = true; | ||
|
||
return $this; | ||
} | ||
|
||
public function excludeBoundaries(): DateBetween | ||
{ | ||
$this->orEquals = false; | ||
|
||
return $this; | ||
} | ||
|
||
public function withoutTime(): DateBetween | ||
{ | ||
$this->start->startOfDay(); | ||
|
||
$this->end->endOfDay(); | ||
|
||
return $this; | ||
} | ||
|
||
public function passes($attribute, $value): bool | ||
{ | ||
$date = $this->createDate($value); | ||
|
||
return $date->between($this->start, $this->end, $this->orEquals); | ||
} | ||
|
||
public function message(): string | ||
{ | ||
return __('validationRules.date_between'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace Spatie\ValidationRules\Rules; | ||
|
||
use Spatie\ValidationRules\IsDateRule; | ||
use Illuminate\Contracts\Validation\Rule; | ||
|
||
class FutureDate implements Rule | ||
{ | ||
use IsDateRule; | ||
|
||
public function __construct(string $format = 'Y-m-d') | ||
{ | ||
$this->format = $format; | ||
} | ||
|
||
public function passes($attribute, $value): bool | ||
{ | ||
$date = $this->createDate($value); | ||
|
||
return $date->isFuture(); | ||
} | ||
|
||
public function message(): string | ||
{ | ||
return __('validationRules.future_date'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace Spatie\ValidationRules\Rules; | ||
|
||
use Spatie\ValidationRules\IsDateRule; | ||
use Illuminate\Contracts\Validation\Rule; | ||
|
||
class PastDate implements Rule | ||
{ | ||
use IsDateRule; | ||
|
||
public function __construct(string $format = 'Y-m-d') | ||
{ | ||
$this->format = $format; | ||
} | ||
|
||
public function passes($attribute, $value): bool | ||
{ | ||
$date = $this->createDate($value); | ||
|
||
return $date->isPast(); | ||
} | ||
|
||
public function message(): string | ||
{ | ||
return __('validationRules.past_date'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
|
||
namespace Spatie\ValidationRules\Tests\Rules; | ||
|
||
use Carbon\Carbon; | ||
use Spatie\ValidationRules\Tests\TestCase; | ||
use Spatie\ValidationRules\Rules\DateBetween; | ||
use Spatie\ValidationRules\Exceptions\InvalidDate; | ||
|
||
class DateBetweenTest extends TestCase | ||
{ | ||
public function setUp() | ||
{ | ||
parent::setUp(); | ||
|
||
Carbon::setTestNow(Carbon::make('2018-01-01 00:00:00')); | ||
} | ||
|
||
/** @test */ | ||
public function it_will_return_true_when_between_or_equals() | ||
{ | ||
$rule = new DateBetween(now(), now()->addDays(2)); | ||
|
||
$this->assertTrue($rule->passes('attribute', '2018-01-01')); | ||
$this->assertTrue($rule->passes('attribute', '2018-01-02')); | ||
$this->assertTrue($rule->passes('attribute', '2018-01-03')); | ||
} | ||
|
||
/** @test */ | ||
public function it_will_return_false_when_not_between() | ||
{ | ||
$rule = new DateBetween(now(), now()->addDays(2)); | ||
|
||
$this->assertFalse($rule->passes('attribute', '2017-01-01')); | ||
$this->assertFalse($rule->passes('attribute', '2019-01-01')); | ||
} | ||
|
||
/** @test */ | ||
public function it_will_return_false_when_on_boundary_and_boundaries_are_excluded() | ||
{ | ||
$rule = (new DateBetween(now(), now()->addDays(2))) | ||
->excludeBoundaries(); | ||
|
||
$this->assertFalse($rule->passes('attribute', '2018-01-01')); | ||
$this->assertTrue($rule->passes('attribute', '2018-01-02')); | ||
$this->assertFalse($rule->passes('attribute', '2018-01-03')); | ||
} | ||
|
||
/** @test */ | ||
public function it_will_return_true_when_comparing_without_time_and_within_day_boundary() | ||
{ | ||
$rule = (new DateBetween( | ||
Carbon::make('2018-01-01 12:00:00'), | ||
Carbon::make('2018-01-03 12:00:00'), | ||
'Y-m-d H:i:s' | ||
)) | ||
->withoutTime(); | ||
|
||
$this->assertTrue($rule->passes('attribute', Carbon::make('2018-01-01 10:00:00'))); | ||
$this->assertTrue($rule->passes('attribute', Carbon::make('2018-01-03 14:00:00'))); | ||
} | ||
|
||
/** @test */ | ||
public function it_will_return_false_when_comparing_with_time_and_within_day_boundary() | ||
{ | ||
$rule = (new DateBetween( | ||
Carbon::make('2018-01-01 12:00:00'), | ||
Carbon::make('2018-01-03 12:00:00'), | ||
'Y-m-d H:i:s' | ||
)); | ||
|
||
$this->assertFalse($rule->passes('attribute', Carbon::make('2018-01-01 10:00:00'))); | ||
$this->assertFalse($rule->passes('attribute', Carbon::make('2018-01-03 14:00:00'))); | ||
} | ||
|
||
/** @test */ | ||
public function it_throws_an_invalid_date_exception_on_invalid_format() | ||
{ | ||
$rule = (new DateBetween( | ||
Carbon::make('2018-01-01 12:00:00'), | ||
Carbon::make('2018-01-03 12:00:00'), | ||
'Y-m-d' | ||
)); | ||
|
||
$this->expectException(InvalidDate::class); | ||
|
||
$rule->passes('attribute', Carbon::make('2018-01-01')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace Spatie\ValidationRules\Tests\Rules; | ||
|
||
use Carbon\Carbon; | ||
use Spatie\ValidationRules\Tests\TestCase; | ||
use Spatie\ValidationRules\Rules\FutureDate; | ||
|
||
class FutureDateTest extends TestCase | ||
{ | ||
public function setUp() | ||
{ | ||
parent::setUp(); | ||
|
||
Carbon::setTestNow(Carbon::make('2018-01-01 00:00:00')); | ||
} | ||
|
||
/** @test */ | ||
public function it_will_return_true_when_is_future_date() | ||
{ | ||
$rule = new FutureDate(); | ||
|
||
$this->assertTrue($rule->passes('attribute', '2018-01-02')); | ||
} | ||
|
||
/** @test */ | ||
public function it_will_return_false_when_not_future_date() | ||
{ | ||
$rule = new FutureDate(); | ||
|
||
$this->assertFalse($rule->passes('attribute', '2017-01-01')); | ||
} | ||
} |
Oops, something went wrong.