| @@ 14-44 (lines=31) @@ | ||
| 11 | /** |
|
| 12 | * Bankgiro account number factory |
|
| 13 | */ |
|
| 14 | class BankgiroFactory implements AccountFactoryInterface |
|
| 15 | { |
|
| 16 | /** |
|
| 17 | * @var ValidatorInterface |
|
| 18 | */ |
|
| 19 | private $validator; |
|
| 20 | ||
| 21 | public function __construct() |
|
| 22 | { |
|
| 23 | $this->validator = new CheckDigitType2Validator; |
|
| 24 | } |
|
| 25 | ||
| 26 | public function createAccount(string $number): AccountNumber |
|
| 27 | { |
|
| 28 | if (!preg_match('/^0*(\d{3,4})-?(\d{3})(\d)$/', $number, $matches)) { |
|
| 29 | throw new InvalidAccountNumberException("Invalid bankgiro account number structure"); |
|
| 30 | } |
|
| 31 | ||
| 32 | $account = new Bankgiro($number, $matches[1].$matches[2], $matches[3]); |
|
| 33 | ||
| 34 | $result = $this->validator->validate($account); |
|
| 35 | ||
| 36 | if (!$result->isValid()) { |
|
| 37 | throw new InvalidAccountNumberException( |
|
| 38 | "Unable to parse bankgiro $number: {$result->getMessage()}" |
|
| 39 | ); |
|
| 40 | } |
|
| 41 | ||
| 42 | return $account; |
|
| 43 | } |
|
| 44 | } |
|
| 45 | ||
| @@ 14-44 (lines=31) @@ | ||
| 11 | /** |
|
| 12 | * Plusgiro account number factory |
|
| 13 | */ |
|
| 14 | class PlusgiroFactory implements AccountFactoryInterface |
|
| 15 | { |
|
| 16 | /** |
|
| 17 | * @var ValidatorInterface |
|
| 18 | */ |
|
| 19 | private $validator; |
|
| 20 | ||
| 21 | public function __construct() |
|
| 22 | { |
|
| 23 | $this->validator = new CheckDigitType2Validator; |
|
| 24 | } |
|
| 25 | ||
| 26 | public function createAccount(string $number): AccountNumber |
|
| 27 | { |
|
| 28 | if (!preg_match('/^0*(\d{1,7})-?(\d)$/', $number, $matches)) { |
|
| 29 | throw new InvalidAccountNumberException("Invalid PlusGiro account number structure"); |
|
| 30 | } |
|
| 31 | ||
| 32 | $account = new PlusGiro($number, $matches[1], $matches[2]); |
|
| 33 | ||
| 34 | $result = $this->validator->validate($account); |
|
| 35 | ||
| 36 | if (!$result->isValid()) { |
|
| 37 | throw new InvalidAccountNumberException( |
|
| 38 | "Unable to parse PlusGiro $number: {$result->getMessage()}" |
|
| 39 | ); |
|
| 40 | } |
|
| 41 | ||
| 42 | return $account; |
|
| 43 | } |
|
| 44 | } |
|
| 45 | ||