Data::__construct()   A
last analyzed

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
/**
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
The condition $attribute === null is always false.
Loading history...
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