Total Complexity | 8 |
Total Lines | 36 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
10 | class ChildrenRecursively |
||
11 | { |
||
12 | protected Collection $regions; |
||
13 | |||
14 | public function __construct(RegionModel $region, string $code, int $deep = 0) |
||
15 | { |
||
16 | $this->regions = new Collection(); |
||
17 | $this->loadRecursively($region, $code, $deep); |
||
18 | } |
||
19 | |||
20 | protected function loadRecursively(RegionModel $region, string $code, int $deep = 0) |
||
21 | { |
||
22 | if ($region->code === $code) { |
||
23 | $this->regions = $region->children; |
||
24 | if ($deep > 0) { |
||
25 | foreach ($region->children as $child) { |
||
26 | $this->regions = $this->regions->merge($this->loadRecursively($child, $code, $deep - 1)); |
||
27 | } |
||
28 | } |
||
29 | } else { |
||
30 | foreach ($region->children as $child) { |
||
31 | $this->regions = $this->loadRecursively($child, $code, $deep); |
||
32 | if ($this->regions->isNotEmpty()) { |
||
33 | break; |
||
34 | } |
||
35 | } |
||
36 | } |
||
37 | } |
||
38 | |||
39 | /** |
||
40 | * 获取列表 |
||
41 | * @return \Illuminate\Support\Collection |
||
42 | */ |
||
43 | public function getRegions(): Collection |
||
46 | } |
||
47 | } |