ArrayAccess::get()   B
last analyzed

Complexity

Conditions 7
Paths 4

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 7

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 13
cts 13
cp 1
rs 8.2222
c 0
b 0
f 0
cc 7
eloc 11
nc 4
nop 2
crap 7
1
<?php
2
3
/*
4
 * This file is part of the ConfigCacheBundle package.
5
 *
6
 * Copyright (c) 2015-2016 Yahoo Japan Corporation
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace YahooJapan\ConfigCacheBundle\ConfigCache\Util;
13
14
/**
15
 * The array access utility by dotted path.
16
 */
17
class ArrayAccess implements ArrayAccessInterface
18
{
19
    protected $parameters;
20
21
    /**
22
     * Constructor.
23
     *
24
     * @param array $parameters
25
     */
26 42
    public function __construct(array $parameters = array())
27
    {
28 42
        $this->parameters = $parameters;
29 42
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34 29
    public function get($path, $default = array())
35
    {
36 29
        if (isset($this->parameters[$path])) {
37 3
            return $this->parameters[$path];
38
        }
39
40 26
        $parameters = $this->parameters;
41 26
        foreach (explode('.', $path) as $key) {
42 26
            if ((!isset($parameters[0]) && isset($parameters[$key]))
43 20
                || (is_array($parameters) && array_key_exists($key, $parameters))
44 26
            ) {
45 22
                $parameters = $parameters[$key];
46 22
            } else {
47 17
                return $default;
48
            }
49 22
        }
50
51 9
        return $parameters;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 5
    public function replace(array $parameters)
58
    {
59 5
        $this->parameters = $parameters;
60
61 5
        return $this;
62
    }
63
64
    /**
65
     * Creates a self instance.
66
     *
67
     * @param array $parameters
68
     *
69
     * @return ArrayAccess
70
     */
71 1
    public static function create(array $parameters)
72
    {
73 1
        return new static($parameters);
74
    }
75
}
76