| Total Complexity | 8 |
| Total Lines | 44 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 1 | 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 = $this->loadRecursively($region, $code, $deep); |
||
| 17 | } |
||
| 18 | |||
| 19 | /** |
||
| 20 | * 加载封装 |
||
| 21 | * @param \SimpleCMS\Region\Packages\RegionModel $region |
||
| 22 | * @param string $code |
||
| 23 | * @param int $deep |
||
| 24 | * @return \Illuminate\Support\Collection |
||
| 25 | */ |
||
| 26 | protected function loadRecursively(RegionModel $region, string $code, int $deep = 0):Collection |
||
| 27 | { |
||
| 28 | $regions = new Collection(); |
||
| 29 | if ($region->code === $code) { |
||
| 30 | $regions = $region->children; |
||
| 31 | if ($deep > 0) { |
||
| 32 | foreach ($region->children as $child) { |
||
| 33 | $regions = $regions->merge($this->loadRecursively($child, $code, $deep - 1)); |
||
| 34 | } |
||
| 35 | } |
||
| 36 | } else { |
||
| 37 | foreach ($region->children as $child) { |
||
| 38 | $regions = $this->loadRecursively($child, $code, $deep); |
||
| 39 | if ($regions->isNotEmpty()) { |
||
| 40 | break; |
||
| 41 | } |
||
| 42 | } |
||
| 43 | } |
||
| 44 | return $regions; |
||
| 45 | } |
||
| 46 | |||
| 47 | /** |
||
| 48 | * 获取列表 |
||
| 49 | * @return \Illuminate\Support\Collection |
||
| 50 | */ |
||
| 51 | public function getRegions(): Collection |
||
| 54 | } |
||
| 55 | } |