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

DataMapper::hasInfo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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
    public function hasInfo(): bool
91
    {
92
        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 mixed $arguments
112
     *
113
     * @return mixed|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
        if (! method_exists($this, $name)) {
129 1
            throw new MethodNotExistException("{$name} 方法不存在");
130
        }
131
    }
132
133
    /**
134
     * @param string $field
135
     *
136
     * @return string
137
     *
138
     * @author duc <[email protected]>
139
     */
140 3
    public function getField(string $field):string
141
    {
142 3
        return isset($this->info[$field]) ? $this->info[$field] : '';
143
    }
144
}
145