Skip to content

Commit 9e69a59

Browse files
modified codes add new methods
1 parent 7d461da commit 9e69a59

File tree

6 files changed

+156
-87
lines changed

6 files changed

+156
-87
lines changed

CHANGELOG.md

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,32 @@
1-
# Changelog
1+
# CHANGELOG
22

33
All notable changes to `shergela/validation-rule` will be documented in this file.
44

5-
## 1.0.1 - Friday 08, August - 2024
5+
## 1.0.2 | 19 August - 2024
66

7+
## These methods work with timezones
8+
### You need to provide city names in the methods.
9+
10+
- add new `timezones()` method
11+
- add new `timezoneAfrica()` method
12+
- add new `timezoneAsia()` method
13+
- add new `timezoneEurope()` method
14+
- add new `timezoneAmerica()` method
15+
- add new `timezoneAntarctica()` method
16+
- add new `timezoneArctic()` method
17+
- add new `timezoneAtlantic()` method
18+
- add new `timezoneAustralia()` method
19+
- add new `timezoneIndian()` method
20+
- add new `timezonePacific()` method
21+
22+
## 1.0.1 | 16 August - 2024
23+
24+
- add new `lowercaseFirstLetter()` method
725
- add new `separateIntegersByComma()` method
826
- add new `separateStringsByComma()` method
927
- add new `separateStringsByUnderscore()` method
1028

11-
## 1.0.0 - Tuesday 23, July - 2024
29+
## 1.0.0 | 23 July - 2024
1230

1331
- add new `size()` method
1432
- add new `endsWith()` method

README.md

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -28,48 +28,48 @@ return [
2828

2929
## Available rules (47)
3030

31-
| **Methods** | **Laravel Rule** | **Methods** | **Rule** |
32-
|:-------------------------:|:---------------------:|:------------------------:|:--------------------------:|
33-
| Rule::required() | required | ->startsWith() | starts_with:foo,bar... |
34-
| Rule::nullable() | nullable | ->uppercaseFirstLetter() | new UppercaseFirstLetter() |
35-
| Rule::boolean() | boolean | ->size() | size |
36-
| Rule::rules() | custom rules | ->endsWith() | ends_with:foo,bar |
37-
| ->email() | email | ->doesntStartWith() | doesnt_start_with:foo,bar |
38-
| ->uniqueEmail() | unique:users,email | ->doesntEndWith() | doesnt_end_with:foo,bar |
39-
| ->min() | min | ->in() | in:foo,bar... |
40-
| ->max() | max | ->notIn() | not_in:foo,bar,baz... |
41-
| ->minDigits() | min_digits | | |
42-
| ->maxDigits() | max_digits | | |
43-
| ->integer() | integer | | |
44-
| ->numeric() | numeric | | |
45-
| ->digits() | digits:1 | | |
46-
| ->digitsBetween() | digits:1,4 | | |
47-
| ->decimal() | decimal | | |
48-
| ->alpha() | alpha | | |
49-
| ->alphaDash() | alpha_dash | | |
50-
| ->alphaNum() | alpha_num | | |
51-
| ->string() | string | | |
52-
| ->uppercase() | uppercase | | |
53-
| ->lowercase() | lowercase | | |
54-
| ->regex() | regex:pattern | | |
55-
| ->hexColor() | hex_color | | |
56-
| ->json() | json | | |
57-
| ->url() | url or url:http,https | | |
58-
| ->uuid() | uuid | | |
59-
| ->ulid() | ulid | | |
60-
| ->timezone() | timezone | | |
61-
| ->date() | date | | |
62-
| ->dateFormat() | date_format | | |
63-
| ->dateEquals() | date_equals | | |
64-
| ->dateBefore() | before | | |
65-
| ->dateBeforeOrEqual() | before_or_equal | | |
66-
| ->dateAfter() | after | | |
67-
| ->dateAfterOrEqualToday() | after_or_equal:today | | |
68-
| ->dateAfterOrEquals() | after_or_equal | | |
69-
| ->ip() | ip | | |
70-
| ->ipv4() | ipv4 | | |
71-
| ->ipv6() | ipv6 | | |
72-
| ->macAddress() | mac_address | | |
31+
| **Methods** | **Laravel Rule** | **Methods** | **Rule** |
32+
|:-------------------------:|:---------------------:|:-------------------------------:|:---------------------------------:|
33+
| Rule::required() | required | ->startsWith() | starts_with:foo,bar... |
34+
| Rule::nullable() | nullable | ->uppercaseFirstLetter() | new UppercaseFirstLetter() |
35+
| Rule::boolean() | boolean | ->size() | size |
36+
| Rule::rules() | custom rules | ->endsWith() | ends_with:foo,bar |
37+
| ->email() | email | ->doesntStartWith() | doesnt_start_with:foo,bar |
38+
| ->uniqueEmail() | unique:users,email | ->doesntEndWith() | doesnt_end_with:foo,bar |
39+
| ->min() | min | ->in() | in:foo,bar... |
40+
| ->max() | max | ->notIn() | not_in:foo,bar,baz... |
41+
| ->minDigits() | min_digits | ->separateIntegersByComma() | new SeparateIntegersByComma() |
42+
| ->maxDigits() | max_digits | ->separateStringsByComma() | new SeparateStringsByComma() |
43+
| ->integer() | integer | ->separateStringsByUnderscore() | new SeparateStringsByUnderscore() |
44+
| ->numeric() | numeric | ->timezones() | new TimezoneValidation() |
45+
| ->digits() | digits:1 | ->timezoneAsia() | new TimezoneRegionValidation() |
46+
| ->digitsBetween() | digits:1,4 | ->timezoneEurope() | new TimezoneRegionValidation() |
47+
| ->decimal() | decimal | ->timezoneAmerica() | new TimezoneRegionValidation() |
48+
| ->alpha() | alpha | ->timezoneAntarctica() | new TimezoneRegionValidation() |
49+
| ->alphaDash() | alpha_dash | ->timezoneArctic() | new TimezoneRegionValidation() |
50+
| ->alphaNum() | alpha_num | ->timezoneAtlantic() | new TimezoneRegionValidation() |
51+
| ->string() | string | ->timezoneAustralia() | new TimezoneRegionValidation() |
52+
| ->uppercase() | uppercase | ->timezoneIndian() | new TimezoneRegionValidation() |
53+
| ->lowercase() | lowercase | ->timezonePacific() | new TimezoneRegionValidation() |
54+
| ->regex() | regex:pattern | ->lowercaseFirstLetter() | new LowercaseFirstLetter() |
55+
| ->hexColor() | hex_color | | |
56+
| ->json() | json | | |
57+
| ->url() | url or url:http,https | | |
58+
| ->uuid() | uuid | | |
59+
| ->ulid() | ulid | | |
60+
| ->timezone() | timezone | | |
61+
| ->date() | date | | |
62+
| ->dateFormat() | date_format | | |
63+
| ->dateEquals() | date_equals | | |
64+
| ->dateBefore() | before | | |
65+
| ->dateBeforeOrEqual() | before_or_equal | | |
66+
| ->dateAfter() | after | | |
67+
| ->dateAfterOrEqualToday() | after_or_equal:today | | |
68+
| ->dateAfterOrEquals() | after_or_equal | | |
69+
| ->ip() | ip | | |
70+
| ->ipv4() | ipv4 | | |
71+
| ->ipv6() | ipv6 | | |
72+
| ->macAddress() | mac_address | | |
7373

7474
</div>
7575

src/Rules/TimezoneRegionValidation.php

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Closure;
66
use DateTimeZone;
77
use Illuminate\Contracts\Validation\ValidationRule;
8+
use Illuminate\Support\Str;
89

910
class TimezoneRegionValidation implements ValidationRule
1011
{
@@ -14,9 +15,9 @@ class TimezoneRegionValidation implements ValidationRule
1415
* @param array<string> $cities
1516
*/
1617
public function __construct(
17-
protected array $cities,
18-
protected int $timezoneGroupNumber,
19-
protected string $timezoneGroup,
18+
protected readonly array $cities,
19+
protected readonly int $timezoneGroupNumber,
20+
protected readonly string $timezoneGroup,
2021
) {
2122
}
2223

@@ -35,24 +36,18 @@ public function validate(string $attribute, mixed $value, Closure $fail): void
3536

3637
$search = $this->timezoneGroup . '/' . $toString;
3738

38-
$timezonesList = $this->getTimezoneLists();
39-
4039
/**
41-
* Validate cities if is valid for time zone regions.
40+
* Validate provided cities
4241
*/
43-
if ($this->validateCities() === false) {
42+
if ($this->validateProvidedValues() === false) {
4443
$fail($this->message);
4544
return;
4645
};
4746

48-
$timezonesList = collect($timezonesList)->map(function ($value) {
49-
return strtolower($value);
50-
})->toArray();
51-
52-
if (! in_array($search, $timezonesList)) {
47+
if (! in_array($search, $this->getTimezoneLists())) {
5348
$fail(
5449
sprintf(
55-
"The city name [:attribute] (%s) is not in the valid timezone for (%s) list.",
50+
"The city name [input: :attribute] (%s) is not in the valid timezone for (%s) list.",
5651
ucfirst($toString),
5752
ucfirst($this->timezoneGroup)
5853
)
@@ -63,13 +58,14 @@ public function validate(string $attribute, mixed $value, Closure $fail): void
6358
/**
6459
* @return bool
6560
*/
66-
private function validateCities(): bool
61+
private function validateProvidedValues(): bool
6762
{
6863
foreach ($this->cities as $city) {
6964
if (! in_array($city, $this->getTimezoneLists())) {
65+
$after = ucfirst(Str::after($city, '/'));
7066
$this->message = sprintf(
7167
"This timezone [%s] is not in the valid timezone for [%s].",
72-
ucfirst($city),
68+
ucfirst($this->timezoneGroup . '/' . $after),
7369
ucfirst($this->timezoneGroup)
7470
);
7571
return false;
@@ -83,10 +79,13 @@ private function validateCities(): bool
8379
*/
8480
private function getTimezoneLists(): array
8581
{
86-
return collect(DateTimeZone::listIdentifiers($this->timezoneGroupNumber))->map(
82+
/** @var array<string> $list */
83+
$list = collect(DateTimeZone::listIdentifiers($this->timezoneGroupNumber))->map(
8784
function ($value) {
8885
return strtolower($value);
8986
}
9087
)->toArray();
88+
89+
return $list;
9190
}
9291
}

src/Rules/TimezoneValidation.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class TimezoneValidation implements ValidationRule
1010
/**
1111
* @param array<string> $timezones
1212
*/
13-
public function __construct(protected array $timezones)
13+
public function __construct(protected readonly array $timezones)
1414
{
1515
}
1616

src/Validation/BuildValidationRule.php

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,17 @@ class BuildValidationRule
186186
* @var array<string>
187187
*/
188188
protected array $timezoneIdentifierCities = [];
189-
protected ?int $dateTimezoneGroupNumber = null;
190-
protected ?string $dateTimezoneGroupName = null;
189+
190+
/**
191+
* @var int
192+
* No timezone
193+
*/
194+
protected int $dateTimezoneGroupNumber = 0;
195+
196+
/**
197+
* @var string
198+
*/
199+
protected string $dateTimezoneGroupName = '';
191200

192201
/**
193202
* @var bool
@@ -197,13 +206,13 @@ class BuildValidationRule
197206
/**
198207
* @var bool
199208
*/
200-
protected bool $uppercaseFirstLetter = false;
201-
protected bool $lowercaseFirstLetter = false;
209+
protected bool $lowerCase = false;
202210

203211
/**
204212
* @var bool
205213
*/
206-
protected bool $lowerCase = false;
214+
protected bool $uppercaseFirstLetter = false;
215+
protected bool $lowercaseFirstLetter = false;
207216

208217
/**
209218
* @var string|null
@@ -214,14 +223,25 @@ class BuildValidationRule
214223
* @var bool
215224
*/
216225
protected bool $separateIntegersByComma = false;
226+
227+
/**
228+
* @var bool
229+
*/
217230
protected bool $separateStringsByComma = false;
231+
232+
/**
233+
* @var bool
234+
*/
218235
protected bool $separateStringsByUnderscore = false;
219236

220237
/**
221238
* @var bool
222239
*/
223240
protected bool $hexColor = false;
224241

242+
/**
243+
* @var string|null
244+
*/
225245
protected ?string $url = null;
226246

227247
/**
@@ -369,15 +389,17 @@ protected function buildValidationRules(): array
369389
...($this->timezone !== null ? [DateRule::TIMEZONE_ALL] : []),
370390
...($this->timezones !== null ? [new TimezoneValidation(timezones: $this->timezones)] : []),
371391

372-
...(!empty($this->timezoneIdentifierCities)
392+
...(
393+
!empty($this->timezoneIdentifierCities)
373394
? [
374395
new TimezoneRegionValidation(
375396
cities: $this->timezoneIdentifierCities,
376397
timezoneGroupNumber: $this->dateTimezoneGroupNumber,
377398
timezoneGroup: $this->dateTimezoneGroupName
378399
)
379400
]
380-
: []),
401+
: []
402+
),
381403

382404
...($this->dateBeforeOrEqual !== null
383405
? [DateRule::DATE_BEFORE_OR_EQUAL . $this->dateBeforeOrEqual]

0 commit comments

Comments
 (0)