From 0ec339392b2a5cb67d19e87d4fc2d3d19e13f170 Mon Sep 17 00:00:00 2001 From: Oliver Vogel Date: Mon, 11 Nov 2024 10:13:37 +0100 Subject: [PATCH] Optimize code --- src/Rules/Domainname.php | 2 +- src/Rules/Grid.php | 10 +++++++++- src/Rules/Isin.php | 6 +++++- src/Rules/Postalcode.php | 7 ++++--- 4 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/Rules/Domainname.php b/src/Rules/Domainname.php index 7d1d737..47331af 100644 --- a/src/Rules/Domainname.php +++ b/src/Rules/Domainname.php @@ -69,7 +69,7 @@ private function isValidLabel(string $value): bool */ private function isValidALabel(string $value): bool { - return substr($value, 0, 4) === 'xn--' && $this->idnToUtf8($value) !== false; + return substr($value, 0, 4) === 'xn--' && $this->idnToUtf8($value) != false; } /** diff --git a/src/Rules/Grid.php b/src/Rules/Grid.php index f2f9159..291d50e 100644 --- a/src/Rules/Grid.php +++ b/src/Rules/Grid.php @@ -73,6 +73,14 @@ private function hasValidChecksum(string $value): bool */ private function charValue(string $char): int { - return is_numeric($char) ? (int) $char : (int) str_replace(range('A', 'Z'), range(10, 35), strtoupper($char)); + if (is_numeric($char)) { + return (int) $char; + } + + return (int) str_replace( + range('A', 'Z'), + array_map(fn (int $val) => strval($val), range(10, 35)), + strtoupper($char), + ); } } diff --git a/src/Rules/Isin.php b/src/Rules/Isin.php index d271db8..fb4c89f 100644 --- a/src/Rules/Isin.php +++ b/src/Rules/Isin.php @@ -69,7 +69,11 @@ public function normalize(string $value): string */ private function replaceChars(string $value): string { - return str_replace($this->chars, array_keys($this->chars), $value); + return str_replace( + $this->chars, + array_map(fn (int $value) => strval($value), array_keys($this->chars)), + $value, + ); } /** diff --git a/src/Rules/Postalcode.php b/src/Rules/Postalcode.php index 4a4e9b6..b3f711f 100644 --- a/src/Rules/Postalcode.php +++ b/src/Rules/Postalcode.php @@ -14,7 +14,7 @@ class Postalcode extends AbstractRule implements DataAwareRule * * @var ?string */ - private ?string $reference; + private ?string $reference = null; /** * Data set used for validation @@ -30,6 +30,7 @@ class Postalcode extends AbstractRule implements DataAwareRule */ public function __construct(protected array $countrycodes = []) { + // } /** @@ -112,7 +113,7 @@ private function countryCodes(): array { if (count($this->countrycodes) == 0) { // return country code by reference - if (is_array($this->data) && array_key_exists($this->reference, $this->data)) { + if (!is_null($this->reference) && array_key_exists($this->reference, $this->data)) { return [$this->data[$this->reference]]; } } @@ -151,7 +152,7 @@ private function pattern(string $countrycode): ?string 'sz' => "/^[a-z]{1}[0-9]{3}$/i", 'tw' => "/^[0-9]{3}([0-9]{2})?$/", 'gb' => "/^(([a-z][0-9])|([a-z][0-9]{2})|([a-z][0-9][a-z])|([a-z]{2}[0-9])" . - "|([a-z]{2}[0-9]{2})|([a-z]{2}[0-9][a-z])) [0-9][a-z]{2}$/i", + "|([a-z]{2}[0-9]{2})|([a-z]{2}[0-9][a-z])) [0-9][a-z]{2}$/i", 'ie' => "/^[A-Za-z][A-Za-z0-9]{2} [A-Za-z0-9]{4}$/i", default => null, };