|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace h4kuna\Fio\Account; |
|
4
|
|
|
|
|
5
|
|
|
use h4kuna\Fio\Exceptions\InvalidArgument; |
|
6
|
|
|
|
|
7
|
|
|
class Bank |
|
8
|
|
|
{ |
|
9
|
|
|
|
|
10
|
|
|
/** @var string */ |
|
11
|
|
|
private $account; |
|
12
|
|
|
|
|
13
|
|
|
/** @var string */ |
|
14
|
|
|
private $bankCode = ''; |
|
15
|
|
|
|
|
16
|
|
|
/** @var string */ |
|
17
|
|
|
private $prefix = ''; |
|
18
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
private function __construct(string $account, string $bankCode, string $prefix) |
|
21
|
|
|
{ |
|
22
|
|
|
$this->account = $account; |
|
23
|
|
|
$this->bankCode = $bankCode; |
|
24
|
|
|
$this->prefix = $prefix; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
public function getAccount(): string |
|
29
|
|
|
{ |
|
30
|
|
|
return $this->prefix() . $this->account; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
public function getBankCode(): string |
|
35
|
|
|
{ |
|
36
|
|
|
return $this->bankCode; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
public function getPrefix(): string |
|
41
|
|
|
{ |
|
42
|
|
|
return $this->prefix; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
public function getAccountAndCode(): string |
|
47
|
|
|
{ |
|
48
|
|
|
return $this->getAccount() . $this->bankCode(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
public function __toString() |
|
53
|
|
|
{ |
|
54
|
|
|
return (string) $this->getAccount(); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
private function bankCode(): string |
|
59
|
|
|
{ |
|
60
|
|
|
if ($this->bankCode !== '') { |
|
61
|
|
|
return '/' . $this->bankCode; |
|
62
|
|
|
} |
|
63
|
|
|
return $this->bankCode; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
|
|
67
|
|
|
private function prefix(): string |
|
68
|
|
|
{ |
|
69
|
|
|
if ($this->prefix !== '') { |
|
70
|
|
|
return $this->prefix . '-'; |
|
71
|
|
|
} |
|
72
|
|
|
return $this->prefix; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
|
|
76
|
|
|
public static function createInternational(string $account): self |
|
77
|
|
|
{ |
|
78
|
|
|
if (!preg_match('~^(?P<account>[a-z0-9]{1,34})(?P<code>/[a-z0-9]{11})?$~i', $account, $find)) { |
|
79
|
|
|
throw new InvalidArgument('Account must have format account[/code].'); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
$bankCode = ''; |
|
83
|
|
|
if (isset($find['code']) && $find['code'] !== '') { |
|
84
|
|
|
$bankCode = substr($find['code'], 1); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return new self($find['account'], $bankCode, ''); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
public static function createNational(string $account): self |
|
92
|
|
|
{ |
|
93
|
|
|
if (!preg_match('~^(?P<prefix>\d+-)?(?P<account>\d+)(?P<code>/\d+)?$~', $account, $find)) { |
|
94
|
|
|
throw new InvalidArgument('Account must have format [prefix-]account[/code].'); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
if (strlen($find['account']) > 16) { |
|
98
|
|
|
throw new InvalidArgument('Account max length is 16 chars.'); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
$account = $find['account']; |
|
102
|
|
|
$prefix = $bankCode = ''; |
|
103
|
|
|
if (isset($find['code']) && $find['code'] !== '') { |
|
104
|
|
|
$bankCode = substr($find['code'], 1); |
|
105
|
|
|
if (strlen($bankCode) !== 4) { |
|
106
|
|
|
throw new InvalidArgument('Code must have 4 chars length.'); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
if (isset($find['prefix']) && $find['prefix'] !== '') { |
|
111
|
|
|
$prefix = substr($find['prefix'], 0, -1); |
|
112
|
|
|
} |
|
113
|
|
|
return new self($account, $bankCode, $prefix); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
} |
|
117
|
|
|
|