Stack::offsetUnset()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php namespace Mbh\Collection;
2
3
/**
4
 * MBHFramework
5
 *
6
 * @link      https://github.com/MBHFramework/mbh-framework
7
 * @copyright Copyright (c) 2017 Ulises Jeremias Cornejo Fandos
8
 * @license   https://github.com/MBHFramework/mbh-framework/blob/master/LICENSE (MIT License)
9
 */
10
11
use Mbh\Collection\Interfaces\Collection as CollectionInterface;
12
use Mbh\Interfaces\Allocated as AllocatedInterface;
13
use Traversable;
14
use ArrayAccess;
15
use IteratorAggregate;
16
use Error;
17
use OutOfBoundsException;
18
19
/**
20
 * A “last in, first out” or “LIFO” collection that only allows access to the
21
 * value at the top of the structure and iterates in that order, destructively.
22
 *
23
 * @package structures
24
 * @author Ulises Jeremias Cornejo Fandos <[email protected]>
25
 */
26
27
class Stack implements AllocatedInterface, ArrayAccess, CollectionInterface, IteratorAggregate
28
{
29
    use Traits\Collection;
30
31
    const MIN_CAPACITY = 8;
32
33
    /**
34
     * @var FixedArray internal sfa to store values of the stack.
35
     */
36
    private $sfa;
37
38
    /**
39
     * Creates an instance using the values of an array or Traversable object.
40
     *
41
     * @param array|Traversable $values
42
     */
43
    public function __construct($values = [])
44
    {
45
        $this->sfa = FixedArray::empty();
46
47
        $this->pushAll($values);
48
    }
49
50
    /**
51
     * Clear all elements in the Stack
52
     */
53
    public function clear()
54
    {
55
        $this->sfa->clear();
56
    }
57
58
    /**
59
     * @inheritdoc
60
     */
61
    public function copy()
62
    {
63
        return new self($this->sfa);
64
    }
65
66
    /**
67
     * Returns the number of elements in the Stack
68
     *
69
     * @return int
70
     */
71
    public function count(): int
72
    {
73
        return count($this->sfa);
74
    }
75
76
    /**
77
     * Ensures that enough memory is allocated for a specified capacity. This
78
     * potentially reduces the number of reallocations as the size increases.
79
     *
80
     * @param int $capacity The number of values for which capacity should be
81
     *                      allocated. Capacity will stay the same if this value
82
     *                      is less than or equal to the current capacity.
83
     */
84
    public function allocate(int $capacity)
85
    {
86
        $this->sfa->allocate($capacity);
87
    }
88
89
    /**
90
     * Returns the current capacity of the stack.
91
     *
92
     * @return int
93
     */
94
    public function capacity(): int
95
    {
96
        return $this->sfa->capacity();
97
    }
98
99
    /**
100
     * Returns the value at the top of the stack without removing it.
101
     *
102
     * @return mixed
103
     *
104
     * @throws UnderflowException if the stack is empty.
105
     */
106
    public function peek()
107
    {
108
        return $this->sfa->last();
109
    }
110
111
    /**
112
     * Returns and removes the value at the top of the stack.
113
     *
114
     * @return mixed
115
     *
116
     * @throws UnderflowException if the stack is empty.
117
     */
118
    public function pop()
119
    {
120
        return $this->sfa->pop();
121
    }
122
123
    /**
124
     * Pushes zero or more values onto the top of the stack.
125
     *
126
     * @param mixed ...$values
127
     */
128
    public function push(...$values)
129
    {
130
        $this->sfa->push(...$values);
131
    }
132
133
    /**
134
     * Creates associations for all keys and corresponding values of either an
135
     * array or iterable object.
136
     *
137
     * @param Traversable|array $values
138
     */
139
    private function pushAll($values)
140
    {
141
        foreach ($values as &$value) {
142
            $this[] = $value;
143
        }
144
    }
145
146
    /**
147
     * @inheritDoc
148
     */
149
    public function toArray(): array
150
    {
151
        return array_reverse($this->sfa->toArray());
152
    }
153
154
    /**
155
     * @inheritDoc
156
     */
157
    public function unserialize($values)
158
    {
159
        $values = unserialize($values);
160
        $this->sfa = FixedArray::fromArray($values);
161
    }
162
163
    /**
164
     *
165
     */
166
    public function getIterator()
167
    {
168
        while (!$this->isEmpty()) {
169
            yield $this->pop();
170
        }
171
    }
172
173
    /**
174
     * @inheritdoc
175
     *
176
     * @throws OutOfBoundsException
177
     */
178
    public function offsetSet($offset, $value)
179
    {
180
        if ($offset === null) {
181
            $this->push($value);
182
        } else {
183
            throw new OutOfBoundsException();
184
        }
185
    }
186
187
    /**
188
     * @inheritdoc
189
     *
190
     * @throws Error
191
     */
192
    public function offsetGet($offset)
193
    {
194
        throw new Error();
195
    }
196
197
    /**
198
     * @inheritdoc
199
     *
200
     * @throws Error
201
     */
202
    public function offsetUnset($offset)
203
    {
204
        throw new Error();
205
    }
206
207
    /**
208
     * @inheritdoc
209
     *
210
     * @throws Error
211
     */
212
    public function offsetExists($offset)
213
    {
214
        throw new Error();
215
    }
216
}
217