Passed
Push — master ( 10b845...c93f04 )
by du
01:52
created

NullDataMapper::__call()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0
cc 4
nc 3
nop 2
crap 4
1
<?php
2
3
namespace DucCnzj\Ip;
4
5
use DucCnzj\Ip\Imp\DataMapImp;
6
use DucCnzj\Ip\Exceptions\MethodNotExistException;
7
8
/**
9
 *
10
 * Class NullDataMapper
11
 *
12
 * @package DucCnzj\Ip
13
 */
14
class NullDataMapper implements DataMapImp
15
{
16
    /**
17
     * @var array
18
     */
19
    protected $info = [];
20
21
    /**
22
     * @param array $info
23
     *
24
     * @return DataMapImp
25
     *
26
     * @author duc <[email protected]>
27
     */
28 5
    public function setInfo(array $info): DataMapImp
29
    {
30 5
        $this->info = $info;
31
32 5
        return $this;
33
    }
34
35
    /**
36
     * @return string|null
37
     *
38
     * @author duc <[email protected]>
39
     */
40 1
    public function getIp()
41
    {
42 1
        return isset($this->info['ip']) ? $this->info['ip'] : null;
43
    }
44
45
    /**
46
     * @return null
47
     *
48
     * @author duc <[email protected]>
49
     */
50 4
    public function getCity()
51
    {
52 4
        return null;
53
    }
54
55
    /**
56
     * @return null
57
     *
58
     * @author duc <[email protected]>
59
     */
60 1
    public function getCountry()
61
    {
62 1
        return null;
63
    }
64
65
    /**
66
     * @return null
67
     *
68
     * @author duc <[email protected]>
69
     */
70 1
    public function getRegion()
71
    {
72 1
        return null;
73
    }
74
75
    /**
76
     * @return null
77
     *
78
     * @author duc <[email protected]>
79
     */
80 2
    public function getAddress()
81
    {
82 2
        return null;
83
    }
84
85
    /**
86
     * @param $name
87
     *
88
     * @return mixed|string
89
     *
90
     * @author duc <[email protected]>
91
     */
92 1
    public function __get(string $name)
93
    {
94 1
        $field = toUnderScore($name);
95
96 1
        return isset($this->info[$field]) ? $this->info[$field] : null;
97
    }
98
99
    /**
100
     * @param string $name
101
     * @param mixed $arguments
102
     *
103
     * @return mixed|string
104
     * @throws MethodNotExistException
105
     *
106
     * @author duc <[email protected]>
107
     */
108 3
    public function __call(string $name, $arguments)
109
    {
110 3
        preg_match('/get(.*)/', $name, $matches, PREG_OFFSET_CAPTURE);
111
112 3
        if (count($matches) >= 2 && strlen($name) > 3) {
113 2
            $field = toUnderScore($matches[1][0]);
114
115 2
            return isset($this->info[$field]) ? $this->info[$field] : null;
116
        }
117
118 1
        throw new MethodNotExistException("{$name} 方法不存在");
119
    }
120
121
    /**
122
     * @return bool
123
     *
124
     * @author duc <[email protected]>
125
     */
126 1
    public function hasInfo(): bool
127
    {
128 1
        return false;
129
    }
130
}
131