BinModel::jsonSerialize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 1
eloc 1
c 2
b 1
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
namespace SimpleCMS\Bank\Packages;
3
4
/**
5
 * Bin模型
6
 */
7
class BinModel implements \JsonSerializable
8
{
9
    /**
10
     * Bin码
11
     * @var string
12
     */
13
    public string $bin;
14
15
    /**
16
     * 卡号长度
17
     * @var int
18
     */
19
    public int $length = 0;
20
21
    /**
22
     * 卡类型
23
     * @var string<'DC','PC','CC','SCC'>
24
     */
25
    public string $type = 'DC';
26
27
    public function __construct(string $bin, string $type, int $length)
28
    {
29
        $this->bin = $bin;
30
        $this->type = $type;
31
        $this->length = $length;
32
    }
33
34
    public function toArray(): array
35
    {
36
        return [
37
            'bin' => $this->bin,
38
            'length' => $this->length,
39
            'type' => $this->type
40
        ];
41
    }
42
43
    /**
44
     * 检查有效性
45
     * @param string $card_number
46
     * @return bool
47
     */
48
    public function isValid(string $card_number): bool
49
    {
50
        if (strpos($card_number, $this->bin) !== 0) {
51
            return false;
52
        }
53
        if (strlen($card_number) !== $this->length) {
54
            return false;
55
        }
56
        return true;
57
    }
58
59
    /**
60
     * jsonSerialize
61
     * @return array<array<int|string|null>|string|null>
62
     */
63
    public function jsonSerialize()
64
    {
65
        return get_object_vars($this);
66
    }
67
68
    /**
69
     * __toString
70
     * @return string
71
     */
72
    public function __toString(): string
73
    {
74
        return json_encode($this);
75
    }
76
}