From 14cbb99a26e967960549237dbc84d61680d25a15 Mon Sep 17 00:00:00 2001 From: Erik Perri Date: Mon, 11 Nov 2024 00:39:28 -0500 Subject: [PATCH] update: added support for timezone constraints --- src/Rule.php | 13 ++++++++++--- src/RuleSet.php | 11 +++++++---- tests/Unit/RuleTest.php | 25 +++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 7 deletions(-) diff --git a/src/Rule.php b/src/Rule.php index cd31f29..e1ea56b 100644 --- a/src/Rule.php +++ b/src/Rule.php @@ -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'; } diff --git a/src/RuleSet.php b/src/RuleSet.php index 2ce8f86..0e34435 100644 --- a/src/RuleSet.php +++ b/src/RuleSet.php @@ -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)); } /** diff --git a/tests/Unit/RuleTest.php b/tests/Unit/RuleTest.php index 02024a2..7a0b2e1 100644 --- a/tests/Unit/RuleTest.php +++ b/tests/Unit/RuleTest.php @@ -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(),