Skip to content

Commit

Permalink
Merge pull request #17 from vdhicts/feature/minute-step
Browse files Browse the repository at this point in the history
Add DateHasSpecificMinutes validation rule
  • Loading branch information
dvdheiden authored Sep 30, 2021
2 parents 650eba9 + 65f586f commit 61c8da7
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 46 deletions.
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ This package offers extra validation rules for Laravel.

## Requirements

This package requires Laravel 5.7+ and PHP 7.4+.
This package requires Laravel 8+ and PHP 7.4+.

| Version | Compatible with |
| --- | --- |
Expand Down Expand Up @@ -57,6 +57,20 @@ Validates if the value is a date before or equals the provided date (Carbon).
'field' => [new DateBeforeOrEqual($date)],
```

### DateHasSpecificMinutes

Validates if the selected minutes for the provided date are according to the available minutes.

```php
'field' => [new DateHasSpecificMinutes([0, 15, 30, 45])],
```

When the date is not according to the 'Y-m-d H:i' format then you are able to specify the format as second parameter:

```php
'field' => [new DateHasSpecificMinutes([0, 15, 30, 45], 'd-m-Y H:i')],
```

### DutchPhone

Validates if the value is a valid dutch phone number. Both mobile or landlines are supported. See the `Phone` validation
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
}
],
"require": {
"php": "^7.4",
"php": "^7.4|^8.0",
"illuminate/support": "^8.0"
},
"require-dev": {
Expand Down
45 changes: 23 additions & 22 deletions resources/lang/en/validationRules.php
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',
];
45 changes: 23 additions & 22 deletions resources/lang/nl/validationRules.php
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',
];
49 changes: 49 additions & 0 deletions src/Rules/DateHasSpecificMinutes.php
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)
]);
}
}
36 changes: 36 additions & 0 deletions tests/Rules/DateHasSpecificMinutesTest.php
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'));
}
}

0 comments on commit 61c8da7

Please sign in to comment.