Test Failed
Push — master ( 04b63e...1cbff4 )
by
08:50
created

Section::initSettings()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
crap 2
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)
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()
58
    {
59
        return $this->data['owner_aware'];
60
    }
61
62
    /**
63
     * @return bool
64
     */
65
    public function isEnabled()
66
    {
67
        return $this->data['enabled'];
68
    }
69
70
    /**
71
     * @return string
72
     */
73
    public function getLabel()
74
    {
75
        return $this->data['label'];
76
    }
77
78
    /**
79
     * @return string
80
     */
81 9
    public function getDescription()
82
    {
83 9
        return $this->data['description'];
84
    }
85
86
    /**
87
     * @return string
88
     */
89
    public function getName()
90
    {
91
        return $this->data['name'];
92
    }
93 2
94
    /**
95 2
     * @param string $key
96 1
     *
97
     * @return SettingSchema
98
     *
99 1
     * @throws \InvalidArgumentException
100
     */
101
    public function getSetting($key)
102
    {
103
        if (!array_key_exists($key, $this->settings)) {
104
            throw new \InvalidArgumentException("No setting key `$key` in this section.`");
105 3
        }
106
107 3
        return $this->settings[$key];
108
    }
109
110
    /**
111
     * @return SettingSchema[]
112
     */
113
    public function getSettings()
114
    {
115
        return array_values($this->settings);
116
    }
117
118
    /**
119
     * {@inheritdoc}
120
     */
121
    public function jsonSerialize()
122
    {
123
        return array_merge($this->data, [
124
            'settings' => $this->settings,
125
        ]);
126
    }
127
}
128