diff --git a/src/Personnummer.php b/src/Personnummer.php index 1d28d32..015001b 100644 --- a/src/Personnummer.php +++ b/src/Personnummer.php @@ -25,6 +25,8 @@ final class Personnummer implements PersonnummerInterface private array $options; + private bool $isInterim; + /** * @inheritDoc */ @@ -88,6 +90,14 @@ public function isCoordinationNumber(): bool return checkdate((int)$parts['month'], $parts['day'] - 60, $parts['fullYear']); } + /** + * @inheritDoc + */ + public function isInterimNumber(): bool + { + return $this->isInterim; + } + /** * @inheritDoc */ @@ -256,9 +266,9 @@ private function isValid(): bool // Correct interim if allowed. $interimTest = '/(?![-+])\D/'; - $isInterim = preg_match($interimTest, $parts['original']) !== 0; + $this->isInterim = preg_match($interimTest, $parts['original']) !== 0; - if ($this->options['allowInterimNumber'] === false && $isInterim) { + if ($this->options['allowInterimNumber'] === false && $this->isInterim) { throw new PersonnummerException(sprintf( '%s contains non-integer characters and options are set to not allow interim numbers', $parts['original'] @@ -266,7 +276,7 @@ private function isValid(): bool } $num = $parts['num']; - if ($this->options['allowInterimNumber'] && $isInterim) { + if ($this->options['allowInterimNumber'] && $this->isInterim) { $num = preg_replace($interimTest, '1', $num); } diff --git a/src/PersonnummerInterface.php b/src/PersonnummerInterface.php index c41a3d6..cc87151 100644 --- a/src/PersonnummerInterface.php +++ b/src/PersonnummerInterface.php @@ -72,6 +72,13 @@ public function isMale(): bool; */ public function isCoordinationNumber(): bool; + /** + * Check if the Swedish social security number is an interim number. + * + * @return bool + */ + public function isInterimNumber(): bool; + public function __construct(string $ssn, array $options = []); /** diff --git a/tests/InterimNumberTest.php b/tests/InterimNumberTest.php index 9da74db..b1489f5 100644 --- a/tests/InterimNumberTest.php +++ b/tests/InterimNumberTest.php @@ -60,6 +60,13 @@ public function testValidateInvalidInterim(PersonnummerData $num): void self::assertFalse(Personnummer::valid($num->separatedFormat, $this->options)); } + #[DataProvider('validProvider')] + public function testIsInterim(PersonnummerData $num): void + { + self::assertTrue(Personnummer::parse($num->longFormat, $this->options)->isInterimNumber()); + self::assertTrue(Personnummer::parse($num->separatedFormat, $this->options)->isInterimNumber()); + } + #[DataProvider('validProvider')] public function testFormatLongInterim(PersonnummerData $num): void { diff --git a/tests/PersonnummerData.php b/tests/PersonnummerData.php index a00b0f1..5f3f79e 100644 --- a/tests/PersonnummerData.php +++ b/tests/PersonnummerData.php @@ -1,4 +1,5 @@ isMale = $p['isMale']; $this->isFemale = $p['isFemale']; } - public string $longFormat; public string $shortFormat; public string $separatedFormat; diff --git a/tests/PersonnummerTest.php b/tests/PersonnummerTest.php index 29fcba3..a9824e8 100644 --- a/tests/PersonnummerTest.php +++ b/tests/PersonnummerTest.php @@ -227,4 +227,13 @@ public function testMissingProperties(): void }, E_USER_NOTICE); $this->assertFalse(isset(Personnummer::parse('121212-1212')->missingProperty)); } + + public function testIsNotInterim(): void + { + foreach (self::$testdataList as $testdata) { + if ($testdata['valid']) { + $this->assertFalse(Personnummer::parse($testdata['separated_format'])->isInterimNumber()); + } + } + } }