Repository   A
last analyzed

Complexity

Total Complexity 26

Size/Duplication

Total Lines 174
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 26
lcom 1
cbo 1
dl 0
loc 174
rs 10
c 0
b 0
f 0

17 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A get() 0 4 2
A set() 0 4 1
A del() 0 6 2
A union() 0 17 5
A intersection() 0 17 2
A getArray() 0 4 1
A offsetSet() 0 8 2
A offsetExists() 0 4 1
A offsetUnset() 0 4 1
A offsetGet() 0 4 2
A count() 0 4 1
A rewind() 0 4 1
A current() 0 4 1
A key() 0 4 1
A next() 0 4 1
A valid() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the Yosymfony\Config-loader.
5
 *
6
 * (c) YoSymfony <http://github.com/yosymfony>
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 Yosymfony\ConfigLoader;
13
14
/**
15
 * Simple implementation of a configuration repository.
16
 *
17
 * @author Victor Puertas <[email protected]>
18
 */
19
class Repository implements RepositoryInterface
20
{
21
    protected $repository = [];
22
23
    /**
24
     * Construct a configuration repository from an array
25
     *
26
     * @param array $values
27
     */
28
    public function __construct(array $values = [])
29
    {
30
        $this->repository = $values;
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function get(string $key, $default = null)
37
    {
38
        return isset($this->repository[$key]) ? $this->repository[$key] : $default;
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function set(string $key, $value) : void
45
    {
46
        $this->offsetSet($key, $value);
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function del(string $key) : void
53
    {
54
        if (array_key_exists($key, $this->repository)) {
55
            unset($this->repository[$key]);
56
        }
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function union(RepositoryInterface $repository) : RepositoryInterface
63
    {
64
        $union = function (array $r1, array $r2) use (&$union) {
65
            $res = $r1;
66
            foreach ($r2 as $k => $v) {
67
                if (isset($res[$k]) && is_array($r1[$k])) {
68
                    $res[$k] = $union($r1[$k], $v);
69
                } elseif (!isset($res[$k])) {
70
                    $res[$k] = $v;
71
                }
72
            }
73
74
            return $res;
75
        };
76
77
        return new self($union($this->getArray(), $repository->getArray()));
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function intersection(RepositoryInterface $repository) : RepositoryInterface
84
    {
85
        $interception = function ($main, $second) {
86
            $result = new Repository();
87
            $keysMain = array_keys($main->getArray());
88
            $keysSecond = array_keys($second->getArray());
89
            $keys = array_intersect($keysMain, $keysSecond);
90
91
            foreach ($keys as $key) {
92
                $result[$key] = $main[$key];
93
            }
94
95
            return $result;
96
        };
97
98
        return $interception($this, $repository);
99
    }
100
101
    /**
102
     * {@inheritdoc}.
103
     */
104
    public function getArray() : array
105
    {
106
        return $this->repository;
107
    }
108
109
    /**
110
     * Set a new key (From ArrayAccess interface).
111
     */
112
    public function offsetSet($offset, $value)
113
    {
114
        if (is_null($offset)) {
115
            $this->repository[] = $value;
116
        } else {
117
            $this->repository[$offset] = $value;
118
        }
119
    }
120
121
    /**
122
     * Check if a key exists (from ArrayAccess interface).
123
     */
124
    public function offsetExists($offset)
125
    {
126
        return isset($this->repository[$offset]);
127
    }
128
129
    /**
130
     * Delete a key (from ArrayAccess interface).
131
     */
132
    public function offsetUnset($offset)
133
    {
134
        unset($this->repository[$offset]);
135
    }
136
137
    /**
138
     * Retrueve a key (from ArrayAccess interface).
139
     */
140
    public function offsetGet($offset)
141
    {
142
        return isset($this->repository[$offset]) ? $this->repository[$offset] : null;
143
    }
144
145
    /**
146
     * Count of element of a repository (from Countable interface).
147
     */
148
    public function count()
149
    {
150
        return count($this->repository);
151
    }
152
153
    /**
154
     * Set the pointer to the first element (from Iterator interface).
155
     */
156
    public function rewind()
157
    {
158
        return reset($this->repository);
159
    }
160
161
    /**
162
     * Get the current element (from Iterator interface).
163
     */
164
    public function current()
165
    {
166
        return current($this->repository);
167
    }
168
169
    /**
170
     * Get the current position (from Iterator interface).
171
     */
172
    public function key()
173
    {
174
        return key($this->repository);
175
    }
176
177
    /**
178
     * Set the pointer to the next element (from Iterator interface).
179
     */
180
    public function next()
181
    {
182
        return next($this->repository);
183
    }
184
185
    /**
186
     * Checks if the current position is valid (from Iterator interface).
187
     */
188
    public function valid()
189
    {
190
        return key($this->repository) !== null;
191
    }
192
}
193