Test Setup Failed
Push — master ( fdb2e5...af3ec1 )
by Dennis
03:42
created

RegionModel::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
namespace SimpleCMS\Region\Packages;
3
4
use function is_array;
5
use Illuminate\Support\Collection;
6
7
/**
8
 * 地理模块
9
 *
10
 * @author Dennis Lui <[email protected]>
11
 */
12
class RegionModel extends RegionStatic implements \JsonSerializable
13
{
14
    /**
15
     * 下级
16
     *
17
     * @var Collection<RegionModel>
18
     */
19
    public Collection $children;
20
21
    /**
22
     * 上级
23
     * @var RegionModel|null
24
     */
25
    public ?RegionModel $parent = null;
26
27
    public function __construct()
28
    {
29
        $this->children = new Collection();
30
    }
31
32
    /**
33
     * 设置模型
34
     * @param array $data
35
     * @param ?RegionStatic $parent
36
     * @return void
37
     */
38
    public function setData(array $data, ?RegionStatic $parent = null): void
39
    {
40
        if ($this->deep < 5) {
41
            $this->initData($data, $parent);
42
        }
43
    }
44
45
    /**
46
     * 初始化数据
47
     * @param array $data
48
     * @param mixed $parent
49
     * @return void
50
     */
51
    protected function initData(array $data, ?RegionStatic $parent = null)
52
    {
53
        $imp = [
54
            'name' => null,
55
            'short' => null,
56
            'code' => null,
57
            'area' => null,
58
            'zip' => null,
59
            'lng' => 0,
60
            'lat' => 0,
61
            'children' => []
62
        ];
63
        $data = array_merge($imp, $data);
64
        foreach (array_keys($imp) as $keyName) {
65
            $this->setValue($keyName, $data[$keyName]);
66
        }
67
        $this->parent = $parent;
68
        if ($this->deep < 4 && is_array($data['children'])) {
69
            foreach ($data['children'] as $child) {
70
                $_child = new static();
71
                $_child->setDeep($this->deep + 1);
72
                $_child->setData($child, $this->cloneRegion());
73
                $this->children->push($_child);
74
            }
75
        }
76
    }
77
78
    /**
79
     * 复制
80
     * @return RegionStatic
81
     */
82
    protected function cloneRegion(): RegionStatic
83
    {
84
        $self = new static();
85
        $self->name = $this->name;
86
        $self->short = $this->short;
87
        $self->code = $this->code;
88
        $self->area = $this->area;
89
        $self->zip = $this->zip;
90
        $self->lng = $this->lng;
91
        $self->lat = $this->lat;
92
        return $self;
93
    }
94
95
    protected function setValue(string $keyName, $value): void
96
    {
97
        switch ($keyName) {
98
            case 'name':
99
                $this->name = (string) $value;
100
                break;
101
            case 'short':
102
                $this->short = (string) $value;
103
                break;
104
            case 'code':
105
                $this->code = (string) $value;
106
                break;
107
            case 'area':
108
                $this->area = (string) $value;
109
                break;
110
            case 'zip':
111
                $this->zip = (string) $value;
112
                break;
113
            case 'lng':
114
                $this->lng = (float) $value;
115
                break;
116
            case 'lat':
117
                $this->lat = (float) $value;
118
                break;
119
        }
120
    }
121
122
    /**
123
     * toArray
124
     * @return array<string,null|string|float|int|array<string,null|string|float|int|mixed>>
125
     */
126
    public function toArray(): array
127
    {
128
        $data = [
129
            'name' => $this->name ?? null,
130
            'short' => $this->short ?? null,
131
            'code' => $this->code ?? 0,
132
            'area' => $this->area ?? 0,
133
            'zip' => $this->zip ?? 0,
134
            'lng' => $this->lng ?? 0,
135
            'lat' => $this->lat ?? 0,
136
            'children' => $this->children->toArray()
137
        ];
138
139
        return $data;
140
    }
141
142
    /**
143
     * 检查名称有效性
144
     *
145
     * @param string $name
146
     * @return bool
147
     */
148
    public function checkName(string $name): bool
149
    {
150
        return $this->name == $name || $this->short == $name;
151
    }
152
153
    /**
154
     * 检查代码有效性
155
     *
156
     * @param string $code
157
     * @return bool
158
     */
159
    public function checkCode(string $code): bool
160
    {
161
        return $this->code == $code;
162
    }
163
164
    /**
165
     * 检查区号有效性
166
     *
167
     * @param string $area
168
     * @return bool
169
     */
170
    public function checkArea(string $area): bool
171
    {
172
        return $this->area == $area;
173
    }
174
175
    /**
176
     * 检查电话号码有效性
177
     *
178
     * @param string $number
179
     * @return bool
180
     */
181
    public function checkNumber(string $number): bool
182
    {
183
        if (strlen($number) < 10)
184
            return false;
185
        $area = substr($number, 0, strlen($this->area));
186
        return $this->area == $area;
187
    }
188
189
    /**
190
     * 检查邮编有效性
191
     *
192
     * @param string $zip
193
     * @return bool
194
     */
195
    public function checkZip(string $zip): bool
196
    {
197
        return substr($this->zip, 0, 5) == substr($zip, 0, 5);
198
    }
199
200
    /**
201
     * jsonSerialize
202
     * @return mixed
203
     */
204
    public function jsonSerialize()
205
    {
206
        return $this->toArray();
207
    }
208
209
    /**
210
     * __toString
211
     * @return string
212
     */
213
    public function __toString(): string
214
    {
215
        return json_encode($this->toArray());
216
    }
217
218
    /**
219
     * 获取上级
220
     * @return RegionStatic|null
221
     */
222
    public function getParent(): ?RegionStatic
223
    {
224
        return $this->parent;
225
    }
226
}