1 | <?php |
||
2 | |||
3 | namespace Kontrolio\Data; |
||
4 | |||
5 | /** |
||
6 | * Validated data. |
||
7 | */ |
||
8 | final class Data |
||
9 | { |
||
10 | private $data; |
||
11 | |||
12 | public function __construct(array $data) |
||
13 | { |
||
14 | $this->data = $data; |
||
15 | } |
||
16 | |||
17 | public function raw() |
||
18 | { |
||
19 | return $this->data; |
||
20 | } |
||
21 | |||
22 | /** |
||
23 | * Returns an object representation of the given attribute. |
||
24 | * |
||
25 | * @param string $attribute |
||
26 | * |
||
27 | * @return Attribute |
||
28 | */ |
||
29 | public function get($attribute) |
||
30 | { |
||
31 | if ($attribute === null) { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
32 | return new Attribute($attribute); |
||
33 | } |
||
34 | |||
35 | if (isset($this->data[$attribute])) { |
||
36 | return new Attribute($attribute, $this->data[$attribute]); |
||
37 | } |
||
38 | |||
39 | $segments = explode('.', $attribute); |
||
40 | $data = null; |
||
41 | |||
42 | foreach ($segments as $segment) { |
||
43 | if (!array_key_exists($segment, $this->data)) { |
||
44 | return new Attribute($attribute); |
||
45 | } |
||
46 | |||
47 | $data = $this->data[$segment]; |
||
48 | } |
||
49 | |||
50 | return new Attribute($attribute, $data); |
||
51 | } |
||
52 | } |
||
53 |