Skip to content

Commit

Permalink
Optimize code
Browse files Browse the repository at this point in the history
  • Loading branch information
olivervogel committed Nov 11, 2024
1 parent 3d765ac commit 0ec3393
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/Rules/Domainname.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
10 changes: 9 additions & 1 deletion src/Rules/Grid.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
);
}
}
6 changes: 5 additions & 1 deletion src/Rules/Isin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
}

/**
Expand Down
7 changes: 4 additions & 3 deletions src/Rules/Postalcode.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Postalcode extends AbstractRule implements DataAwareRule
*
* @var ?string
*/
private ?string $reference;
private ?string $reference = null;

/**
* Data set used for validation
Expand All @@ -30,6 +30,7 @@ class Postalcode extends AbstractRule implements DataAwareRule
*/
public function __construct(protected array $countrycodes = [])
{
//
}

/**
Expand Down Expand Up @@ -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]];
}
}
Expand Down Expand Up @@ -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,
};
Expand Down

0 comments on commit 0ec3393

Please sign in to comment.