1
|
|
|
<?php |
2
|
|
|
namespace SimpleCMS\Bank\Packages; |
3
|
|
|
|
4
|
|
|
use Illuminate\Support\Collection; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* 银行卡模块 |
8
|
|
|
* |
9
|
|
|
* @author Dennis Lui <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
class BankModel implements \JsonSerializable |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* 银行名称 |
15
|
|
|
* |
16
|
|
|
* @var string|null |
17
|
|
|
*/ |
18
|
|
|
public ?string $name = null; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* 银行代码 |
22
|
|
|
* |
23
|
|
|
* @var string|null |
24
|
|
|
*/ |
25
|
|
|
public ?string $code = null; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Bin 列表 |
29
|
|
|
* |
30
|
|
|
* @var Collection<BinModel>|null |
31
|
|
|
*/ |
32
|
|
|
public Collection $bins; |
33
|
|
|
|
34
|
|
|
public function __construct(string $name = null, string $code = null, array $bins = []) |
35
|
|
|
{ |
36
|
|
|
$this->name = $name; |
37
|
|
|
$this->code = $code; |
38
|
|
|
$this->bins = collect(); |
39
|
|
|
$this->pushBins($bins); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* 将 Bin 数据推入到 BankModel 中 |
44
|
|
|
* |
45
|
|
|
* @param array $data |
46
|
|
|
* @return void |
47
|
|
|
*/ |
48
|
|
|
protected function pushBins(array $data): void |
49
|
|
|
{ |
50
|
|
|
foreach ($data as $type => $_bins) { |
51
|
|
|
foreach ($_bins as $length => $bin) { |
52
|
|
|
foreach ($bin as $_bin) { |
53
|
|
|
$this->bins->push(new BinModel((string) $_bin, (string) $type, (int) $length)); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* 检查卡号有效性 |
61
|
|
|
* |
62
|
|
|
* @param string $card_number |
63
|
|
|
* @return bool |
64
|
|
|
*/ |
65
|
|
|
public function isValid(string $card_number): bool |
66
|
|
|
{ |
67
|
|
|
return $this->bins->contains(fn(BinModel $bin) => $bin->isValid($card_number)); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
// 下面是获取不同类型的 Bin 列表的方法,每个方法返回对应类型的 Bin 列表 |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* 返回储蓄卡列表 |
74
|
|
|
* |
75
|
|
|
* @return Collection|null |
76
|
|
|
*/ |
77
|
|
|
public function getDC(): ?Collection |
78
|
|
|
{ |
79
|
|
|
return $this->bins ? $this->bins->filter(fn(BinModel $n) => $n->type == 'DC')->values() : null; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* 返回预付费卡列表 |
84
|
|
|
* |
85
|
|
|
* @return Collection|null |
86
|
|
|
*/ |
87
|
|
|
public function getPC(): ?Collection |
88
|
|
|
{ |
89
|
|
|
return $this->bins ? $this->bins->filter(fn(BinModel $n) => $n->type == 'PC')->values() : null; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* 返回信用卡列表 |
94
|
|
|
* |
95
|
|
|
* @return Collection|null |
96
|
|
|
*/ |
97
|
|
|
public function getCC(): ?Collection |
98
|
|
|
{ |
99
|
|
|
return $this->bins ? $this->bins->filter(fn(BinModel $n) => $n->type == 'CC')->values() : null; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* 检查Bin是否存在 |
104
|
|
|
* @param string $bin |
105
|
|
|
* @return bool |
106
|
|
|
*/ |
107
|
|
|
public function hasBin(string $bin): bool |
108
|
|
|
{ |
109
|
|
|
return $this->bins->contains(fn(BinModel $n) => $n->bin === $bin); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* 检查银行是否包含特定的卡号 |
114
|
|
|
* |
115
|
|
|
* @param string $cardNumber 完整卡号 |
116
|
|
|
* @return bool |
117
|
|
|
*/ |
118
|
|
|
public function hasCardNumber(string $cardNumber): bool |
119
|
|
|
{ |
120
|
|
|
return $this->bins->contains(fn(BinModel $n) => strpos($cardNumber, $n->bin) === 0); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* 返回准贷记卡列表 |
125
|
|
|
* |
126
|
|
|
* @return Collection|null |
127
|
|
|
*/ |
128
|
|
|
public function getSCC(): ?Collection |
129
|
|
|
{ |
130
|
|
|
return $this->bins ? $this->bins->filter(fn(BinModel $n) => $n->type == 'SCC')->values() : null; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* 将 BankModel 对象序列化为数组 |
135
|
|
|
* |
136
|
|
|
* @return array<string,array<string,string|int|null>|string|null> |
137
|
|
|
*/ |
138
|
|
|
public function toArray(): array |
139
|
|
|
{ |
140
|
|
|
$data = [ |
141
|
|
|
'name' => $this->name ?? null, |
142
|
|
|
'code' => $this->code ?? null, |
143
|
|
|
'bins' => $this->bins ? $this->bins->toArray() : null |
144
|
|
|
]; |
145
|
|
|
return $data; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* 将 BankModel 对象序列化为 JSON |
150
|
|
|
* |
151
|
|
|
* @return array<array<int|string|null>|string|null> |
152
|
|
|
*/ |
153
|
|
|
public function jsonSerialize() |
154
|
|
|
{ |
155
|
|
|
return $this->toArray(); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* 将 BankModel 对象转换为 JSON 字符串 |
160
|
|
|
* |
161
|
|
|
* @return string |
162
|
|
|
*/ |
163
|
|
|
public function __toString(): string |
164
|
|
|
{ |
165
|
|
|
return json_encode($this->toArray()); |
166
|
|
|
} |
167
|
|
|
} |