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

DataMapper::__call()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

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