Passed
Push — master ( e83acc...293a64 )
by
06:35
created

Section::hasSetting()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
/*
4
 * This file is part of the PhpMob package.
5
 *
6
 * (c) Ishmael Doss <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace PhpMob\Settings\Schema;
15
16
/**
17
 * @author Ishmael Doss <[email protected]>
18
 */
19
class Section implements \JsonSerializable
20
{
21
    /**
22
     * @var array
23
     */
24
    private $data = [];
25
26
    /**
27
     * @var SettingSchema[]
28
     */
29
    private $settings = [];
30
31 9
    public function __construct($name, array $data)
32
    {
33 9
        $this->data['name'] = $name;
34
35 9
        foreach ($data as $key => $value) {
36 9
            if ('settings' === $key) {
37 9
                $this->initSettings($value);
38
            } else {
39 9
                $this->data[$key] = $value;
40
            }
41
        }
42 9
    }
43
44
    /**
45
     * @param array $settings
46
     */
47 9
    private function initSettings(array $settings): void
48
    {
49 9
        foreach ($settings as $schemaKey => $schemaData) {
50 9
            $this->settings[$schemaKey] = new SettingSchema($this, $schemaKey, $schemaData);
51
        }
52 9
    }
53
54
    /**
55
     * @return bool
56
     */
57
    public function isOwnerAware(): bool
58
    {
59
        return $this->data['owner_aware'];
60
    }
61
62
    /**
63
     * @return bool
64
     */
65
    public function isEnabled(): bool
66
    {
67
        return $this->data['enabled'];
68
    }
69
70
    /**
71
     * @return string
72
     */
73
    public function getLabel(): string
74
    {
75
        return $this->data['label'];
76
    }
77
78
    /**
79
     * @return string|null
80
     */
81
    public function getDescription(): ?string
82
    {
83
        return $this->data['description'];
84
    }
85
86
    /**
87
     * @return string
88
     */
89 9
    public function getName(): string
90
    {
91 9
        return $this->data['name'];
92
    }
93
94
    /**
95
     * @param string $key
96
     *
97
     * @return SettingSchema
98
     *
99
     * @throws \InvalidArgumentException
100
     */
101 2
    public function getSetting($key): SettingSchema
102
    {
103 2
        if (!$this->hasSetting($key)) {
104 1
            throw new \InvalidArgumentException("No setting key `$key` in this section.`");
105
        }
106
107 1
        return $this->settings[$key];
108
    }
109
110
    /**
111
     * @param string $key
112
     *
113
     * @return bool
114
     */
115 2
    public function hasSetting(string $key): bool
116
    {
117 2
        return array_key_exists($key, $this->settings);
118
    }
119
120
    /**
121
     * @return SettingSchema[]
122
     */
123 3
    public function getSettings(): array
124
    {
125 3
        return array_values($this->settings);
126
    }
127
128
    /**
129
     * {@inheritdoc}
130
     */
131
    public function jsonSerialize(): array
132
    {
133
        return array_merge($this->data, [
134
            'settings' => $this->settings,
135
        ]);
136
    }
137
}
138