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

ChildrenRecursively   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 15
c 1
b 0
f 0
dl 0
loc 36
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getRegions() 0 3 1
A __construct() 0 4 1
A loadRecursively() 0 14 6
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
}