-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from vdhicts/feature/minute-step
Add DateHasSpecificMinutes validation rule
- Loading branch information
Showing
6 changed files
with
147 additions
and
46 deletions.
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 |
---|---|---|
@@ -1,27 +1,28 @@ | ||
<?php | ||
|
||
return [ | ||
'bic_number' => 'The :attribute must be a valid BIC number', | ||
'contains' => 'The :attribute must contain `%s`', | ||
'date_after_or_equal' => 'The :attribute must be after or equal to `%s`', | ||
'date_before_or_equal' => 'The :attribute must be before or equal to `%s`', | ||
'dutch_phone' => 'The :attribute must be a valid dutch phone number', | ||
'dutch_postal_code' => 'The :attribute must be a valid dutch post code', | ||
'hex_color' => 'The :attribute must be a valid Hex color', | ||
'hostname' => 'The :attribute must be a valid hostname', | ||
'iban' => 'The :attribute must be a valid IBAN', | ||
'interval' => 'The :attribute must be an interval', | ||
'bic_number' => 'The :attribute must be a valid BIC number', | ||
'contains' => 'The :attribute must contain `%s`', | ||
'date_after_or_equal' => 'The :attribute must be after or equal to `%s`', | ||
'date_before_or_equal' => 'The :attribute must be before or equal to `%s`', | ||
'date_has_specific_minutes' => 'The :attribute must have one of :minutes minutes', | ||
'dutch_phone' => 'The :attribute must be a valid dutch phone number', | ||
'dutch_postal_code' => 'The :attribute must be a valid dutch post code', | ||
'hex_color' => 'The :attribute must be a valid Hex color', | ||
'hostname' => 'The :attribute must be a valid hostname', | ||
'iban' => 'The :attribute must be a valid IBAN', | ||
'interval' => 'The :attribute must be an interval', | ||
'maximum_hour_difference' => 'The start and end date differ more then %s hours', | ||
'mime_type' => 'The :attribute must be a valid MIME', | ||
'not_contains' => 'The :attribute must not contain `%s`', | ||
'not_ends_with' => 'The :attribute must not end with `%s`', | ||
'not_starts_with' => 'The :attribute must not start with `%s`', | ||
'password_strength' => 'The :attribute must contains at least one capital letter, one lowercase letter and one number', | ||
'phone' => 'The :attribute must be a valid phone number', | ||
'positive_interval' => 'The :attribute must be a positive interval', | ||
'price_custom_decimal' => 'The :attribute must be a valid price like `10%s95`', | ||
'price' => 'The :attribute must be a valid price like `10,95` or `10.50`', | ||
'semver' => 'The :attribute must be a valid version according to the semver standard', | ||
'secure_url' => 'The :attribute must be a HTTPS url', | ||
'vat_number' => 'The :attribute must be a valid VAT number', | ||
'mime_type' => 'The :attribute must be a valid MIME', | ||
'not_contains' => 'The :attribute must not contain `%s`', | ||
'not_ends_with' => 'The :attribute must not end with `%s`', | ||
'not_starts_with' => 'The :attribute must not start with `%s`', | ||
'password_strength' => 'The :attribute must contains at least one capital letter, one lowercase letter and one number', | ||
'phone' => 'The :attribute must be a valid phone number', | ||
'positive_interval' => 'The :attribute must be a positive interval', | ||
'price_custom_decimal' => 'The :attribute must be a valid price like `10%s95`', | ||
'price' => 'The :attribute must be a valid price like `10,95` or `10.50`', | ||
'semver' => 'The :attribute must be a valid version according to the semver standard', | ||
'secure_url' => 'The :attribute must be a HTTPS url', | ||
'vat_number' => 'The :attribute must be a valid VAT number', | ||
]; |
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 |
---|---|---|
@@ -1,27 +1,28 @@ | ||
<?php | ||
|
||
return [ | ||
'bic_number' => 'Het veld :attribute moet een geldig BIC nummer bevatten', | ||
'contains' => 'Het veld :attribute moet `%s` bevatten', | ||
'date_after_or_equal' => 'Het veld :attribute moet na of op `%s` zijn', | ||
'date_before_or_equal' => 'Het veld :attribute moet voor of op `%s` zijn', | ||
'dutch_phone' => 'Het veld :attribute moet een geldig telefoonnummer bevatten', | ||
'dutch_postal_code' => 'Het veld :attribute moet een geldige postcode bevatten', | ||
'hex_color' => 'Het veld :attribute moet een geldige Hex kleur bevatten', | ||
'hostname' => 'Het veld :attribute moet een geldige hostname bevatten', | ||
'iban' => 'Het veld :attribute moet een geldige IBAN bevatten', | ||
'interval' => 'Het veld :attribute moet een interval zijn', | ||
'bic_number' => 'Het veld :attribute moet een geldig BIC nummer bevatten', | ||
'contains' => 'Het veld :attribute moet `%s` bevatten', | ||
'date_after_or_equal' => 'Het veld :attribute moet na of op `%s` zijn', | ||
'date_before_or_equal' => 'Het veld :attribute moet voor of op `%s` zijn', | ||
'date_has_specific_minutes' => 'Het veld :attribute moet ingesteld zijn op een van :minutes minuten', | ||
'dutch_phone' => 'Het veld :attribute moet een geldig telefoonnummer bevatten', | ||
'dutch_postal_code' => 'Het veld :attribute moet een geldige postcode bevatten', | ||
'hex_color' => 'Het veld :attribute moet een geldige Hex kleur bevatten', | ||
'hostname' => 'Het veld :attribute moet een geldige hostname bevatten', | ||
'iban' => 'Het veld :attribute moet een geldige IBAN bevatten', | ||
'interval' => 'Het veld :attribute moet een interval zijn', | ||
'maximum_hour_difference' => 'De start en eind datum moeten meer dan %s uur verschil', | ||
'mime_type' => 'Het veld :attribute moet een geldig MIME-type zijn', | ||
'not_contains' => 'Het veld :attribute mag geen `%s` bevatten', | ||
'not_ends_with' => 'Het veld :attribute mag niet eindigen met `%s`', | ||
'not_starts_with' => 'Het veld :attribute mag niet beginnen `%s`', | ||
'password' => 'Het veld :attribute moet minstens een hoofdletter, een kleine letter en een nummer bevatten', | ||
'phone' => 'Het veld :attribute moet een geldig telefoonnummer bevatten', | ||
'positive_interval' => 'Het veld :attribute moet een positieve interval bevatten', | ||
'price_custom_decimal' => 'Het veld :attribute moet een geldig bedrag bevatten, zoals `10%s95`', | ||
'price' => 'Het veld :attribute moet een geldig bedrag bevatten, zoals `10,95` of `10.50`', | ||
'secure_url' => 'Het veld :attribute moet een HTTPS url zijn', | ||
'semver' => 'Het veld :attribute moet een geldige versie zijn volgens de semver standaard', | ||
'vat_number' => 'Het veld :attribute moet een geldig BTW nummer bevatten', | ||
'mime_type' => 'Het veld :attribute moet een geldig MIME-type zijn', | ||
'not_contains' => 'Het veld :attribute mag geen `%s` bevatten', | ||
'not_ends_with' => 'Het veld :attribute mag niet eindigen met `%s`', | ||
'not_starts_with' => 'Het veld :attribute mag niet beginnen `%s`', | ||
'password' => 'Het veld :attribute moet minstens een hoofdletter, een kleine letter en een nummer bevatten', | ||
'phone' => 'Het veld :attribute moet een geldig telefoonnummer bevatten', | ||
'positive_interval' => 'Het veld :attribute moet een positieve interval bevatten', | ||
'price_custom_decimal' => 'Het veld :attribute moet een geldig bedrag bevatten, zoals `10%s95`', | ||
'price' => 'Het veld :attribute moet een geldig bedrag bevatten, zoals `10,95` of `10.50`', | ||
'secure_url' => 'Het veld :attribute moet een HTTPS url zijn', | ||
'semver' => 'Het veld :attribute moet een geldige versie zijn volgens de semver standaard', | ||
'vat_number' => 'Het veld :attribute moet een geldig BTW nummer bevatten', | ||
]; |
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,49 @@ | ||
<?php | ||
|
||
namespace Vdhicts\ValidationRules\Rules; | ||
|
||
use Carbon\Exceptions\InvalidFormatException; | ||
use Illuminate\Contracts\Validation\Rule; | ||
use Illuminate\Support\Carbon; | ||
|
||
class DateHasSpecificMinutes implements Rule | ||
{ | ||
private array $allowedMinutes; | ||
private string $format; | ||
|
||
public function __construct(array $allowedMinutes, string $format = 'Y-m-d H:i') | ||
{ | ||
$this->allowedMinutes = $allowedMinutes; | ||
$this->format = $format; | ||
} | ||
|
||
/** | ||
* Determine if the validation rule passes. | ||
* | ||
* @param string $attribute | ||
* @param mixed $value | ||
* @return bool | ||
*/ | ||
public function passes($attribute, $value): bool | ||
{ | ||
try { | ||
$date = Carbon::createFromFormat($this->format, $value); | ||
} catch (InvalidFormatException $exception) { | ||
return false; | ||
} | ||
|
||
return in_array($date->minute, $this->allowedMinutes); | ||
} | ||
|
||
/** | ||
* Get the validation error message. | ||
* | ||
* @return string | ||
*/ | ||
public function message(): string | ||
{ | ||
return trans('validationRules.date_has_specific_minutes', [ | ||
'minutes' => implode(', ', $this->allowedMinutes) | ||
]); | ||
} | ||
} |
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,36 @@ | ||
<?php | ||
|
||
namespace Vdhicts\ValidationRules\Tests\Rules; | ||
|
||
use Carbon\Carbon; | ||
use Vdhicts\ValidationRules\Rules\DateHasSpecificMinutes; | ||
use Vdhicts\ValidationRules\Tests\TestCase; | ||
|
||
class DateHasSpecificMinutesTest extends TestCase | ||
{ | ||
public function testRulePasses() | ||
{ | ||
$rule = new DateHasSpecificMinutes([0, 10, 20, 30, 40, 55]); | ||
$this->assertTrue($rule->passes('', '2021-09-30 00:00')); | ||
$this->assertTrue($rule->passes('', '2021-09-30 01:10')); | ||
$this->assertTrue($rule->passes('', '2021-09-30 01:20')); | ||
$this->assertTrue($rule->passes('', '2021-09-30 01:30')); | ||
$this->assertTrue($rule->passes('', '2021-09-30 01:40')); | ||
$this->assertTrue($rule->passes('', '2021-09-30 01:55')); | ||
|
||
$rule = new DateHasSpecificMinutes([0, 15, 30, 45], 'd-m-Y H:i'); | ||
$this->assertTrue($rule->passes('', '30-09-2021 09:30')); | ||
} | ||
|
||
public function testRuleFails() | ||
{ | ||
$rule = new DateHasSpecificMinutes([0, 10, 20, 30, 40, 55]); | ||
$this->assertFalse($rule->passes('', 'lol')); | ||
$this->assertFalse($rule->passes('', '2021-09-30 01:01')); | ||
$this->assertFalse($rule->passes('', '2021-09-30 01:02')); | ||
|
||
$rule = new DateHasSpecificMinutes([0, 15, 30, 45], 'd-m-Y H:i'); | ||
$this->assertFalse($rule->passes('', 'lol')); | ||
$this->assertFalse($rule->passes('', '30-09-2021 09:17')); | ||
} | ||
} |