Passed
Pull Request — develop (#53)
by Kevin
08:43
created

ArrayConfigurationRepository::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Magium\Configuration\Config\Repository;
4
5
use Magium\Configuration\Config\InvalidArgumentException;
6
7
class ArrayConfigurationRepository implements ConfigInterface
8
{
9
10
    protected $data;
11
12 10
    public function __construct($data)
13
    {
14 10
        if (!is_array($data)) {
15 1
            throw new InvalidArgumentException('Configuration data must be in an array format');
16
        }
17 9
        $this->data = $data;
18 9
    }
19
20 3
    public function getValue($path)
21
    {
22 3
        return $this->xpath($path);
23
    }
24
25
    public function hasValue($path)
26
    {
27
        return $this->xpath($path) !== null;
28
    }
29
30 2
    public function getValueFlag($path)
31
    {
32 2
        $value = $this->getValue($path);
33 2
        return in_array($value, self::ALLOWED_TRUES);
34
    }
35
36 9
    public function xpath($xpath)
37
    {
38 9
        $parts = explode('/', $xpath);
39 9
        if (count($parts) != 3) {
40 2
            throw new InvalidArgumentException('Path must be in the form of "section/group/element"');
41
        }
42 7
        $process = $this->data;
43 7
        foreach ($parts as $part) {
44 7
            if (isset($process[$part])) {
45 7
                $process = $process[$part];
46
            } else {
47 7
                return null;
48
            }
49
        }
50 6
        return $process;
51
    }
52
53
54
}
55