XrefCollection::getById()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 6
cp 0.8333
rs 9.6666
c 0
b 0
f 0
cc 3
eloc 5
nc 3
nop 1
crap 3.0416
1
<?php
2
3
namespace ConfigToken\TreeCompiler;
4
5
use ConfigToken\Exception\AlreadyRegisteredException;
6
7
8
class XrefCollection implements \IteratorAggregate, \ArrayAccess
9
{
10
    /** @var Xref[] */
11
    protected $collection;
12
13 7
    public function __construct()
14
    {
15 7
        $this->clear();
16 7
    }
17
18
    /**
19
     * (PHP 5 &gt;= 5.0.0)<br/>
20
     * Retrieve an external iterator
21
     * @link http://php.net/manual/en/iteratoraggregate.getiterator.php
22
     * @return \Traversable An instance of an object implementing <b>Iterator</b> or <b>Traversable</b>
23
     */
24
    public function getIterator()
25
    {
26
        return new \ArrayIterator($this->collection);
27
    }
28
29
    /**
30
     * (PHP 5 &gt;= 5.0.0)<br/>
31
     * Whether a offset exists
32
     * @link http://php.net/manual/en/arrayaccess.offsetexists.php
33
     * @param mixed $offset <p>
34
     * An offset to check for.
35
     * </p>
36
     * @return boolean true on success or false on failure.
37
     * </p>
38
     * <p>
39
     * The return value will be casted to boolean if non-boolean was returned.
40
     */
41
    public function offsetExists($offset)
42
    {
43
        return $this->hasById($offset);
44
    }
45
46
    /**
47
     * (PHP 5 &gt;= 5.0.0)<br/>
48
     * Offset to retrieve
49
     * @link http://php.net/manual/en/arrayaccess.offsetget.php
50
     * @param mixed $offset <p>
51
     * The offset to retrieve.
52
     * </p>
53
     * @return mixed Can return all value types.
54
     */
55 7
    public function offsetGet($offset)
56
    {
57 7
        return $this->getById($offset);
58
    }
59
60
    /**
61
     * (PHP 5 &gt;= 5.0.0)<br/>
62
     * Offset to set
63
     * @link http://php.net/manual/en/arrayaccess.offsetset.php
64
     * @param mixed $offset <p>
65
     * The offset to assign the value to.
66
     * </p>
67
     * @param mixed $value <p>
68
     * The value to set.
69
     * </p>
70
     * @return void
71
     * @throws \Exception
72
     */
73
    public function offsetSet($offset, $value)
74
    {
75
        if (!$value instanceof Xref) {
76
            throw new \Exception('Value is not an Xref.');
77
        }
78
        if (!isset($offset)) {
79
            $this->add($value);
80
            return;
81
        }
82
        $this->collection[$offset] = $value;
83
    }
84
85
    /**
86
     * (PHP 5 &gt;= 5.0.0)<br/>
87
     * Offset to unset
88
     * @link http://php.net/manual/en/arrayaccess.offsetunset.php
89
     * @param mixed $offset <p>
90
     * The offset to unset.
91
     * </p>
92
     * @return void
93
     */
94
    public function offsetUnset($offset)
95
    {
96
        if (!isset($offset)) {
97
            return;
98
        }
99
        if ($this->hasById($offset)) {
100
            $this->removeById($offset);
101
            return;
102
        }
103
        trigger_error(sprintf('Xref with id "%s" not in collection.', $offset), E_USER_WARNING);
104
    }
105
106
    public function isEmpty()
107
    {
108
        return empty($this->collection);
109
    }
110
111 7
    public function clear()
112
    {
113 7
        $this->collection = array();
114
115 7
        return $this;
116
    }
117
118 7
    public function add(Xref $xref)
119
    {
120 7
        $xrefId = $xref->getId();
121 7
        if ($this->hasById($xrefId)) {
122 5
            return $this->getById($xrefId);
123
        }
124 7
        $this->collection[] = $xref;
125 7
        return $xref;
126
    }
127
128
    public function removeById($xrefId)
129
    {
130
        $xref = null;
131
        if ($this->hasById($xrefId)) {
132
            foreach ($this->collection as $key => $xref) {
133
                if ($xref->getId() == $xrefId) {
134
                    unset($this->collection[$key]);
135
                }
136
            }
137
        } else {
138
            throw new \Exception(
139
                sprintf(
140
                    'Xref with id "%s" not in collection.',
141
                    $xrefId
142
                )
143
            );
144
        }
145
        return $xref;
146
    }
147
148
    public function remove(Xref $xref)
149
    {
150
        try {
151
            $this->removeById($xref->getId());
152
        } catch (\Exception $e) {
153
            if (strpos($e->getMessage(), $xref->getId())) {
154
                throw new \Exception(
155
                    sprintf(
156
                        'Xref of type "%s" with location "%s" not in collection.',
157
                        $xref->getType(),
158
                        $xref->getLocation()
159
                    )
160
                );
161
            }
162
            throw $e;
163
        }
164
    }
165
166 7
    public function hasById($xrefId)
167
    {
168 7
        foreach ($this->collection as $xref) {
169 7
            if ($xref->getId() == $xrefId) {
170 7
                return true;
171
            }
172 7
        }
173 7
        return false;
174
    }
175
176
    public function has(Xref $xref)
177
    {
178
        return $this->hasById($xref->getId());
179
    }
180
181 7
    public function getById($xrefId)
182
    {
183 7
        foreach ($this->collection as $xref) {
184 7
            if ($xref->getId() == $xrefId) {
185 7
                return $xref;
186
            }
187 5
        }
188
        throw new \Exception(sprintf('Xref with Id "%s" not in collection.', $xrefId));
189
    }
190
191
    public function parse($xrefs, $typeDelimiter, $overwrite = False, $ignore = True)
192
    {
193
        if (!is_array($xrefs)) {
194
            $xrefs = array($xrefs);
195
        }
196
        $parsed = array();
197
        foreach ($xrefs as $key => $value) {
198
            $xref = Xref::makeFromDefinitionString($value, $typeDelimiter);
199
            if ($this->has($xref)) {
200
                if (!$ignore) {
201
                    throw new AlreadyRegisteredException(
202
                        sprintf(
203
                            'Not allowed to overwrite Xref of type "%s" with location "%s" already in collection.',
204
                            $xref->getType(),
205
                            $xref->getLocation()
206
                        )
207
                    );
208
                }
209
                if ($overwrite && (!$ignore)) {
210
                    $this->add($xref);
211
                }
212
            } else {
213
                $this->add($xref);
214
            }
215
            $parsed[$key] = $xref;
216
        }
217
        return $parsed;
218
    }
219
220
    public function hasUnresolved()
221
    {
222
        foreach($this->collection as $xref) {
223
            if (!$xref->isResolved()) {
224
                return true;
225
            }
226
        }
227
        return false;
228
    }
229
230
    public function resolve($force = false)
231
    {
232
        foreach($this->collection as $xref) {
233
            $xref->resolve($force);
234
        }
235
    }
236
}