|
1
|
|
|
<?php |
|
2
|
|
|
namespace SimpleCMS\Region\Packages; |
|
3
|
|
|
|
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* 地理信息平铺 |
|
9
|
|
|
*/ |
|
10
|
|
|
class FlattenRegion |
|
11
|
|
|
{ |
|
12
|
|
|
protected Collection $regions; |
|
13
|
|
|
|
|
14
|
|
|
public function __construct(Collection $regions) |
|
15
|
|
|
{ |
|
16
|
|
|
$this->regions = new Collection(); |
|
17
|
|
|
$this->setFlatten($regions); |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
protected function setFlatten(Collection $regions) |
|
21
|
|
|
{ |
|
22
|
|
|
$regions->each(function (RegionModel $region) { |
|
23
|
|
|
$this->regions->add($this->flatten($region)); |
|
24
|
|
|
}); |
|
25
|
|
|
$this->regions = $this->regions->flatten(5); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* 平铺数组 |
|
30
|
|
|
* |
|
31
|
|
|
* @author Dennis Lui <[email protected]> |
|
32
|
|
|
* @return array<int,RegionModel> |
|
33
|
|
|
* |
|
34
|
|
|
*/ |
|
35
|
|
|
protected function flatten(RegionModel $region): array |
|
36
|
|
|
{ |
|
37
|
|
|
$children = $region->children; |
|
38
|
|
|
$region->children = new Collection(); |
|
39
|
|
|
$newArray = [ |
|
40
|
|
|
$region |
|
41
|
|
|
]; |
|
42
|
|
|
if ($children->count()) { |
|
43
|
|
|
foreach ($children as $child) { |
|
44
|
|
|
$newArray[] = $this->flatten($child); |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
return $newArray; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Determine if an item exists in the collection. |
|
52
|
|
|
* |
|
53
|
|
|
* @param mixed $key |
|
54
|
|
|
* @param mixed $operator |
|
55
|
|
|
* @param mixed $value |
|
56
|
|
|
* @return bool |
|
57
|
|
|
*/ |
|
58
|
|
|
public function contains($key, $operator = null, $value = null) |
|
59
|
|
|
{ |
|
60
|
|
|
return $this->regions->contains($key, $operator, $value); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Get the first item from the collection passing the given truth test. |
|
65
|
|
|
* |
|
66
|
|
|
* @param ?callable $callback |
|
67
|
|
|
* @param ?mixed $default |
|
68
|
|
|
* @return mixed |
|
69
|
|
|
*/ |
|
70
|
|
|
public function first(?callable $callback = null, $default = null) |
|
71
|
|
|
{ |
|
72
|
|
|
return $this->regions->first($callback, $default); |
|
73
|
|
|
} |
|
74
|
|
|
} |