Completed
Push — master ( a97ce7...6f972b )
by Jan
03:17
created

Data::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Kontrolio\Data;
4
5
final class Data
6
{
7
    private $data;
8
9
    public function __construct(array $data)
10
    {
11
        $this->data = $data;
12
    }
13
14
    public function raw()
15
    {
16
        return $this->data;
17
    }
18
19
    /**
20
     * @param string $attribute
21
     *
22
     * @return Attribute
23
     */
24
    public function get($attribute)
25
    {
26
        if ($attribute === null) {
0 ignored issues
show
introduced by
The condition $attribute === null is always false.
Loading history...
27
            return new Attribute($attribute);
28
        }
29
30
        if (isset($this->data[$attribute])) {
31
            return new Attribute($attribute, $this->data[$attribute]);
32
        }
33
34
        $segments = explode('.', $attribute);
35
        $data = null;
36
37
        foreach ($segments as $segment) {
38
            if (!array_key_exists($segment, $this->data)) {
39
                return new Attribute($attribute);
40
            }
41
42
            $data = $this->data[$segment];
43
        }
44
45
        return new Attribute($attribute, $data);
46
    }
47
}
48