University::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\University;
6
7
use Exception;
8
9
class University
10
{
11
    /**
12
     * The attributes array.
13
     *
14
     * @var array
15
     */
16
    protected $attributes;
17
18
    /**
19
     * Create a new University 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->getCountry())) {
32
            throw new Exception('Missing mandatory university 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 alternative name.
118
     *
119
     * @return string|null
120
     */
121
    public function getAltName(): ?string
122
    {
123
        return $this->get('alt_name');
124
    }
125
126
    /**
127
     * Get the country.
128
     *
129
     * @return string|null
130
     */
131
    public function getCountry(): ?string
132
    {
133
        return $this->get('country');
134
    }
135
136
    /**
137
     * Get the state.
138
     *
139
     * @return string|null
140
     */
141
    public function getState(): ?string
142
    {
143
        return $this->get('state');
144
    }
145
146
    /**
147
     * Get the address.
148
     *
149
     * @return array|null
150
     */
151
    public function getAddress(): ?array
152
    {
153
        return $this->get('address');
154
    }
155
156
    /**
157
     * Get the street.
158
     *
159
     * @return string|null
160
     */
161
    public function getStreet(): ?string
162
    {
163
        return $this->get('address.street');
164
    }
165
166
    /**
167
     * Get the city.
168
     *
169
     * @return string|null
170
     */
171
    public function getCity(): ?string
172
    {
173
        return $this->get('address.city');
174
    }
175
176
    /**
177
     * Get the province.
178
     *
179
     * @return string|null
180
     */
181
    public function getProvince(): ?string
182
    {
183
        return $this->get('address.province');
184
    }
185
186
    /**
187
     * Get the postal code.
188
     *
189
     * @return string|null
190
     */
191
    public function getPostalCode(): ?string
192
    {
193
        return $this->get('address.postal_code');
194
    }
195
196
    /**
197
     * Get the contact.
198
     *
199
     * @return array|null
200
     */
201
    public function getContact(): ?array
202
    {
203
        return $this->get('contact');
204
    }
205
206
    /**
207
     * Get the telephone.
208
     *
209
     * @return string|null
210
     */
211
    public function getTelephone(): ?string
212
    {
213
        return $this->get('contact.telephone');
214
    }
215
216
    /**
217
     * Get the website.
218
     *
219
     * @return string|null
220
     */
221
    public function getWebsite(): ?string
222
    {
223
        return $this->get('contact.website');
224
    }
225
226
    /**
227
     * Get the email.
228
     *
229
     * @return string|null
230
     */
231
    public function getEmail(): ?string
232
    {
233
        return $this->get('contact.email');
234
    }
235
236
    /**
237
     * Get the fax.
238
     *
239
     * @return string|null
240
     */
241
    public function getFax(): ?string
242
    {
243
        return $this->get('contact.fax');
244
    }
245
246
    /**
247
     * Get the funding.
248
     *
249
     * @return string|null
250
     */
251
    public function getFunding(): ?string
252
    {
253
        return $this->get('funding');
254
    }
255
256
    /**
257
     * Get the languages.
258
     *
259
     * @return array|null
260
     */
261
    public function getLanguages(): ?array
262
    {
263
        return $this->get('languages');
264
    }
265
266
    /**
267
     * Get the academic year.
268
     *
269
     * @return string|null
270
     */
271
    public function getAcademicYear(): ?string
272
    {
273
        return $this->get('academic_year');
274
    }
275
276
    /**
277
     * Get the accrediting agency.
278
     *
279
     * @return string|null
280
     */
281
    public function getAccreditingAgency(): ?string
282
    {
283
        return $this->get('accrediting_agency');
284
    }
285
}
286