Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit d139d22

Browse files
committed
chore: improved normalizer handling
1 parent cbb51c9 commit d139d22

File tree

4 files changed

+17
-17
lines changed

4 files changed

+17
-17
lines changed

src/Rule/Count.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function assert(mixed $value, ?string $name = null): void
4747
$numElements = \count($value);
4848

4949
if ($this->min !== null && $numElements < $this->min) {
50-
$message = $this->min === $this->max ? $this->exactMessage : $this->minMessage;
50+
$message = ($this->min === $this->max) ? $this->exactMessage : $this->minMessage;
5151

5252
throw new CountException(
5353
message: $message,
@@ -62,7 +62,7 @@ public function assert(mixed $value, ?string $name = null): void
6262
}
6363

6464
if ($this->max !== null && $numElements > $this->max) {
65-
$message = $this->min === $this->max ? $this->exactMessage : $this->maxMessage;
65+
$message = ($this->min === $this->max) ? $this->exactMessage : $this->maxMessage;
6666

6767
throw new CountException(
6868
message: $message,

src/Rule/Email.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ class Email extends AbstractRule implements RuleInterface
2525
self::MODE_HTML5_ALLOW_NO_TLD => '/^[a-zA-Z0-9.!#$%&\'*+\\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/'
2626
];
2727

28-
// Using array to bypass unallowed callable type in properties
29-
private array $normalizer;
28+
/** @var ?callable */
29+
private $normalizer;
3030
private string $message = 'The {{ name }} value is not a valid email address, {{ value }} given.';
3131

3232
public function __construct(
@@ -35,7 +35,7 @@ public function __construct(
3535
?string $message = null
3636
)
3737
{
38-
$this->normalizer['callable'] = $normalizer;
38+
$this->normalizer = $normalizer;
3939
$this->message = $message ?? $this->message;
4040
}
4141

@@ -49,8 +49,8 @@ public function assert(mixed $value, ?string $name = null): void
4949
throw new UnexpectedTypeException('string', get_debug_type($value));
5050
}
5151

52-
if ($this->normalizer['callable'] !== null) {
53-
$value = ($this->normalizer['callable'])($value);
52+
if ($this->normalizer !== null) {
53+
$value = ($this->normalizer)($value);
5454
}
5555

5656
if ($this->mode === self::MODE_STRICT) {

src/Rule/NotBlank.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@
66

77
class NotBlank extends AbstractRule implements RuleInterface
88
{
9-
// Using array to bypass unallowed callable type in properties
10-
private array $normalizer;
9+
/** @var ?callable */
10+
private $normalizer;
1111
private string $message = 'The {{ name }} value should not be blank, {{ value }} given.';
1212

1313
public function __construct(
1414
?callable $normalizer = null,
1515
?string $message = null
1616
)
1717
{
18-
$this->normalizer['callable'] = $normalizer;
18+
$this->normalizer = $normalizer;
1919
$this->message = $message ?? $this->message;
2020
}
2121

@@ -24,8 +24,8 @@ public function __construct(
2424
*/
2525
public function assert(mixed $value, ?string $name = null): void
2626
{
27-
if ($this->normalizer['callable'] !== null) {
28-
$value = ($this->normalizer['callable'])($value);
27+
if ($this->normalizer !== null) {
28+
$value = ($this->normalizer)($value);
2929
}
3030

3131
// Do not allow null, false, [] and ''

src/Rule/Url.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ class Url extends AbstractRule implements RuleInterface
3232
(?:\# (?:[\pL\pN\-._\~!$&\'()*+,;=:@/?]|%%[0-9A-Fa-f]{2})* )? # a fragment (optional)
3333
$~ixu';
3434

35-
// Using array to bypass unallowed callable type in properties
36-
private array $normalizer;
35+
/** @var ?callable */
36+
private $normalizer;
3737
private string $message = 'The {{ name }} value is not a valid URL address, {{ value }} given.';
3838

3939
public function __construct(
@@ -43,7 +43,7 @@ public function __construct(
4343
?string $message = null
4444
)
4545
{
46-
$this->normalizer['callable'] = $normalizer;
46+
$this->normalizer = $normalizer;
4747
$this->message = $message ?? $this->message;
4848
}
4949

@@ -53,8 +53,8 @@ public function assert(mixed $value, ?string $name = null): void
5353
throw new UnexpectedTypeException('string', get_debug_type($value));
5454
}
5555

56-
if ($this->normalizer['callable'] !== null) {
57-
$value = ($this->normalizer['callable'])($value);
56+
if ($this->normalizer !== null) {
57+
$value = ($this->normalizer)($value);
5858
}
5959

6060
$pattern = $this->allowRelativeProtocol ? \str_replace('(%s):', '(?:(%s):)?', self::PATTERN) : self::PATTERN;

0 commit comments

Comments
 (0)