ChildrenRecursively::loadRecursively()   A
last analyzed

Complexity

Conditions 6
Paths 5

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 19
rs 9.2222
cc 6
nc 5
nop 3
1
<?php
2
namespace SimpleCMS\Region\Packages;
3
4
5
use Illuminate\Support\Collection;
6
7
/**
8
 * 获取子级列表
9
 */
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
52
    {
53
        return $this->regions;
54
    }
55
}