1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the ONGR package. |
5
|
|
|
* |
6
|
|
|
* (c) NFQ Technologies UAB <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace ONGR\ElasticsearchDSL; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Container for named builders. |
16
|
|
|
*/ |
17
|
|
|
class BuilderBag |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var BuilderInterface[] |
21
|
|
|
*/ |
22
|
|
|
private $bag = []; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param BuilderInterface[] $builders |
26
|
|
|
*/ |
27
|
|
|
public function __construct($builders = []) |
28
|
|
|
{ |
29
|
|
|
foreach ($builders as $builder) { |
30
|
|
|
$this->add($builder); |
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Adds a builder. |
36
|
|
|
* |
37
|
|
|
* @param BuilderInterface $builder |
38
|
|
|
* |
39
|
|
|
* @return string |
40
|
|
|
*/ |
41
|
|
|
public function add(BuilderInterface $builder) |
42
|
|
|
{ |
43
|
|
|
if (method_exists($builder, 'getName')) { |
44
|
|
|
$name = $builder->getName(); |
|
|
|
|
45
|
|
|
} else { |
46
|
|
|
$name = bin2hex(random_bytes(30)); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$this->bag[$name] = $builder; |
50
|
|
|
|
51
|
|
|
return $name; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Checks if builder exists by a specific name. |
56
|
|
|
* |
57
|
|
|
* @param string $name Builder name. |
58
|
|
|
* |
59
|
|
|
* @return bool |
60
|
|
|
*/ |
61
|
|
|
public function has($name) |
62
|
|
|
{ |
63
|
|
|
return isset($this->bag[$name]); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Removes a builder by name. |
68
|
|
|
* |
69
|
|
|
* @param string $name Builder name. |
70
|
|
|
*/ |
71
|
|
|
public function remove($name) |
72
|
|
|
{ |
73
|
|
|
unset($this->bag[$name]); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Clears contained builders. |
78
|
|
|
*/ |
79
|
|
|
public function clear() |
80
|
|
|
{ |
81
|
|
|
$this->bag = []; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Returns a builder by name. |
86
|
|
|
* |
87
|
|
|
* @param string $name Builder name. |
88
|
|
|
* |
89
|
|
|
* @return BuilderInterface |
90
|
|
|
*/ |
91
|
|
|
public function get($name) |
92
|
|
|
{ |
93
|
|
|
return $this->bag[$name]; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Returns all builders contained. |
98
|
|
|
* |
99
|
|
|
* @param string|null $type Builder type. |
100
|
|
|
* |
101
|
|
|
* @return BuilderInterface[] |
102
|
|
|
*/ |
103
|
|
|
public function all($type = null) |
104
|
|
|
{ |
105
|
|
|
return array_filter( |
106
|
|
|
$this->bag, |
107
|
|
|
/** @var BuilderInterface $builder */ |
108
|
|
|
function (BuilderInterface $builder) use ($type) { |
109
|
|
|
return $type === null || $builder->getType() == $type; |
110
|
|
|
} |
111
|
|
|
); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* {@inheritdoc} |
116
|
|
|
*/ |
117
|
|
|
public function toArray() |
118
|
|
|
{ |
119
|
|
|
$output = []; |
120
|
|
|
foreach ($this->all() as $builder) { |
121
|
|
|
$output = array_merge($output, $builder->toArray()); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return $output; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
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: