Skip to content

Commit 1fe16af

Browse files
add new methods
modify some code
1 parent 9e7e38d commit 1fe16af

File tree

7 files changed

+200
-74
lines changed

7 files changed

+200
-74
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

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

5+
## 1.0.5 | 30 August - 2024
6+
7+
- add new `array()` method.
8+
- add new `arrayDistinct()` method.
9+
- add new `arrayDistinctStrict()` method.
10+
- add new `arrayDistinctIgnoreCase()` method.
11+
512
## 1.0.4 | 26 August - 2024
613

714
- add new `length()` method.

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,10 @@ return [
9494
| ->timezoneAustralia() | new TimezoneRegionValidation() |
9595
| ->timezoneIndian() | new TimezoneRegionValidation() |
9696
| ->timezonePacific() | new TimezoneRegionValidation() |
97-
| ->length() | size |
97+
| ->array() | array |
98+
| ->arrayDistinct() | distinct |
99+
| ->arrayDistinctStrict() | distinct:strict |
100+
| ->arrayDistinctIgnoreCase() | distinct:ignore_case |
98101
</div>
99102

100103

src/Enums/ValidationArrayEnum.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Shergela\Validations\Enums;
4+
5+
enum ValidationArrayEnum: string
6+
{
7+
/**
8+
* Input value must be a date.
9+
* Date validations
10+
*/
11+
public const ARRAY = 'array';
12+
public const DISTINCT = 'distinct';
13+
public const DISTINCT_STRICT = 'distinct:strict';
14+
public const DISTINCT_IGNORE_CASE = 'distinct:ignore_case';
15+
}

src/Enums/ValidationStringEnum.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ enum ValidationStringEnum: string
1818
*/
1919
public const ALPHA = 'alpha';
2020

21+
public const ASCII = 'ascii';
22+
2123
/**
2224
* The field under validation must be entirely Unicode alphanumeric characters contained in \p{L}, \p{M}, \p{N},
2325
* as well as ASCII dashes (-) and ASCII underscores (_).

src/Rules/TimezoneRegionValidation.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,17 @@ public function validate(string $attribute, mixed $value, Closure $fail): void
4646
};
4747

4848
if (! in_array($search, $this->getTimezoneLists())) {
49-
$fail(
50-
sprintf(
51-
"The city name [input: :attribute] (%s) is not in the valid timezone for (%s) list.",
52-
ucfirst($toString),
53-
ucfirst($this->timezoneGroup)
54-
)
55-
);
49+
if ($this->customMessage !== null) {
50+
$fail($this->customMessage);
51+
} else {
52+
$fail(
53+
sprintf(
54+
"The city name [:attribute] (%s) is not in the valid timezone for (%s) list.",
55+
ucfirst($toString),
56+
ucfirst($this->timezoneGroup)
57+
)
58+
);
59+
}
5660
}
5761
}
5862

src/Validation/BuildValidationRule.php

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Shergela\Validations\Rules\SeparateIntegersByComma as IntegerByComma;
1313
use Shergela\Validations\Rules\SeparateStringsByComma as StringByComma;
1414
use Shergela\Validations\Rules\SeparateStringsByUnderscore as StringByUnderscore;
15+
use Shergela\Validations\Enums\ValidationArrayEnum as ArrayEnum;
1516
use Shergela\Validations\Rules\TimezoneRegionValidation;
1617
use Shergela\Validations\Rules\TimezoneValidation;
1718
use Shergela\Validations\Rules\UppercaseFirstLetter;
@@ -37,7 +38,7 @@ class BuildValidationRule
3738
* @var string|null
3839
* Set custom message
3940
*/
40-
protected static ?string $customMessage = null;
41+
protected static ?string $validationMessage = null;
4142

4243
/**
4344
* @var int|null
@@ -317,6 +318,26 @@ class BuildValidationRule
317318
*/
318319
protected static ?string $notIn = null;
319320

321+
/**
322+
* @var bool
323+
*/
324+
protected bool $array = false;
325+
326+
/**
327+
* @var bool
328+
*/
329+
protected bool $arrayDistinct = false;
330+
331+
/**
332+
* @var bool
333+
*/
334+
protected ?bool $distinctStinct = false;
335+
336+
/**
337+
* @var bool
338+
*/
339+
protected bool $distinctIgnoreCase = false;
340+
320341
/**
321342
* @return array<string>
322343
*/
@@ -384,13 +405,13 @@ protected function buildValidationRules(): array
384405

385406
...(
386407
$this->uppercaseFirstLetter === true
387-
? [new UppercaseFirstLetter(message: static::$customMessage)]
408+
? [new UppercaseFirstLetter(message: static::$validationMessage)]
388409
: []
389410
),
390411

391412
...(
392413
$this->lowercaseFirstLetter === true
393-
? [new LowercaseFirstLetter(message: static::$customMessage)]
414+
? [new LowercaseFirstLetter(message: static::$validationMessage)]
394415
: []
395416
),
396417

@@ -407,7 +428,7 @@ protected function buildValidationRules(): array
407428

408429
...(
409430
$this->timezones !== null
410-
? [new TimezoneValidation(timezones: $this->timezones, message: static::$customMessage)]
431+
? [new TimezoneValidation(timezones: $this->timezones, message: static::$validationMessage)]
411432
: []
412433
),
413434

@@ -418,7 +439,7 @@ protected function buildValidationRules(): array
418439
cities: $this->timezoneIdentifierCities,
419440
timezoneGroupNumber: $this->dateTimezoneGroupNumber,
420441
timezoneGroup: $this->dateTimezoneGroupName,
421-
customMessage: static::$customMessage,
442+
customMessage: static::$validationMessage,
422443
)
423444
]
424445
: []
@@ -439,14 +460,22 @@ protected function buildValidationRules(): array
439460
* Regex validations
440461
*/
441462
...($this->regexPattern !== null ? [RegexRule::RULE . $this->regexPattern] : []),
442-
...($this->separateIntegersByComma === true ? [new IntegerByComma(message: static::$customMessage)] : []),
443-
...($this->separateStringsByComma === true ? [new StringByComma(message: static::$customMessage)] : []),
463+
...($this->separateIntegersByComma === true ? [new IntegerByComma(message: static::$validationMessage)] : []),
464+
...($this->separateStringsByComma === true ? [new StringByComma(message: static::$validationMessage)] : []),
444465

445466
...(
446467
$this->separateStringsByUnderscore === true
447-
? [new StringByUnderscore(message: static::$customMessage)]
468+
? [new StringByUnderscore(message: static::$validationMessage)]
448469
: []
449470
),
471+
472+
/**
473+
* Array validations
474+
*/
475+
...($this->array === true ? [ArrayEnum::ARRAY] : []),
476+
...($this->arrayDistinct === true ? [ArrayEnum::DISTINCT] : []),
477+
...($this->distinctStinct === true ? [ArrayEnum::DISTINCT_STRICT] : []),
478+
...($this->distinctIgnoreCase === true ? [ArrayEnum::DISTINCT_IGNORE_CASE] : []),
450479
];
451480

452481
return $rules;

0 commit comments

Comments
 (0)