Completed
Pull Request — master (#18)
by Hannes
06:53
created

PersonalIdDateValidator::validate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 1
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
ccs 7
cts 7
cp 1
crap 2
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace byrokrat\banking\Validator;
6
7
use byrokrat\banking\AccountNumber;
8
9
/**
10
 * Validate that account numbers contains dates as in swedish personal ids
11
 */
12
class PersonalIdDateValidator implements ValidatorInterface
13
{
14 61
    public function validate(AccountNumber $number): ResultInterface
15
    {
16 61
        $day = (int)substr($number->getSerialNumber(), -5, 2);
17 61
        $month = (int)substr($number->getSerialNumber(), -7, 2);
18 61
        $year = (int)substr($number->getSerialNumber(), 0, -7);
19
20 61
        if (!checkdate($month, $day, $year)) {
21 54
            return new Failure("Account number is not a valid personal id number");
22
        }
23
24 23
        return new Success("Valid personal id number");
25
    }
26
}
27