|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the Cubiche package. |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright (c) Cubiche |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
namespace Cubiche\Core\Collections\ArrayCollection; |
|
11
|
|
|
|
|
12
|
|
|
use Cubiche\Core\Collections\CollectionInterface; |
|
13
|
|
|
use Cubiche\Core\Collections\DataSource\ArrayDataSource; |
|
14
|
|
|
use Cubiche\Core\Collections\DataSourceHashMap; |
|
15
|
|
|
use Cubiche\Core\Comparable\Comparator; |
|
16
|
|
|
use Cubiche\Core\Comparable\ComparatorInterface; |
|
17
|
|
|
use Cubiche\Core\Specification\SpecificationInterface; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* ArrayHashMap Class. |
|
21
|
|
|
* |
|
22
|
|
|
* @author Karel Osorio RamÃrez <[email protected]> |
|
23
|
|
|
* @author Ivannis Suárez Jerez <[email protected]> |
|
24
|
|
|
*/ |
|
25
|
|
|
class ArrayHashMap extends ArrayCollection implements ArrayHashMapInterface |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* ArrayHashMap constructor. |
|
29
|
|
|
* |
|
30
|
|
|
* @param array $elements |
|
31
|
|
|
*/ |
|
32
|
|
|
public function __construct(array $elements = array()) |
|
33
|
|
|
{ |
|
34
|
|
|
foreach ($elements as $key => $element) { |
|
35
|
|
|
$this->set($key, $element); |
|
36
|
|
|
} |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* {@inheritdoc} |
|
41
|
|
|
*/ |
|
42
|
|
|
public function get($key) |
|
43
|
|
|
{ |
|
44
|
|
|
if ($this->containsKey($key)) { |
|
45
|
|
|
return $this->elements[$key]; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
return; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* {@inheritdoc} |
|
53
|
|
|
*/ |
|
54
|
|
|
public function set($key, $value) |
|
55
|
|
|
{ |
|
56
|
|
|
$this->validateKey($key); |
|
57
|
|
|
|
|
58
|
|
|
$this->elements[$key] = $value; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* {@inheritdoc} |
|
63
|
|
|
*/ |
|
64
|
|
|
public function containsKey($key) |
|
65
|
|
|
{ |
|
66
|
|
|
return $this->hasKey($key); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* {@inheritdoc} |
|
71
|
|
|
*/ |
|
72
|
|
|
public function containsValue($value) |
|
73
|
|
|
{ |
|
74
|
|
|
return $this->values()->contains($value); |
|
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* {@inheritdoc} |
|
79
|
|
|
*/ |
|
80
|
|
|
public function removeAt($key) |
|
81
|
|
|
{ |
|
82
|
|
|
if ($this->containsKey($key)) { |
|
83
|
|
|
$removed = $this->elements[$key]; |
|
84
|
|
|
unset($this->elements[$key]); |
|
85
|
|
|
|
|
86
|
|
|
return $removed; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* {@inheritdoc} |
|
94
|
|
|
*/ |
|
95
|
|
|
public function find(SpecificationInterface $criteria) |
|
96
|
|
|
{ |
|
97
|
|
|
return new DataSourceHashMap(new ArrayDataSource($this->elements, $criteria)); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* {@inheritdoc} |
|
102
|
|
|
*/ |
|
103
|
|
|
public function keys() |
|
104
|
|
|
{ |
|
105
|
|
|
return new ArraySet(\array_keys($this->elements)); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* {@inheritdoc} |
|
110
|
|
|
*/ |
|
111
|
|
|
public function values() |
|
112
|
|
|
{ |
|
113
|
|
|
return new ArrayList(\array_values($this->elements)); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* {@inheritdoc} |
|
118
|
|
|
*/ |
|
119
|
|
View Code Duplication |
public function sort(ComparatorInterface $criteria = null) |
|
|
|
|
|
|
120
|
|
|
{ |
|
121
|
|
|
if ($criteria === null) { |
|
122
|
|
|
$criteria = new Comparator(); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
uksort($this->elements, function ($a, $b) use ($criteria) { |
|
126
|
|
|
return $criteria->compare($a, $b); |
|
127
|
|
|
}); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @param ComparatorInterface $criteria |
|
132
|
|
|
* |
|
133
|
|
|
* @return CollectionInterface |
|
134
|
|
|
*/ |
|
135
|
|
|
public function sorted(ComparatorInterface $criteria) |
|
136
|
|
|
{ |
|
137
|
|
|
return new DataSourceHashMap(new ArrayDataSource($this->elements, null, $criteria)); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* {@inheritdoc} |
|
142
|
|
|
*/ |
|
143
|
|
|
public function offsetUnset($offset) |
|
144
|
|
|
{ |
|
145
|
|
|
return $this->removeAt($offset); |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: