|
1
|
|
|
<?php namespace GenericCollections\Traits; |
|
2
|
|
|
|
|
3
|
|
|
use GenericCollections\Internal\DoubleLinkedList; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* This trait include all deque standard methods to queue, stack and deque |
|
7
|
|
|
* |
|
8
|
|
|
* @property DoubleLinkedList $storage |
|
9
|
|
|
* |
|
10
|
|
|
* @package GenericCollections\Traits |
|
11
|
|
|
*/ |
|
12
|
|
|
trait DequeCommonMethods |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* Protected method to do the checks for add and offer methods |
|
16
|
|
|
* |
|
17
|
|
|
* @param $element |
|
18
|
|
|
* @return string The reason why this would produce an error |
|
19
|
|
|
*/ |
|
20
|
216 |
|
private function checkAddOffer($element) |
|
21
|
|
|
{ |
|
22
|
|
|
// always throw an exception if element is null and container does not allow nulls |
|
23
|
216 |
|
if (! $this->optionAllowNullMembers() && is_null($element)) { |
|
|
|
|
|
|
24
|
24 |
|
throw new \InvalidArgumentException( |
|
25
|
24 |
|
'The ' . $this->containerInternalName() . ' does not allow null elements' |
|
|
|
|
|
|
26
|
24 |
|
); |
|
27
|
|
|
} |
|
28
|
|
|
// always throw an exception if element is not the correct type |
|
29
|
192 |
View Code Duplication |
if (! $this->checkElementType($element)) { |
|
|
|
|
|
|
30
|
18 |
|
throw new \InvalidArgumentException( |
|
31
|
|
|
'Invalid element type;' |
|
32
|
18 |
|
. ' the ' . $this->containerInternalName() . ' ' . get_class($this) |
|
|
|
|
|
|
33
|
18 |
|
. ' was expecting a ' . $this->getElementType() . ' type' |
|
|
|
|
|
|
34
|
18 |
|
); |
|
35
|
|
|
} |
|
36
|
|
|
// return the error message, add will throw an exception, offer will return false |
|
37
|
174 |
|
if ($this->optionUniqueValues() && $this->contains($element)) { |
|
|
|
|
|
|
38
|
18 |
|
return 'The ' . $this->containerInternalName() . ' does not allow duplicated elements'; |
|
|
|
|
|
|
39
|
|
|
} |
|
40
|
|
|
// no error found |
|
41
|
174 |
|
return ''; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
36 |
View Code Duplication |
public function addFirst($element) |
|
|
|
|
|
|
45
|
|
|
{ |
|
46
|
36 |
|
if ('' !== $errorMessage = $this->checkAddOffer($element)) { |
|
47
|
3 |
|
throw new \InvalidArgumentException($errorMessage); |
|
48
|
|
|
} |
|
49
|
24 |
|
$this->storage->unshift($element); |
|
50
|
24 |
|
} |
|
51
|
|
|
|
|
52
|
156 |
View Code Duplication |
public function addLast($element) |
|
|
|
|
|
|
53
|
|
|
{ |
|
54
|
156 |
|
if ('' !== $errorMessage = $this->checkAddOffer($element)) { |
|
55
|
6 |
|
throw new \InvalidArgumentException($errorMessage); |
|
56
|
|
|
} |
|
57
|
141 |
|
$this->storage->push($element); |
|
58
|
141 |
|
} |
|
59
|
|
|
|
|
60
|
21 |
View Code Duplication |
public function offerFirst($element) |
|
|
|
|
|
|
61
|
|
|
{ |
|
62
|
21 |
|
if ('' !== $this->checkAddOffer($element)) { |
|
63
|
3 |
|
return false; |
|
64
|
|
|
} |
|
65
|
12 |
|
$this->storage->unshift($element); |
|
66
|
12 |
|
return true; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
39 |
View Code Duplication |
public function offerLast($element) |
|
|
|
|
|
|
70
|
|
|
{ |
|
71
|
39 |
|
if ('' !== $this->checkAddOffer($element)) { |
|
72
|
6 |
|
return false; |
|
73
|
|
|
} |
|
74
|
21 |
|
$this->storage->push($element); |
|
75
|
21 |
|
return true; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
24 |
|
public function getFirst() |
|
79
|
|
|
{ |
|
80
|
24 |
|
if ($this->isEmpty()) { |
|
|
|
|
|
|
81
|
6 |
|
throw new \LogicException('Can not get an element from an empty ' . $this->containerInternalName()); |
|
|
|
|
|
|
82
|
|
|
} |
|
83
|
18 |
|
return $this->storage->bottom(); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
6 |
|
public function peekFirst() |
|
87
|
|
|
{ |
|
88
|
6 |
|
if ($this->isEmpty()) { |
|
|
|
|
|
|
89
|
6 |
|
return null; |
|
90
|
|
|
} |
|
91
|
6 |
|
return $this->storage->bottom(); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
18 |
|
public function removeFirst() |
|
95
|
|
|
{ |
|
96
|
18 |
|
if ($this->isEmpty()) { |
|
|
|
|
|
|
97
|
6 |
|
throw new \LogicException('Can not remove an element from an empty ' . $this->containerInternalName()); |
|
|
|
|
|
|
98
|
|
|
} |
|
99
|
12 |
|
return $this->storage->shift(); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
12 |
|
public function pollFirst() |
|
103
|
|
|
{ |
|
104
|
12 |
|
if ($this->isEmpty()) { |
|
|
|
|
|
|
105
|
6 |
|
return null; |
|
106
|
|
|
} |
|
107
|
12 |
|
return $this->storage->shift(); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
27 |
|
public function contains($element) |
|
111
|
|
|
{ |
|
112
|
27 |
|
return $this->storage->contains($element); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
243 |
|
protected function createStorageObject() |
|
116
|
|
|
{ |
|
117
|
243 |
|
$this->storage = new DoubleLinkedList(); |
|
118
|
243 |
|
if (! $this->optionComparisonIsIdentical()) { |
|
|
|
|
|
|
119
|
6 |
|
$this->storage->strictComparisonOff(); |
|
120
|
6 |
|
} |
|
121
|
243 |
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.