Completed
Pull Request — master (#6)
by Tomáš
05:31
created

ProxySourceCollection::offsetGet()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 1
crap 6
1
<?php
2
3
/*
4
 * This file is a part of Sculpin.
5
 *
6
 * (c) Dragonfly Development Inc.
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 Symplify\PHP7_Sculpin\Contrib\ProxySourceCollection;
13
14
use Symplify\PHP7_Sculpin\Contrib\ProxySourceCollection\Sorter\DefaultSorter;
15
use Symplify\PHP7_Sculpin\Contrib\ProxySourceCollection\Sorter\SorterInterface;
16
17
final class ProxySourceCollection implements \ArrayAccess, \Iterator, \Countable
18
{
19
    protected $items;
20
    protected $sorter;
21
22
    public function __construct(array $items = [], SorterInterface $sorter = null)
23
    {
24
        $this->items = $items;
25
        $this->sorter = $sorter ?: new DefaultSorter();
26
    }
27
28
    public function offsetSet($offset, $value)
29
    {
30
        if (is_null($offset)) {
31
            $this->items[] = $value;
32
        } else {
33
            $this->items[$offset] = $value;
34
        }
35
    }
36
37
    public function offsetExists($offset)
38
    {
39
        return isset($this->items[$offset]);
40
    }
41
42
    public function offsetUnset($offset)
43
    {
44
        unset($this->items[$offset]);
45
    }
46
47
    public function offsetGet($offset)
48
    {
49
        return isset($this->items[$offset]) ? $this->items[$offset] : null;
50
    }
51
52
    public function rewind()
53
    {
54
        reset($this->items);
55
    }
56
57
    public function current()
58
    {
59
        return current($this->items);
60
    }
61
62
    public function key()
63
    {
64
        return key($this->items);
65
    }
66
67
    public function next()
68
    {
69
        return next($this->items);
70
    }
71
72
    public function valid()
73
    {
74
        return $this->current() !== false;
75
    }
76
77
    public function count()
78
    {
79
        return count($this->items);
80
    }
81
82
    public function init()
83
    {
84
        $this->sort();
85
86
        $previousItem = null;
87
        $item = null;
88
89
        foreach (array_reverse($this->items) as $item) {
90
            if ($previousItem) {
91
                $previousItem->setNextItem($item);
92
            }
93
            $item->setPreviousItem($previousItem);
94
            $previousItem = $item;
95
        }
96
97
        if ($item) {
98
            $item->setNextItem(null);
99
        }
100
    }
101
102
    public function first()
103
    {
104
        $keys = array_keys($this->items);
105
106
        return $this->items[$keys[0]];
107
    }
108
109
    public function sort()
110
    {
111
        uasort($this->items, [$this->sorter, 'sort']);
112
    }
113
}
114