Language::get()   B
last analyzed

Complexity

Conditions 6
Paths 5

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 5
nop 2
dl 0
loc 22
rs 8.9457
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Rinvex\Language;
6
7
use Exception;
8
9
class Language
10
{
11
    /**
12
     * The attributes array.
13
     *
14
     * @var array
15
     */
16
    protected $attributes;
17
18
    /**
19
     * Create a new Language instance.
20
     *
21
     * @param array $attributes
22
     *
23
     * @throws \Exception
24
     */
25
    public function __construct($attributes)
26
    {
27
        // Set the attributes
28
        $this->setAttributes($attributes);
29
30
        // Check required mandatory attributes
31
        if (empty($this->getName()) || empty($this->getNativeName()) || empty($this->getIso6391())) {
32
            throw new Exception('Missing mandatory language attributes!');
33
        }
34
    }
35
36
    /**
37
     * Set the attributes.
38
     *
39
     * @param array $attributes
40
     *
41
     * @return $this
42
     */
43
    public function setAttributes($attributes)
44
    {
45
        $this->attributes = $attributes;
46
47
        return $this;
48
    }
49
50
    /**
51
     * Get the attributes.
52
     *
53
     * @return array|null
54
     */
55
    public function getAttributes(): ?array
56
    {
57
        return $this->attributes;
58
    }
59
60
    /**
61
     * Set single attribute.
62
     *
63
     * @param string $key
64
     * @param mixed  $value
65
     *
66
     * @return $this
67
     */
68
    public function set($key, $value)
69
    {
70
        $this->attributes[$key] = $value;
71
72
        return $this;
73
    }
74
75
    /**
76
     * Get an item from attributes array using "dot" notation.
77
     *
78
     * @param string $key
79
     * @param mixed  $default
80
     *
81
     * @return mixed
82
     */
83
    public function get($key, $default = null)
84
    {
85
        $array = $this->attributes;
86
87
        if (is_null($key)) {
88
            return $array;
89
        }
90
91
        if (array_key_exists($key, $array)) {
92
            return $array[$key];
93
        }
94
95
        foreach (explode('.', $key) as $segment) {
96
            if (is_array($array) && array_key_exists($segment, $array)) {
97
                $array = $array[$segment];
98
            } else {
99
                return $default;
100
            }
101
        }
102
103
        return $array;
104
    }
105
106
    /**
107
     * Get the name.
108
     *
109
     * @return string|null
110
     */
111
    public function getName(): ?string
112
    {
113
        return $this->get('name');
114
    }
115
116
    /**
117
     * Get the given native name or fallback to first native name.
118
     *
119
     * @return string|null
120
     */
121
    public function getNativeName(): ?string
122
    {
123
        return $this->get('native');
124
    }
125
126
    /**
127
     * Get the ISO 639-1 code.
128
     *
129
     * @return string|null
130
     */
131
    public function getIso6391()
132
    {
133
        return $this->get('iso_639_1');
134
    }
135
136
    /**
137
     * Get the ISO 639-2 code.
138
     *
139
     * @return string|null
140
     */
141
    public function getIso6392()
142
    {
143
        return $this->get('iso_639_2');
144
    }
145
146
    /**
147
     * Get the ISO 639-3 code.
148
     *
149
     * @return string|null
150
     */
151
    public function getIso6393()
152
    {
153
        return $this->get('iso_639_3');
154
    }
155
156
    /**
157
     * Get the script.
158
     *
159
     * @return array|null
160
     */
161
    public function getScript(): ?array
162
    {
163
        return $this->get('script');
164
    }
165
166
    /**
167
     * Get the script name.
168
     *
169
     * @return string|null
170
     */
171
    public function getScriptName(): ?string
172
    {
173
        return $this->get('script.name');
174
    }
175
176
    /**
177
     * Get the script ISO 15924.
178
     *
179
     * @return string|null
180
     */
181
    public function getScriptIso15924()
182
    {
183
        return $this->get('script.iso_15924');
184
    }
185
186
    /**
187
     * Get the script ISO numeric.
188
     *
189
     * @return string|null
190
     */
191
    public function getScriptIsoNumeric(): ?string
192
    {
193
        return $this->get('script.iso_numeric');
194
    }
195
196
    /**
197
     * Get the script direction.
198
     *
199
     * @return string|null
200
     */
201
    public function getScriptDirection(): ?string
202
    {
203
        return $this->get('script.direction');
204
    }
205
206
    /**
207
     * Get the family.
208
     *
209
     * @return array|null
210
     */
211
    public function getFamily(): ?array
212
    {
213
        return $this->get('family');
214
    }
215
216
    /**
217
     * Get the family name.
218
     *
219
     * @return string|null
220
     */
221
    public function getFamilyName(): ?string
222
    {
223
        return $this->get('family.name');
224
    }
225
226
    /**
227
     * Get the family ISO 639-5.
228
     *
229
     * @return string|null
230
     */
231
    public function getFamilyIso6395()
232
    {
233
        return $this->get('family.iso_639_5');
234
    }
235
236
    /**
237
     * Get the family hierarchy.
238
     *
239
     * @return string|null
240
     */
241
    public function getFamilyHierarchy(): ?string
242
    {
243
        return $this->get('family.hierarchy');
244
    }
245
246
    /**
247
     * Get the scope.
248
     *
249
     * @return string|null
250
     */
251
    public function getScope(): ?string
252
    {
253
        return $this->get('scope');
254
    }
255
256
    /**
257
     * Get the type.
258
     *
259
     * @return string|null
260
     */
261
    public function getType(): ?string
262
    {
263
        return $this->get('type');
264
    }
265
266
    /**
267
     * Get the cultures.
268
     *
269
     * @return array|null
270
     */
271
    public function getCultures(): ?array
272
    {
273
        return $this->get('cultures');
274
    }
275
276
    /**
277
     * Get the given culture.
278
     *
279
     * @param string|null $culture
280
     *
281
     * @return array|null
282
     */
283
    public function getCulture($culture = null): ?array
284
    {
285
        return $this->getCultures()[$culture] ?? (! empty($this->getCultures()) ? current($this->getCultures()) : null);
286
    }
287
}
288