1: <?php
2: /**
3: * BN-PHP (https://bitbucket.org/zdenekdrahos/bn-php)
4: * @license New BSD License
5: * @author Zdenek Drahos
6: */
7:
8: namespace BN\Collections;
9:
10: /**
11: * Class represeting LIFO data structure. The last element added
12: * to the stack will be the first one to be removed.
13: */
14: class Stack extends LinearDataStructure
15: {
16: /**
17: * Inserts element to the beggining of the stack
18: * @param mixed $element
19: * @throws \BN\Collections\NullArgumentException
20: */
21: public function push($element)
22: {
23: parent::checkIfElementIsNotNull($element);
24: array_unshift($this->data, $element);
25: }
26:
27: /**
28: * Removes and returns first element of the stack.
29: * Returns null if the stack is empty
30: * @return mixed
31: */
32: public function pop()
33: {
34: if (!$this->isEmpty()) {
35: return array_shift($this->data);
36: }
37: return null;
38: }
39:
40: /**
41: * Returns first element of the stack (element is not removed from the stack).
42: * Returns null if the stack is empty
43: * @return mixed
44: */
45: public function peek()
46: {
47: if (!$this->isEmpty()) {
48: return $this->data[0];
49: }
50: return null;
51: }
52: }
53: