Test Setup Failed
Branch master (ca3ca3)
by Dennis
14:25
created

ChildrenRecursively::loadRecursively()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 14
rs 9.2222
cc 6
nc 6
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 = 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
44
    {
45
        return $this->regions;
46
    }
47
}