Skip to content

Commit

Permalink
update: added support for timezone constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
erik-perri committed Nov 11, 2024
1 parent 98dc925 commit 14cbb99
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 7 deletions.
13 changes: 10 additions & 3 deletions src/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -1218,13 +1218,20 @@ public static function string(): string
}

/**
* The field under validation must be a valid timezone identifier according to the *timezone_identifiers_list* PHP
* function.
* The field under validation must be a valid timezone identifier according to the
* {@see DateTimeZone::listIdentifiers} method.
*
* @param ?string $timezoneGroup One of the {@see DateTimeZone} class constant names.
* @param ?string $countryCode A two-letter (uppercase) ISO 3166-1 compatible country code. Note: This option is only used when timezoneGroup is set to "per_country".
* @link https://laravel.com/docs/11.x/validation#rule-timezone
* @link https://www.php.net/manual/en/datetimezone.listidentifiers.php
*/
public static function timezone(): string
public static function timezone(?string $timezoneGroup = null, ?string $countryCode = null): string
{
if ($timezoneGroup !== null || $countryCode !== null) {
return 'timezone:'.($timezoneGroup ?? 'all').','.$countryCode;
}

return 'timezone';
}

Expand Down
11 changes: 7 additions & 4 deletions src/RuleSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -1286,14 +1286,17 @@ public function string(): self
}

/**
* The field under validation must be a valid timezone identifier according to the *timezone_identifiers_list* PHP
* function.
* The field under validation must be a valid timezone identifier according to the
* {@see DateTimeZone::listIdentifiers} method.
*
* @param ?string $timezoneGroup One of the {@see DateTimeZone} class constant names.
* @param ?string $countryCode A two-letter (uppercase) ISO 3166-1 compatible country code. Note: This option is only used when timezoneGroup is set to "per_country".
* @link https://laravel.com/docs/11.x/validation#rule-timezone
* @link https://www.php.net/manual/en/datetimezone.listidentifiers.php
*/
public function timezone(): self
public function timezone(?string $timezoneGroup = null, ?string $countryCode = null): self
{
return $this->rule(Rule::timezone());
return $this->rule(Rule::timezone($timezoneGroup, $countryCode));
}

/**
Expand Down
25 changes: 25 additions & 0 deletions tests/Unit/RuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2771,11 +2771,36 @@ public function validate(string $attribute, mixed $value, Closure $fail): void
'rules' => fn() => RuleSet::create()->timezone(),
'fails' => false,
],
'timezone valid europe' => [
'data' => 'Europe/London',
'rules' => fn() => RuleSet::create()->timezone(),
'fails' => false,
],
'timezone invalid' => [
'data' => 'not a timezone',
'rules' => fn() => RuleSet::create()->timezone(),
'fails' => true,
],
'timezone constrained to country valid' => [
'data' => 'America/New_York',
'rules' => fn() => RuleSet::create()->timezone('america'),
'fails' => false,
],
'timezone constrained to country invalid' => [
'data' => 'Europe/London',
'rules' => fn() => RuleSet::create()->timezone('america'),
'fails' => true,
],
'timezone constrained to country code valid' => [
'data' => 'America/New_York',
'rules' => fn() => RuleSet::create()->timezone('per_country', 'us'),
'fails' => false,
],
'timezone constrained to country code invalid' => [
'data' => 'Europe/London',
'rules' => fn() => RuleSet::create()->timezone('per_country', 'us'),
'fails' => true,
],
'ulid valid' => [
'data' => Str::ulid()->toBase32(),
'rules' => fn() => RuleSet::create()->ulid(),
Expand Down

0 comments on commit 14cbb99

Please sign in to comment.