Completed
Pull Request — master (#28)
by Tom
06:02 queued 03:01
created

Config::offsetExists()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 10
rs 9.4285
cc 2
eloc 6
nc 2
nop 1
1
<?php
2
3
namespace TomPHP\ConfigServiceProvider;
4
5
use ArrayAccess;
6
use TomPHP\ConfigServiceProvider\Exception\EntryDoesNotExistException;
7
use TomPHP\ConfigServiceProvider\Exception\ReadOnlyException;
8
9
final class Config implements ArrayAccess
10
{
11
    /**
12
     * @var array
13
     */
14
    private $config;
15
16
    /**
17
     * @var string
18
     */
19
    private $separator;
20
21
    /**
22
     * @param string $separator
23
     */
24
    public function __construct(array $config, $separator = '.')
25
    {
26
        $this->config    = $config;
27
        $this->separator = $separator;
28
    }
29
30
    public function offsetExists($offset)
31
    {
32
        try {
33
            $this->traverseConfig($this->getPath($offset));
34
        } catch (EntryDoesNotExistException $e) {
35
            return false;
36
        }
37
38
        return true;
39
    }
40
41
    public function offsetGet($offset)
42
    {
43
        return $this->traverseConfig($this->getPath($offset));
44
    }
45
46
    public function offsetSet($offset, $value)
47
    {
48
        throw new ReadOnlyException('Config is read only.');
49
    }
50
51
    public function offsetUnset($offset)
52
    {
53
        throw new ReadOnlyException('Config is read only.');
54
    }
55
56
    /**
57
     * @return array
58
     */
59
    public function asArray()
60
    {
61
        return $this->config;
62
    }
63
64
    /**
65
     * @return string
66
     */
67
    public function getSeparator()
68
    {
69
        return $this->separator;
70
    }
71
72
    private function getPath($offset)
73
    {
74
        return explode($this->separator, $offset);
75
    }
76
77
    private function traverseConfig(array $path)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
78
    {
79
        $pointer = &$this->config;
80
81
        foreach ($path as $node) {
82
            if (!is_array($pointer) || !array_key_exists($node, $pointer)) {
83
                throw new EntryDoesNotExistException("No entry found for " . implode($this->separator, $path));
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal No entry found for does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
84
            }
85
86
            $pointer = &$pointer[$node];
87
        }
88
89
        return $pointer;
90
    }
91
}
92