Passed
Push — master ( 4dd018...10b845 )
by du
02:24
created

NullDataMapper::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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 3
    public function setInfo(array $info): DataMapImp
29
    {
30 3
        $this->info = $info;
31
32 3
        return $this;
33
    }
34
35
    /**
36
     * @return string
37
     *
38
     * @author duc <[email protected]>
39
     */
40
    public function getIp(): string
41
    {
42
        return $this->info['ip'];
43
    }
44
45
    /**
46
     * @return null
47
     *
48
     * @author duc <[email protected]>
49
     */
50 3
    public function getCity()
51
    {
52 3
        return null;
53
    }
54
55
    /**
56
     * @return null
57
     *
58
     * @author duc <[email protected]>
59
     */
60
    public function getCountry()
61
    {
62
        return null;
63
    }
64
65
    /**
66
     * @return null
67
     *
68
     * @author duc <[email protected]>
69
     */
70
    public function getRegion()
71
    {
72
        return null;
73
    }
74
75
    /**
76
     * @return null
77
     *
78
     * @author duc <[email protected]>
79
     */
80 1
    public function getAddress()
81
    {
82 1
        return null;
83
    }
84
85
    /**
86
     * @param $name
87
     *
88
     * @return mixed|string
89
     *
90
     * @author duc <[email protected]>
91
     */
92
    public function __get(string $name)
93
    {
94
        $field = toUnderScore($name);
95
96
        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 1
    public function __call(string $name, $arguments)
109
    {
110 1
        preg_match('/get(.*)/', $name, $matches, PREG_OFFSET_CAPTURE);
111
112 1
        if (count($matches) >= 2 && strlen($name) > 3) {
113 1
            $field = toUnderScore($matches[1][0]);
114
115 1
            return isset($this->info[$field]) ? $this->info[$field] : null;
116
        }
117
118
        if (! method_exists($this, $name)) {
119
            throw new MethodNotExistException("{$name} 方法不存在");
120
        }
121
    }
122
123
    /**
124
     * @return bool
125
     *
126
     * @author duc <[email protected]>
127
     */
128
    public function hasInfo(): bool
129
    {
130
        return false;
131
    }
132
}
133