From 23eb068c35e6c8950b72b9d541d634b65d3a69ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Tegn=C3=A9r?= Date: Sat, 20 Jan 2024 11:52:39 +0100 Subject: [PATCH] Added isInterimNumber method and tests. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Johannes Tegnér --- src/Personnummer.php | 16 +++++++++++++--- src/PersonnummerInterface.php | 7 +++++++ tests/InterimNumberTest.php | 7 +++++++ tests/PersonnummerTest.php | 9 +++++++++ 4 files changed, 36 insertions(+), 3 deletions(-) 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/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()); + } + } + } }