Skip to content

Commit

Permalink
modified codes add new methods
Browse files Browse the repository at this point in the history
  • Loading branch information
SkyWalkerGhost committed Aug 19, 2024
1 parent 7d461da commit 9e69a59
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 87 deletions.
24 changes: 21 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
84 changes: 42 additions & 42 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 | | |

</div>

Expand Down
31 changes: 15 additions & 16 deletions src/Rules/TimezoneRegionValidation.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Closure;
use DateTimeZone;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Support\Str;

class TimezoneRegionValidation implements ValidationRule
{
Expand All @@ -14,9 +15,9 @@ class TimezoneRegionValidation implements ValidationRule
* @param array<string> $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,
) {
}

Expand All @@ -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)
)
Expand All @@ -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;
Expand All @@ -83,10 +79,13 @@ private function validateCities(): bool
*/
private function getTimezoneLists(): array
{
return collect(DateTimeZone::listIdentifiers($this->timezoneGroupNumber))->map(
/** @var array<string> $list */
$list = collect(DateTimeZone::listIdentifiers($this->timezoneGroupNumber))->map(
function ($value) {
return strtolower($value);
}
)->toArray();

return $list;
}
}
2 changes: 1 addition & 1 deletion src/Rules/TimezoneValidation.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class TimezoneValidation implements ValidationRule
/**
* @param array<string> $timezones
*/
public function __construct(protected array $timezones)
public function __construct(protected readonly array $timezones)
{
}

Expand Down
36 changes: 29 additions & 7 deletions src/Validation/BuildValidationRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,17 @@ class BuildValidationRule
* @var array<string>
*/
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
Expand All @@ -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
Expand All @@ -214,14 +223,25 @@ class BuildValidationRule
* @var bool
*/
protected bool $separateIntegersByComma = false;

/**
* @var bool
*/
protected bool $separateStringsByComma = false;

/**
* @var bool
*/
protected bool $separateStringsByUnderscore = false;

/**
* @var bool
*/
protected bool $hexColor = false;

/**
* @var string|null
*/
protected ?string $url = null;

/**
Expand Down Expand Up @@ -369,15 +389,17 @@ 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,
timezoneGroupNumber: $this->dateTimezoneGroupNumber,
timezoneGroup: $this->dateTimezoneGroupName
)
]
: []),
: []
),

...($this->dateBeforeOrEqual !== null
? [DateRule::DATE_BEFORE_OR_EQUAL . $this->dateBeforeOrEqual]
Expand Down
Loading

0 comments on commit 9e69a59

Please sign in to comment.