Queue::poll()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
namespace phootwork\collection;
3
4
use \Iterator;
5
6
/**
7
 * Represents a Queue
8
 * 
9
 * FIFO - first in first out
10
 * 
11
 * @author Thomas Gossmann
12
 */
13
class Queue extends AbstractList {
14
	
15
	/**
16
	 * Creates a new Queue
17
	 * 
18
	 * @param array|Iterator $collection
19
	 */
20
	public function __construct($collection = []) {
21
		foreach ($collection as $element) {
22
			$this->collection[] = $element;
23
		}
24
	}
25
	
26
	/**
27
	 * Enqueues an element
28
	 * 
29
	 * @param mixed $element
30
	 * @return $this
31
	 */
32
	public function enqueue($element) {
33
		array_unshift($this->collection, $element);
34
		
35
		return $this;
36
	}
37
	
38
	/**
39
	 * Enqueues many elements
40
	 *
41
	 * @param array|Iterator $collection
42
	 * @return $this
43
	 */
44
	public function enqueueAll($collection) {
45
		foreach ($collection as $element) {
46
			$this->enqueue($element);
47
		}
48
	
49
		return $this;
50
	}
51
	
52
	/**
53
	 * Returns the element at the head or null if the queue is empty but doesn't remove that element  
54
	 * 
55
	 * @return mixed
56
	 */
57
	public function peek() {
58
		if ($this->size() > 0) {
59
			return $this->collection[0];
60
		}
61
		
62
		return null;
63
	}
64
	
65
	/**
66
	 * Removes and returns the element at the head or null if the is empty
67
	 * 
68
	 * @return mixed
69
	 */
70
	public function poll() {
71
		return array_shift($this->collection);
72
	}
73
	
74
}