Skip to content

Commit

Permalink
Merge pull request #13 from rasmusbe/getGender
Browse files Browse the repository at this point in the history
Add methods for identifying sex
  • Loading branch information
Johannestegner authored Sep 25, 2019
2 parents dd179b0 + 2f02fcf commit 3363798
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 1 deletion.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,24 @@ Personnummer::format('6403273813', true);
//=> 196403273813
```

### Get Age
```php
use Frozzare\Personnummer\Personnummer;

Personnummer::getAge(6403273813);
//=> 55
```

### Get Sex
```php
use Frozzare\Personnummer\Personnummer;

Personnummer::isMale(6403273813);
//=> true
Personnummer::isFemale(6403273813);
//=> false
```

See [PersonnummerTest.php](tests/PersonnummerTest.php) for more examples.

## License
Expand Down
35 changes: 35 additions & 0 deletions src/Personnummer.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,41 @@ public static function getAge($ssn, $includeCoordinationNumber = true)
return $d1->diff($d2)->y;
}

/**
* Check if a Swedish social security number is for a male.
*
* @param string|int $ssn
* @param bool $includeCoordinationNumber
*
* @return bool
* @throws PersonnummerException
*/
public static function isMale($ssn, $includeCoordinationNumber = true)
{
if (!self::valid($ssn, $includeCoordinationNumber)) {
throw new PersonnummerException();
}

$parts = self::getParts($ssn);
$genderDigit = substr($parts['num'], -1);

return boolval($genderDigit % 2);
}

/**
* Check if a Swedish social security number is for a female.
*
* @param string|int $ssn
* @param bool $includeCoordinationNumber
*
* @return bool
* @throws PersonnummerException
*/
public static function isFemale($ssn, $includeCoordinationNumber = true)
{
return !static::isMale($ssn, $includeCoordinationNumber);
}

/**
* Parse a Swedish social security number and get the parts.
*
Expand Down
31 changes: 30 additions & 1 deletion tests/PersonnummerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
use PHPUnit_Framework_TestCase;
use VladaHejda\AssertException;

function time() {
function time()
{
return 1565704890;
}

Expand Down Expand Up @@ -143,4 +144,32 @@ public function testExcludeOfCoOrdinationNumbersAge()
Personnummer::getAge('640883-3231', false);
}, PersonnummerException::class);
}

public function testSex()
{
$this->assertTrue(Personnummer::isMale(6403273813, false));
$this->assertFalse(Personnummer::isFemale(6403273813, false));
$this->assertTrue(Personnummer::isFemale('510818-9167', false));
$this->assertFalse(Personnummer::isMale('510818-9167', false));
}

public function testSexWithCoOrdinationNumbers()
{
$this->assertTrue(Personnummer::isMale('701063-2391'));
$this->assertFalse(Personnummer::isFemale('701063-2391'));
$this->assertTrue(Personnummer::isFemale('640883-3223'));
$this->assertFalse(Personnummer::isMale('640883-3223'));
}

public function testSexWithInvalidNumbers()
{
foreach ($this->invalidNumbers as $invalidNumber) {
$this->assertException(function () use ($invalidNumber) {
Personnummer::isMale($invalidNumber);
}, PersonnummerException::class);
$this->assertException(function () use ($invalidNumber) {
Personnummer::isFemale($invalidNumber);
}, PersonnummerException::class);
}
}
}

0 comments on commit 3363798

Please sign in to comment.