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 FIFO data structure. The first element added
12: * to the queue will be the first one to be removed.
13: */
14: class Queue extends LinearDataStructure
15: {
16: /**
17: * Inserts element at the end of the queue
18: * @param mixed $element
19: * @throws \BN\Collections\NullArgumentException
20: */
21: public function push($element)
22: {
23: parent::checkIfElementIsNotNull($element);
24: array_push($this->data, $element);
25: }
26:
27: /**
28: * Removes and returns first element of the queue.
29: * Returns null if the queue 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 queue (element is not removed from the queue).
42: * Returns null if the queue 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: /**
54: * Clones current queue
55: * @return \BN\Collections\Queue
56: */
57: public function __clone()
58: {
59: $queue = new Queue();
60: $queue->data = $this->data;
61: return $queue;
62: }
63: }
64: