Skip to content

Commit

Permalink
fix: do not alter the TIN value when calling getTin()
Browse files Browse the repository at this point in the history
Fix #39
  • Loading branch information
drupol committed Jun 6, 2024
1 parent 39e5f92 commit b2cbb19
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
23 changes: 14 additions & 9 deletions src/CountryHandler/CountryHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,7 @@ final public function __construct(string $tin = '')

public function getTIN(): string
{
if (null !== $string = preg_replace('#[^[:alnum:]\-+]#u', '', $this->tin)) {
return strtoupper($string);
}

return '';
return $this->tin;
}

final public static function supports(string $country): bool
Expand All @@ -47,7 +43,7 @@ final public static function supports(string $country): bool

final public function validate(): bool
{
$tin = $this->getTIN();
$tin = $this->normalizeTin($this->getTIN());

if (!$this->hasValidLength($tin)) {
throw TINException::invalidLength($this->tin);
Expand Down Expand Up @@ -116,6 +112,15 @@ protected function getLastDigit(int $number): int
return (int) end($split);
}

protected function normalizeTin(string $tin): string
{
if (null !== $string = preg_replace('#[^[:alnum:]\-+]#u', '', $tin)) {
return strtoupper($string);
}

return '';
}

protected function hasValidDate(string $tin): bool
{
return true;
Expand All @@ -126,12 +131,12 @@ protected function hasValidDate(string $tin): bool
*/
protected function hasValidLength(string $tin): bool
{
return $this->matchLength($this->getTIN(), static::LENGTH);
return $this->matchLength($this->normalizeTin($tin), static::LENGTH);
}

protected function hasValidPattern(string $tin): bool
{
return $this->matchPattern($this->getTIN(), static::PATTERN);
return $this->matchPattern($this->normalizeTin($tin), static::PATTERN);
}

protected function hasValidRule(string $tin): bool
Expand All @@ -141,7 +146,7 @@ protected function hasValidRule(string $tin): bool

protected function matchLength(string $tin, int $length): bool
{
return strlen($tin) === $length;
return strlen($this->normalizeTin($tin)) === $length;
}

protected function matchPattern(string $subject, string $pattern): bool
Expand Down
4 changes: 2 additions & 2 deletions src/CountryHandler/Ireland.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,12 @@ private function isFollowLength2(string $tin): bool

private function isFollowPattern1(): bool
{
return $this->matchPattern($this->getTIN(), self::PATTERN_1);
return $this->matchPattern($this->normalizeTin($this->getTIN()), self::PATTERN_1);
}

private function isFollowPattern2(): bool
{
return $this->matchPattern($this->getTIN(), self::PATTERN_2);
return $this->matchPattern($this->normalizeTin($this->getTIN()), self::PATTERN_2);
}

private function letterToNumber(string $toConv): int
Expand Down
2 changes: 1 addition & 1 deletion src/CountryHandler/Spain.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ final class Spain extends CountryHandler

public function getTIN(): string
{
return str_pad(parent::getTIN(), self::LENGTH, '0', STR_PAD_LEFT);
return str_pad($this->normalizeTin(parent::getTIN()), self::LENGTH, '0', STR_PAD_LEFT);
}

protected function hasValidPattern(string $tin): bool
Expand Down

0 comments on commit b2cbb19

Please sign in to comment.