Bag::next()   A
last analyzed

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
namespace Chadicus\Spl\DataStructures;
4
5
/**
6
 * Unordered bag collection.
7
 */
8
final class Bag implements BagInterface, \JsonSerializable
9
{
10
    /**
11
     * @var array
12
     */
13
    private $elements = [];
14
15
    /**
16
     * @var integer
17
     */
18
    private $position;
19
20
    /**
21
     * Adds a new element to the bag.
22
     *
23
     * @param mixed $element The element to add.
24
     *
25
     * @return Bag
26
     */
27
    public function add($element)
28
    {
29
        array_unshift($this->elements, $element);
30
        shuffle($this->elements);
31
        return $this;
32
    }
33
34
    /**
35
     * Removes a random element from the bag.
36
     *
37
     * @return mixed
38
     *
39
     * @throws \RuntimeException Thrown if remove is called on an empty bag.
40
     */
41
    public function remove()
42
    {
43
        if (empty($this->elements)) {
44
            throw new \RuntimeException('Bag is empty');
45
        }
46
47
        return array_pop($this->elements);
48
    }
49
50
    /**
51
     * Returns true if the bag contains no elements.
52
     *
53
     * @return boolean
54
     */
55
    public function isEmpty()
56
    {
57
        return empty($this->elements);
58
    }
59
60
    /**
61
     * @see \Iterator::rewind()
62
     *
63
     * @return void
64
     */
65
    public function rewind()
66
    {
67
        $this->position = 0;
68
        shuffle($this->elements);
69
    }
70
71
    /**
72
     * @see \Iterator::current()
73
     *
74
     * @return mixed
75
     */
76
    public function current()
77
    {
78
        return $this->elements[$this->position];
79
    }
80
81
    /**
82
     * @see \Iterator::key()
83
     *
84
     * @return integer
85
     */
86
    public function key()
87
    {
88
        return $this->position;
89
    }
90
91
    /**
92
     * @see \Iterator::next()
93
     *
94
     * @return void
95
     */
96
    public function next()
97
    {
98
        ++$this->position;
99
    }
100
101
    /**
102
     * @see \Iterator::valid()
103
     *
104
     * @return boolean
105
     */
106
    public function valid()
107
    {
108
        return isset($this->elements[$this->position]);
109
    }
110
111
    /**
112
     * @see \JsonSerializable::jsonSerialize()
113
     *
114
     * @return array
115
     */
116
    public function jsonSerialize()
117
    {
118
        return $this->elements;
119
    }
120
121
    /**
122
     * @see \Countable::count()
123
     *
124
     * @return integer
125
     */
126
    public function count()
127
    {
128
        return count($this->elements);
129
    }
130
131
    /**
132
     * @see CollectionInterface::clear()
133
     *
134
     * @return Bag
135
     */
136
    public function clear()
137
    {
138
        $this->elements = [];
139
        return $this;
140
    }
141
}
142