Skip to content

Commit

Permalink
Added isInterimNumber method and tests.
Browse files Browse the repository at this point in the history
Signed-off-by: Johannes Tegnér <johannes@jitesoft.com>
  • Loading branch information
Johannestegner committed Jan 20, 2024
1 parent 1d81bbc commit 7a69af3
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 4 deletions.
16 changes: 13 additions & 3 deletions src/Personnummer.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ final class Personnummer implements PersonnummerInterface

private array $options;

private bool $isInterim;

/**
* @inheritDoc
*/
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -256,17 +266,17 @@ 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']
));
}

$num = $parts['num'];
if ($this->options['allowInterimNumber'] && $isInterim) {
if ($this->options['allowInterimNumber'] && $this->isInterim) {
$num = preg_replace($interimTest, '1', $num);
}

Expand Down
7 changes: 7 additions & 0 deletions src/PersonnummerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []);

/**
Expand Down
7 changes: 7 additions & 0 deletions tests/InterimNumberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
2 changes: 1 addition & 1 deletion tests/PersonnummerData.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Personnummer\Tests;

class PersonnummerData
Expand All @@ -14,7 +15,6 @@ public function __construct(array $p)
$this->isMale = $p['isMale'];
$this->isFemale = $p['isFemale'];
}

public string $longFormat;
public string $shortFormat;
public string $separatedFormat;
Expand Down
9 changes: 9 additions & 0 deletions tests/PersonnummerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
}
}

0 comments on commit 7a69af3

Please sign in to comment.