|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the Slince/China package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Slince <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace China\Region; |
|
12
|
|
|
|
|
13
|
|
|
use China\Common\ResourceFile; |
|
14
|
|
|
use China\IDCard\IDCard; |
|
15
|
|
|
use China\Region\Location\AddressInterface; |
|
16
|
|
|
use Doctrine\Common\Collections\Collection; |
|
17
|
|
|
|
|
18
|
|
|
class RegionService implements RegionServiceInterface |
|
19
|
|
|
{ |
|
20
|
|
|
use AddressFinderTrait; |
|
21
|
|
|
/** |
|
22
|
|
|
* @var AddressInterface |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $regions; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* 根据code做索引. |
|
28
|
|
|
* |
|
29
|
|
|
* @var AddressInterface[]|Collection |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $flattenRegions; |
|
32
|
|
|
|
|
33
|
|
|
public function __construct(ResourceFile $resourceFile) |
|
34
|
|
|
{ |
|
35
|
|
|
$this->regions = new RegionLoader($resourceFile); |
|
|
|
|
|
|
36
|
|
|
$this->flattenRegions = new RegionCollection(); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* {@inheritdoc} |
|
41
|
|
|
*/ |
|
42
|
|
|
public function getProvinces() |
|
43
|
|
|
{ |
|
44
|
|
|
return $this->regions->first()->getChildren(); |
|
|
|
|
|
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* {@inheritdoc} |
|
49
|
|
|
*/ |
|
50
|
|
|
public function findByIdCard($idCard) |
|
51
|
|
|
{ |
|
52
|
|
|
if (is_string($idCard)) { |
|
53
|
|
|
$idCard = new IDCard($idCard); |
|
54
|
|
|
} |
|
55
|
|
|
$areaCode = substr($idCard, 0, 6); |
|
56
|
|
|
|
|
57
|
|
|
return $this->findByCode($areaCode); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* {@inheritdoc} |
|
62
|
|
|
*/ |
|
63
|
|
|
public function filter(\Closure $callback) |
|
64
|
|
|
{ |
|
65
|
|
|
if (!$this->flattenRegions->isEmpty()) { |
|
66
|
|
|
return $this->flattenRegions->filter($callback); |
|
67
|
|
|
} |
|
68
|
|
|
// 没有遍历过则自行遍历 |
|
69
|
|
|
$addresses = []; |
|
70
|
|
|
$this->traverseTree($this->regions->first(), function(AddressInterface $address) use ($callback, &$addresses){ |
|
|
|
|
|
|
71
|
|
|
if ($callback($address) === true) { |
|
72
|
|
|
$addresses[] = $address; |
|
73
|
|
|
} |
|
74
|
|
|
}); |
|
75
|
|
|
|
|
76
|
|
|
return new RegionCollection($addresses); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* 遍历地区树. |
|
81
|
|
|
* |
|
82
|
|
|
* @param AddressInterface $address |
|
83
|
|
|
* @param \Closure $callback |
|
84
|
|
|
*/ |
|
85
|
|
|
protected function traverseTree(AddressInterface $address, \Closure $callback) |
|
86
|
|
|
{ |
|
87
|
|
|
$callback($address); |
|
88
|
|
|
//将树形结构扁平化 |
|
89
|
|
|
$this->flattenRegions->add($address); |
|
90
|
|
|
if ($address->getChildren()) { |
|
91
|
|
|
foreach ($address->getChildren() as $child) { |
|
92
|
|
|
$this->traverseTree($child, $callback); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
} |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..