Skip to content

Commit

Permalink
GetDate function (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
Johannestegner authored Jan 21, 2024
1 parent d2cc664 commit 8c9453c
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 10 deletions.
19 changes: 10 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ composer require personnummer/personnummer
| valid | string personnummer, [ array options<sup>[*](#options)</sup> ] | bool |

#### Instance
| Method | Arguments | Returns |
|----------------------|:----------------|--------:|
| format | bool longFormat | string |
| getAge | none | int |
| isMale | none | bool |
| isFemale | none | bool |
| isCoordinationNumber | none | bool |
| isInterimNumber | none | bool |
| Method | Arguments | Returns |
|----------------------|:----------------|---------:|
| format | bool longFormat | string |
| getAge | none | int |
| isMale | none | bool |
| isFemale | none | bool |
| isCoordinationNumber | none | bool |
| isInterimNumber | none | bool |
| getDate | none | DateTime |

| Property | Type | Description |
| ---------|:-------|----------------------------:|
Expand All @@ -43,7 +44,7 @@ When a personnummer is invalid a PersonnummerException is thrown.
| Option | Type | Default | Description |
|-------------------------|:-----|:--------|:---------------------------:|
| allowCoordinationNumber | bool | true | Accept coordination numbers |
| allowInterimNumber | bool | false | Accept interim/T numbers |
| allowInterimNumber | bool | false | Accept interim/T numbers |

## Examples

Expand Down
10 changes: 9 additions & 1 deletion src/Personnummer.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ private static function getParts(string $ssn): array
}

$parts['fullYear'] = $parts['century'] . $parts['year'];

$parts['realDay'] = $parts['day'] > 60 ? $parts['day'] - 60 : $parts['day'];
$parts['original'] = $ssn;
return $parts;
}
Expand Down Expand Up @@ -230,6 +230,14 @@ public function getAge(): int
return (new DateTime())->diff($birthday)->y;
}

public function getDate(): DateTime
{
return DateTime::createFromFormat(
'Ymd',
$this->parts['fullYear'] . $this->parts['month'] . $this->parts['realDay']
);
}

public function __get(string $name)
{
if (isset($this->parts[$name])) {
Expand Down
8 changes: 8 additions & 0 deletions src/PersonnummerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Personnummer;

use DateTime;
use Exception;

/**
Expand Down Expand Up @@ -89,4 +90,11 @@ public function __construct(string $ssn, array $options = []);
* @throws Exception When date is invalid or problems with DateTime library
*/
public function getAge(): int;

/**
* Get the date part of the personnummer as a DateTime object.
*
* @return DateTime
*/
public function getDate(): DateTime;
}
25 changes: 25 additions & 0 deletions tests/PersonnummerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,4 +236,29 @@ public function testIsNotInterim(): void
}
}
}

public function testDate(): void
{
foreach (self::$testdataList as $testdata) {
if (!$testdata['valid']) {
continue;
}

$val = $testdata['long_format'];
if ($testdata['type'] === 'con') {
// If it's a coordination number, we aught to remove 60 from the "real day" to make sure
// we get the correct date.
$val = substr($testdata['long_format'], 0, 6); // YYMM
$val .= (int)substr($testdata['long_format'], 6, 2) - 60; // DD
}

$pn = Personnummer::parse($testdata['separated_format']);
$date = DateTime::createFromFormat('Ymd', substr($val, 0, 8)); // Only want YYYYMMDD

self::assertSame(
$pn->getDate()->format("Ymd"),
$date->format('Ymd')
);
}
}
}

0 comments on commit 8c9453c

Please sign in to comment.