Completed
Push — master ( f12285...016462 )
by Anton
09:53 queued 07:39
created

Section::getIcon()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Spiral Framework.
4
 *
5
 * @license   MIT
6
 * @author    Anton Titov (Wolfy-J)
7
 */
8
9
namespace Spiral\Vault\Models;
10
11
use Spiral\Core\Component;
12
use Spiral\Translator\Traits\TranslatorTrait;
13
use Spiral\Vault\Vault;
14
15
class Section extends Component
16
{
17
    use TranslatorTrait;
18
19
    /**
20
     * @var Vault
21
     */
22
    private $vault = null;
23
24
    /**
25
     * @var array
26
     */
27
    private $section = [
28
        'title' => '',
29
        'badge' => '',
30
        'icon'  => '',
31
        'items' => []
32
    ];
33
34
    /**
35
     * @var Item[]
36
     */
37
    private $items = [];
38
39
    /**
40
     * @param Vault $vault
41
     * @param array $section
42
     */
43
    public function __construct(Vault $vault, array $section)
44
    {
45
        $this->vault = $vault;
46
        $this->section = $section;
47
48
        foreach ($this->section['items'] as $target => $item) {
49
            $this->items[] = new Item($vault, $target, $item);
50
        }
51
    }
52
53
    /**
54
     * Section title.
55
     *
56
     * @return string
57
     */
58
    public function getTitle(): string
59
    {
60
        return $this->say($this->section['title']);
61
    }
62
63
    /**
64
     * @return string
65
     */
66
    public function getIcon(): string
67
    {
68
        return $this->section['icon'];
69
    }
70
71
    /**
72
     * Must return true is section has at least one available item.
73
     *
74
     * @return bool
75
     */
76
    public function isAvailable(): bool
77
    {
78
        foreach ($this->items as $item) {
79
            if ($item->isVisible()) {
80
                return true;
81
            }
82
        }
83
84
        return false;
85
    }
86
87
    /**
88
     * Must return true if at least one of section items points to a given controller (based on
89
     * target).
90
     *
91
     * @param string $controller
92
     *
93
     * @return bool
94
     */
95
    public function hasController(string $controller): bool
96
    {
97
        foreach ($this->items as $item) {
98
            if (
99
                $item->getTarget() == $controller
100
                || strpos($item->getTarget(), $controller . ':') === 0
101
            ) {
102
                return true;
103
            }
104
        }
105
106
        return false;
107
    }
108
109
    /**
110
     * @return Item[]
111
     */
112
    public function getItems(): array
113
    {
114
        return $this->items;
115
    }
116
}