Completed
Push — master ( 35d78a...cbe7c9 )
by Ryan
07:52
created

SectionCollection::children()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 11
rs 9.4285
cc 1
eloc 6
nc 1
nop 1
1
<?php namespace Anomaly\Streams\Platform\Ui\ControlPanel\Component\Section;
2
3
use Anomaly\Streams\Platform\Ui\ControlPanel\Component\Section\Contract\SectionInterface;
4
use Illuminate\Support\Collection;
5
6
/**
7
 * Class SectionCollection
8
 *
9
 * @link          http://anomaly.is/streams-platform
10
 * @author        AnomalyLabs, Inc. <[email protected]>
11
 * @author        Ryan Thompson <[email protected]>
12
 * @package       Anomaly\Streams\Platform\Ui\ControlPanel\Component\Section
13
 */
14
class SectionCollection extends Collection
15
{
16
17
    /**
18
     * Return the active section.
19
     *
20
     * @return null|SectionInterface
21
     */
22
    public function active()
23
    {
24
        /* @var SectionInterface $item */
25
        foreach ($this->items as $item) {
26
            if ($item->isActive()) {
27
                return $item;
28
            }
29
        }
30
31
        return null;
32
    }
33
34
    /**
35
     * Return only root sections.
36
     *
37
     * @return SectionCollection
38
     */
39
    public function root()
40
    {
41
        return self::make(
42
            array_filter(
43
                $this->all(),
44
                function (SectionInterface $section) {
45
                    return $section->getParent() === null;
46
                }
47
            )
48
        );
49
    }
50
51
    /**
52
     * Return only sections with parent.
53
     *
54
     * @param $parent
55
     * @return static
56
     */
57
    public function children($parent)
58
    {
59
        return self::make(
60
            array_filter(
61
                $this->all(),
62
                function (SectionInterface $section) use ($parent) {
63
                    return $section->getParent() === $parent;
64
                }
65
            )
66
        );
67
    }
68
}
69