Completed
Push — master ( 0e678b...8a4e34 )
by Filipe
02:44
created

PriorityList::getIterator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/**
4
 * This file is part of slick/configuration
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Slick\Configuration\Common;
11
12
use Traversable;
13
14
/**
15
 * PriorityList
16
 *
17
 * @package Slick\Configuration\Common
18
 */
19
class PriorityList implements \ArrayAccess, \Countable, \IteratorAggregate
20
{
21
    /**
22
     * @var array
23
     */
24
    private $data = [];
25
26
    /**
27
     * @var int
28
     */
29
    private $lastPriority = 0;
30
31
    /**
32
     * Inserts the provided element in the right order given the priority
33
     *
34
     * The lowest priority will be the first element in the list. If no priority
35
     * if given, the
36
     *
37
     * @param mixed $element
38
     * @param int  $priority
39
     *
40
     * @return PriorityList
41
     */
42
    public function insert($element, $priority = 0)
43
    {
44
        $data = [];
45
        $inserted = false;
46
        $priority = $priority === 0 ? $this->lastPriority : $priority;
47
48
        foreach ($this->data as $datum) {
49
            $inserted = $this->tryToInsert($element, $priority, $data, $datum);
50
            $data[] = ['element' => $datum['element'], 'priority' => $datum['priority']];
51
            $this->lastPriority = $datum['priority'];
52
        }
53
54
        if (!$inserted) {
55
            $data[] = ['element' => $element, 'priority' => $priority];
56
            $this->lastPriority = $priority;
57
        }
58
59
        $this->data = $data;
60
        return $this;
61
    }
62
63
    /**
64
     * Tries to insert the provided element in the passed data array
65
     *
66
     * @param mixed   $element
67
     * @param integer $priority
68
     * @param array   $data
69
     * @param array   $datum
70
     *
71
     * @return bool
72
     */
73
    private function tryToInsert($element, $priority, &$data, $datum)
74
    {
75
        $inserted = false;
76
        if ($datum['priority'] > $priority) {
77
            $data[] = ['element' => $element, 'priority' => $priority];
78
            $inserted = true;
79
        }
80
        return $inserted;
81
    }
82
83
    /**
84
     * Returns the inserted elements in the order given by priority as an array
85
     *
86
     * @return array
87
     */
88
    public function asArray()
89
    {
90
        $data = [];
91
        foreach ($this->data as $datum) {
92
            $data[] = $datum['element'];
93
        }
94
        return $data;
95
    }
96
97
    /**
98
     * @inheritdoc
99
     */
100
    public function offsetExists($offset)
101
    {
102
        return isset($this->data[$offset]);
103
    }
104
105
    /**
106
     * @inheritdoc
107
     */
108
    public function offsetGet($offset)
109
    {
110
        return $this->data[$offset];
111
    }
112
113
    /**
114
     * @inheritdoc
115
     */
116
    public function offsetSet($offset, $value)
117
    {
118
        $this->data[$offset] = $value;
119
    }
120
121
    /**
122
     * @inheritdoc
123
     */
124
    public function offsetUnset($offset)
125
    {
126
        unset($this->data[$offset]);
127
    }
128
129
    /**
130
     * @inheritdoc
131
     */
132
    public function count()
133
    {
134
        return count($this->data);
135
    }
136
137
    /**
138
     * @inheritdoc
139
     */
140
    public function getIterator()
141
    {
142
        return new \ArrayIterator($this->asArray());
143
    }
144
}
145