From 9e69a59a8d8beedd2bf62e34c3f30b0f8b2e44d2 Mon Sep 17 00:00:00 2001 From: SkyWalkerGhost Date: Mon, 19 Aug 2024 11:52:18 +0300 Subject: [PATCH] modified codes add new methods --- CHANGELOG.md | 24 +++++++- README.md | 84 +++++++++++++------------- src/Rules/TimezoneRegionValidation.php | 31 +++++----- src/Rules/TimezoneValidation.php | 2 +- src/Validation/BuildValidationRule.php | 36 ++++++++--- src/Validation/Rule.php | 66 ++++++++++++++------ 6 files changed, 156 insertions(+), 87 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 75f3dde..3689df1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,14 +1,32 @@ -# Changelog +# CHANGELOG All notable changes to `shergela/validation-rule` will be documented in this file. -## 1.0.1 - Friday 08, August - 2024 +## 1.0.2 | 19 August - 2024 +## These methods work with timezones +### You need to provide city names in the methods. + +- add new `timezones()` method +- add new `timezoneAfrica()` method +- add new `timezoneAsia()` method +- add new `timezoneEurope()` method +- add new `timezoneAmerica()` method +- add new `timezoneAntarctica()` method +- add new `timezoneArctic()` method +- add new `timezoneAtlantic()` method +- add new `timezoneAustralia()` method +- add new `timezoneIndian()` method +- add new `timezonePacific()` method + +## 1.0.1 | 16 August - 2024 + +- add new `lowercaseFirstLetter()` method - add new `separateIntegersByComma()` method - add new `separateStringsByComma()` method - add new `separateStringsByUnderscore()` method -## 1.0.0 - Tuesday 23, July - 2024 +## 1.0.0 | 23 July - 2024 - add new `size()` method - add new `endsWith()` method diff --git a/README.md b/README.md index 326dd1e..91edf58 100644 --- a/README.md +++ b/README.md @@ -28,48 +28,48 @@ return [ ## Available rules (47) -| **Methods** | **Laravel Rule** | **Methods** | **Rule** | -|:-------------------------:|:---------------------:|:------------------------:|:--------------------------:| -| Rule::required() | required | ->startsWith() | starts_with:foo,bar... | -| Rule::nullable() | nullable | ->uppercaseFirstLetter() | new UppercaseFirstLetter() | -| Rule::boolean() | boolean | ->size() | size | -| Rule::rules() | custom rules | ->endsWith() | ends_with:foo,bar | -| ->email() | email | ->doesntStartWith() | doesnt_start_with:foo,bar | -| ->uniqueEmail() | unique:users,email | ->doesntEndWith() | doesnt_end_with:foo,bar | -| ->min() | min | ->in() | in:foo,bar... | -| ->max() | max | ->notIn() | not_in:foo,bar,baz... | -| ->minDigits() | min_digits | | | -| ->maxDigits() | max_digits | | | -| ->integer() | integer | | | -| ->numeric() | numeric | | | -| ->digits() | digits:1 | | | -| ->digitsBetween() | digits:1,4 | | | -| ->decimal() | decimal | | | -| ->alpha() | alpha | | | -| ->alphaDash() | alpha_dash | | | -| ->alphaNum() | alpha_num | | | -| ->string() | string | | | -| ->uppercase() | uppercase | | | -| ->lowercase() | lowercase | | | -| ->regex() | regex:pattern | | | -| ->hexColor() | hex_color | | | -| ->json() | json | | | -| ->url() | url or url:http,https | | | -| ->uuid() | uuid | | | -| ->ulid() | ulid | | | -| ->timezone() | timezone | | | -| ->date() | date | | | -| ->dateFormat() | date_format | | | -| ->dateEquals() | date_equals | | | -| ->dateBefore() | before | | | -| ->dateBeforeOrEqual() | before_or_equal | | | -| ->dateAfter() | after | | | -| ->dateAfterOrEqualToday() | after_or_equal:today | | | -| ->dateAfterOrEquals() | after_or_equal | | | -| ->ip() | ip | | | -| ->ipv4() | ipv4 | | | -| ->ipv6() | ipv6 | | | -| ->macAddress() | mac_address | | | +| **Methods** | **Laravel Rule** | **Methods** | **Rule** | +|:-------------------------:|:---------------------:|:-------------------------------:|:---------------------------------:| +| Rule::required() | required | ->startsWith() | starts_with:foo,bar... | +| Rule::nullable() | nullable | ->uppercaseFirstLetter() | new UppercaseFirstLetter() | +| Rule::boolean() | boolean | ->size() | size | +| Rule::rules() | custom rules | ->endsWith() | ends_with:foo,bar | +| ->email() | email | ->doesntStartWith() | doesnt_start_with:foo,bar | +| ->uniqueEmail() | unique:users,email | ->doesntEndWith() | doesnt_end_with:foo,bar | +| ->min() | min | ->in() | in:foo,bar... | +| ->max() | max | ->notIn() | not_in:foo,bar,baz... | +| ->minDigits() | min_digits | ->separateIntegersByComma() | new SeparateIntegersByComma() | +| ->maxDigits() | max_digits | ->separateStringsByComma() | new SeparateStringsByComma() | +| ->integer() | integer | ->separateStringsByUnderscore() | new SeparateStringsByUnderscore() | +| ->numeric() | numeric | ->timezones() | new TimezoneValidation() | +| ->digits() | digits:1 | ->timezoneAsia() | new TimezoneRegionValidation() | +| ->digitsBetween() | digits:1,4 | ->timezoneEurope() | new TimezoneRegionValidation() | +| ->decimal() | decimal | ->timezoneAmerica() | new TimezoneRegionValidation() | +| ->alpha() | alpha | ->timezoneAntarctica() | new TimezoneRegionValidation() | +| ->alphaDash() | alpha_dash | ->timezoneArctic() | new TimezoneRegionValidation() | +| ->alphaNum() | alpha_num | ->timezoneAtlantic() | new TimezoneRegionValidation() | +| ->string() | string | ->timezoneAustralia() | new TimezoneRegionValidation() | +| ->uppercase() | uppercase | ->timezoneIndian() | new TimezoneRegionValidation() | +| ->lowercase() | lowercase | ->timezonePacific() | new TimezoneRegionValidation() | +| ->regex() | regex:pattern | ->lowercaseFirstLetter() | new LowercaseFirstLetter() | +| ->hexColor() | hex_color | | | +| ->json() | json | | | +| ->url() | url or url:http,https | | | +| ->uuid() | uuid | | | +| ->ulid() | ulid | | | +| ->timezone() | timezone | | | +| ->date() | date | | | +| ->dateFormat() | date_format | | | +| ->dateEquals() | date_equals | | | +| ->dateBefore() | before | | | +| ->dateBeforeOrEqual() | before_or_equal | | | +| ->dateAfter() | after | | | +| ->dateAfterOrEqualToday() | after_or_equal:today | | | +| ->dateAfterOrEquals() | after_or_equal | | | +| ->ip() | ip | | | +| ->ipv4() | ipv4 | | | +| ->ipv6() | ipv6 | | | +| ->macAddress() | mac_address | | | diff --git a/src/Rules/TimezoneRegionValidation.php b/src/Rules/TimezoneRegionValidation.php index 761b460..9b35e33 100644 --- a/src/Rules/TimezoneRegionValidation.php +++ b/src/Rules/TimezoneRegionValidation.php @@ -5,6 +5,7 @@ use Closure; use DateTimeZone; use Illuminate\Contracts\Validation\ValidationRule; +use Illuminate\Support\Str; class TimezoneRegionValidation implements ValidationRule { @@ -14,9 +15,9 @@ class TimezoneRegionValidation implements ValidationRule * @param array $cities */ public function __construct( - protected array $cities, - protected int $timezoneGroupNumber, - protected string $timezoneGroup, + protected readonly array $cities, + protected readonly int $timezoneGroupNumber, + protected readonly string $timezoneGroup, ) { } @@ -35,24 +36,18 @@ public function validate(string $attribute, mixed $value, Closure $fail): void $search = $this->timezoneGroup . '/' . $toString; - $timezonesList = $this->getTimezoneLists(); - /** - * Validate cities if is valid for time zone regions. + * Validate provided cities */ - if ($this->validateCities() === false) { + if ($this->validateProvidedValues() === false) { $fail($this->message); return; }; - $timezonesList = collect($timezonesList)->map(function ($value) { - return strtolower($value); - })->toArray(); - - if (! in_array($search, $timezonesList)) { + if (! in_array($search, $this->getTimezoneLists())) { $fail( sprintf( - "The city name [:attribute] (%s) is not in the valid timezone for (%s) list.", + "The city name [input: :attribute] (%s) is not in the valid timezone for (%s) list.", ucfirst($toString), ucfirst($this->timezoneGroup) ) @@ -63,13 +58,14 @@ public function validate(string $attribute, mixed $value, Closure $fail): void /** * @return bool */ - private function validateCities(): bool + private function validateProvidedValues(): bool { foreach ($this->cities as $city) { if (! in_array($city, $this->getTimezoneLists())) { + $after = ucfirst(Str::after($city, '/')); $this->message = sprintf( "This timezone [%s] is not in the valid timezone for [%s].", - ucfirst($city), + ucfirst($this->timezoneGroup . '/' . $after), ucfirst($this->timezoneGroup) ); return false; @@ -83,10 +79,13 @@ private function validateCities(): bool */ private function getTimezoneLists(): array { - return collect(DateTimeZone::listIdentifiers($this->timezoneGroupNumber))->map( + /** @var array $list */ + $list = collect(DateTimeZone::listIdentifiers($this->timezoneGroupNumber))->map( function ($value) { return strtolower($value); } )->toArray(); + + return $list; } } diff --git a/src/Rules/TimezoneValidation.php b/src/Rules/TimezoneValidation.php index 36235bf..29a00d1 100644 --- a/src/Rules/TimezoneValidation.php +++ b/src/Rules/TimezoneValidation.php @@ -10,7 +10,7 @@ class TimezoneValidation implements ValidationRule /** * @param array $timezones */ - public function __construct(protected array $timezones) + public function __construct(protected readonly array $timezones) { } diff --git a/src/Validation/BuildValidationRule.php b/src/Validation/BuildValidationRule.php index 701ae57..2ed83f1 100644 --- a/src/Validation/BuildValidationRule.php +++ b/src/Validation/BuildValidationRule.php @@ -186,8 +186,17 @@ class BuildValidationRule * @var array */ protected array $timezoneIdentifierCities = []; - protected ?int $dateTimezoneGroupNumber = null; - protected ?string $dateTimezoneGroupName = null; + + /** + * @var int + * No timezone + */ + protected int $dateTimezoneGroupNumber = 0; + + /** + * @var string + */ + protected string $dateTimezoneGroupName = ''; /** * @var bool @@ -197,13 +206,13 @@ class BuildValidationRule /** * @var bool */ - protected bool $uppercaseFirstLetter = false; - protected bool $lowercaseFirstLetter = false; + protected bool $lowerCase = false; /** * @var bool */ - protected bool $lowerCase = false; + protected bool $uppercaseFirstLetter = false; + protected bool $lowercaseFirstLetter = false; /** * @var string|null @@ -214,7 +223,15 @@ class BuildValidationRule * @var bool */ protected bool $separateIntegersByComma = false; + + /** + * @var bool + */ protected bool $separateStringsByComma = false; + + /** + * @var bool + */ protected bool $separateStringsByUnderscore = false; /** @@ -222,6 +239,9 @@ class BuildValidationRule */ protected bool $hexColor = false; + /** + * @var string|null + */ protected ?string $url = null; /** @@ -369,7 +389,8 @@ protected function buildValidationRules(): array ...($this->timezone !== null ? [DateRule::TIMEZONE_ALL] : []), ...($this->timezones !== null ? [new TimezoneValidation(timezones: $this->timezones)] : []), - ...(!empty($this->timezoneIdentifierCities) + ...( + !empty($this->timezoneIdentifierCities) ? [ new TimezoneRegionValidation( cities: $this->timezoneIdentifierCities, @@ -377,7 +398,8 @@ protected function buildValidationRules(): array timezoneGroup: $this->dateTimezoneGroupName ) ] - : []), + : [] + ), ...($this->dateBeforeOrEqual !== null ? [DateRule::DATE_BEFORE_OR_EQUAL . $this->dateBeforeOrEqual] diff --git a/src/Validation/Rule.php b/src/Validation/Rule.php index f9cdc19..e9fc66f 100644 --- a/src/Validation/Rule.php +++ b/src/Validation/Rule.php @@ -48,6 +48,11 @@ class Rule extends BuildValidationRule implements ValidationRule, ValidatorAware */ protected array $customMessages = []; + /** + * @var array + */ + protected array $customAttributes = []; + /** * Additional validation rules that should be merged into the default rules during validation. * @var array @@ -475,11 +480,14 @@ public function timezoneAsia(array $cities): static } /** - * @param array $cities + * @param array|string $cities * @return $this */ - public function timezoneEurope(array $cities): static + public function timezoneEurope(array|string $cities): static { + /** @var array $cities */ + $cities = is_array($cities) ? $cities : func_get_args(); + $this->timezoneIdentifierCities = $this->collectCities( cities: $cities, timezone: DatetimeZoneAbbreviationEnum::EUROPE, @@ -492,11 +500,14 @@ public function timezoneEurope(array $cities): static } /** - * @param array $cities + * @param array|string $cities * @return $this */ - public function timezoneAmerica(array $cities): static + public function timezoneAmerica(array|string $cities): static { + /** @var array $cities */ + $cities = is_array($cities) ? $cities : func_get_args(); + $this->timezoneIdentifierCities = $this->collectCities( cities: $cities, timezone: DatetimeZoneAbbreviationEnum::AMERICA, @@ -509,11 +520,14 @@ public function timezoneAmerica(array $cities): static } /** - * @param array $cities + * @param array|string $cities * @return $this */ - public function timezoneAntarctica(array $cities): static + public function timezoneAntarctica(array|string $cities): static { + /** @var array $cities */ + $cities = is_array($cities) ? $cities : func_get_args(); + $this->timezoneIdentifierCities = $this->collectCities( cities: $cities, timezone: DatetimeZoneAbbreviationEnum::ANTARCTICA, @@ -526,11 +540,14 @@ public function timezoneAntarctica(array $cities): static } /** - * @param array $cities + * @param array|string $cities * @return $this */ - public function timezoneArctic(array $cities): static + public function timezoneArctic(array|string $cities): static { + /** @var array $cities */ + $cities = is_array($cities) ? $cities : func_get_args(); + $this->timezoneIdentifierCities = $this->collectCities( cities: $cities, timezone: DatetimeZoneAbbreviationEnum::ARCTIC, @@ -543,11 +560,14 @@ public function timezoneArctic(array $cities): static } /** - * @param array $cities + * @param array|string $cities * @return $this */ - public function timezoneAtlantic(array $cities): static + public function timezoneAtlantic(array|string $cities): static { + /** @var array $cities */ + $cities = is_array($cities) ? $cities : func_get_args(); + $this->timezoneIdentifierCities = $this->collectCities( cities: $cities, timezone: DatetimeZoneAbbreviationEnum::ATLANTIC, @@ -560,11 +580,14 @@ public function timezoneAtlantic(array $cities): static } /** - * @param array $cities + * @param array|string $cities * @return $this */ - public function timezoneAustralia(array $cities): static + public function timezoneAustralia(array|string $cities): static { + /** @var array $cities */ + $cities = is_array($cities) ? $cities : func_get_args(); + $this->timezoneIdentifierCities = $this->collectCities( cities: $cities, timezone: DatetimeZoneAbbreviationEnum::AUSTRALIA, @@ -577,11 +600,14 @@ public function timezoneAustralia(array $cities): static } /** - * @param array $cities + * @param array|string $cities * @return $this */ - public function timezoneIndian(array $cities): static + public function timezoneIndian(array|string $cities): static { + /** @var array $cities */ + $cities = is_array($cities) ? $cities : func_get_args(); + $this->timezoneIdentifierCities = $this->collectCities( cities: $cities, timezone: DatetimeZoneAbbreviationEnum::INDIAN, @@ -594,11 +620,14 @@ public function timezoneIndian(array $cities): static } /** - * @param array $cities + * @param array|string $cities * @return $this */ - public function timezonePacific(array $cities): static + public function timezonePacific(array|string $cities): static { + /** @var array $cities */ + $cities = is_array($cities) ? $cities : func_get_args(); + $this->timezoneIdentifierCities = $this->collectCities( cities: $cities, timezone: DatetimeZoneAbbreviationEnum::PACIFIC, @@ -861,7 +890,7 @@ public function rules(array $rules): static */ public function attributes(array $attributes): static { - $this->validator->customAttributes = $attributes; + $this->customAttributes = $attributes; return $this; } @@ -928,7 +957,8 @@ public function passes(string $attribute, mixed $value): bool $validator = ValidatorFacade::make( data: $this->getValidationData(), rules: [$attribute => $this->getValidationRules()], - messages: $this->validator->customMessages, + messages: $this->customMessages, + attributes: $this->customAttributes )->stopOnFirstFailure();