BankCard::getFromArray()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 3
1
<?php
2
3
/*
4
 * This file is part of questocat/bank-card package.
5
 *
6
 * (c) questocat <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Questocat\BankCard;
13
14
class BankCard
15
{
16
    use LuhnTrait;
17
18
    const ALIPAY_GET_CARD_INFO = 'https://ccdcapi.alipay.com/validateAndCacheCardInfo.json';
19
20
    const ALIPAY_GET_CARD_LOGO = 'https://apimg.alipay.com/combo.png';
21
22
    /**
23
     * The card number.
24
     *
25
     * @var string
26
     */
27
    protected $cardNumber;
28
29
    /**
30
     * @var array
31
     */
32
    protected $cardType = [
33
        'CC' => '信用卡',
34
        'DC' => '储蓄卡',
35
        'SCC' => '准贷记卡',
36
        'PC' => '预付费卡',
37
    ];
38
39
    /**
40
     * BankCard construct.
41
     *
42
     * @param string $cardNumber
43
     */
44
    public function __construct($cardNumber)
45
    {
46
        $this->cardNumber = $cardNumber;
47
    }
48
49
    /**
50
     * Get the bank card info.
51
     *
52
     * @return array
53
     */
54
    public function info()
55
    {
56
        $query = [
57
            'cardNo' => $this->cardNumber,
58
            '_input_charset' => 'utf-8',
59
            'cardBinCheck' => 'true',
60
        ];
61
62
        $url = self::ALIPAY_GET_CARD_INFO.'?'.http_build_query($query);
63
        $contents = file_get_contents($url);
64
        $result = json_decode($contents, true);
65
66
        if (!$result['validated']) {
67
            return [
68
                'validated' => false,
69
            ];
70
        }
71
72
        return [
73
            'bank_name' => $this->getBankName($result['bank']),
74
            'short_code' => $result['bank'],
75
            'card_type_name' => $this->getCardType($result['cardType']),
76
            'card_type' => $result['cardType'],
77
            'logo' => $this->logo($result['bank']),
78
            'length' => strlen($this->cardNumber),
79
            'luhn' => $this->luhn()->verify($this->cardNumber),
80
            'validated' => true,
81
        ];
82
    }
83
84
    /**
85
     * Get the logo.
86
     *
87
     * @param string $shortCode
88
     *
89
     * @return string
90
     */
91
    public function logo($shortCode)
92
    {
93
        return self::ALIPAY_GET_CARD_LOGO."?d=cashier&t={$shortCode}";
94
    }
95
96
    /**
97
     * Get the bank name.
98
     *
99
     * @param string $shortCode
100
     *
101
     * @return string
102
     */
103
    protected function getBankName($shortCode)
104
    {
105
        $banks = include dirname(__DIR__).'/resources/Banks.php';
106
107
        return $this->getFromArray($banks, $shortCode);
108
    }
109
110
    /**
111
     * Get the card type.
112
     *
113
     * @param string $code
114
     *
115
     * @return string
116
     */
117
    protected function getCardType($code)
118
    {
119
        return $this->getFromArray($this->cardType, $code);
120
    }
121
122
    /**
123
     * Get an item from an array.
124
     *
125
     * @param array  $array
126
     * @param string $key
127
     * @param mixed  $default
128
     *
129
     * @return mixed
130
     */
131
    protected function getFromArray($array, $key, $default = null)
132
    {
133
        if (array_key_exists($key, $array)) {
134
            return $array[$key];
135
        }
136
137
        return $default;
138
    }
139
}
140