|
1
|
|
|
<?php |
|
2
|
|
|
namespace Nayjest\Grids\Components\Base; |
|
3
|
|
|
|
|
4
|
|
|
use Illuminate\Support\Collection; |
|
5
|
|
|
use Nayjest\Grids\Grid; |
|
6
|
|
|
|
|
7
|
|
|
trait TRegistry |
|
8
|
|
|
{ |
|
9
|
|
|
protected $components; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Returns default child components. |
|
13
|
|
|
* |
|
14
|
|
|
* Override this method. |
|
15
|
|
|
* |
|
16
|
|
|
* @return \Illuminate\Support\Collection|ComponentInterface[]|array |
|
17
|
|
|
*/ |
|
18
|
|
|
protected function getDefaultComponents() |
|
19
|
|
|
{ |
|
20
|
|
|
return []; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Returns child components. |
|
25
|
|
|
* |
|
26
|
|
|
* @return Collection|ComponentInterface[] |
|
27
|
|
|
*/ |
|
28
|
|
|
final public function getComponents() |
|
29
|
|
|
{ |
|
30
|
|
|
if ($this->components === null) { |
|
31
|
|
|
$this->setComponents($this->getDefaultComponents()); |
|
32
|
|
|
} |
|
33
|
|
|
return $this->components; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Finds child component by name. |
|
38
|
|
|
* |
|
39
|
|
|
* @param string $name |
|
40
|
|
|
* @return null|ComponentInterface |
|
41
|
|
|
*/ |
|
42
|
|
|
public function getComponentByName($name) |
|
43
|
|
|
{ |
|
44
|
|
|
foreach ($this->getComponents() as $component) { |
|
45
|
|
|
if ($component->getName() === $name) { |
|
46
|
|
|
return $component; |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Finds child component by name recursively. |
|
53
|
|
|
* |
|
54
|
|
|
* @param string $name |
|
55
|
|
|
* @return null|ComponentInterface |
|
56
|
|
|
*/ |
|
57
|
|
|
public function getComponentByNameRecursive($name) |
|
58
|
|
|
{ |
|
59
|
|
|
foreach ($this->getComponents() as $component) { |
|
60
|
|
|
if ($component->getName() === $name) { |
|
61
|
|
|
return $component; |
|
62
|
|
|
} |
|
63
|
|
|
if ($component instanceof TRegistry || $component instanceof RegistryInterface) { |
|
64
|
|
|
if ($res = $component->getComponentByNameRecursive($name)) { |
|
|
|
|
|
|
65
|
|
|
return $res; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
} |
|
70
|
|
|
return null; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param string|string[] $tagNames |
|
75
|
|
|
* @return Collection|ComponentInterface[] |
|
76
|
|
|
*/ |
|
77
|
|
|
public function getTagged($tagNames) |
|
78
|
|
|
{ |
|
79
|
|
|
return $this->getComponents()->filter( |
|
80
|
|
|
function (ComponentInterface $component) use ($tagNames) { |
|
81
|
|
|
return is_array($tagNames) ? $component->hasTags($tagNames) : $component->hasTag($tagNames); |
|
82
|
|
|
} |
|
83
|
|
|
); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Adds component to the collection of child components. |
|
88
|
|
|
* |
|
89
|
|
|
* @param ComponentInterface $component |
|
90
|
|
|
* @return $this |
|
91
|
|
|
*/ |
|
92
|
|
|
public function addComponent(ComponentInterface $component) |
|
93
|
|
|
{ |
|
94
|
|
|
$this->getComponents()->push($component); |
|
95
|
|
|
$component->attachTo($this); |
|
96
|
|
|
return $this; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Allows to specify collection of child components. |
|
101
|
|
|
* |
|
102
|
|
|
* @param \Illuminate\Support\Collection|ComponentInterface[]|array $components |
|
103
|
|
|
* @return $this |
|
104
|
|
|
*/ |
|
105
|
|
|
public function setComponents($components) |
|
106
|
|
|
{ |
|
107
|
|
|
$this->components = Collection::make($components); |
|
108
|
|
|
foreach ($components as $component) { |
|
109
|
|
|
$component->attachTo($this); |
|
110
|
|
|
} |
|
111
|
|
|
return $this; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Adds set of components to the collection of child components. |
|
116
|
|
|
* |
|
117
|
|
|
* @param Collection|\Illuminate\Support\Contracts\ArrayableInterface|array $components |
|
118
|
|
|
* @return $this |
|
119
|
|
|
*/ |
|
120
|
|
|
public function addComponents($components) |
|
121
|
|
|
{ |
|
122
|
|
|
$this->setComponents( |
|
123
|
|
|
$this->getComponents()->merge($components) |
|
124
|
|
|
); |
|
125
|
|
|
return $this; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Creates component, |
|
130
|
|
|
* adds it to child components collection and returns it. |
|
131
|
|
|
* |
|
132
|
|
|
* @param string $class |
|
133
|
|
|
* @return ComponentInterface |
|
134
|
|
|
*/ |
|
135
|
|
|
public function makeComponent($class) |
|
136
|
|
|
{ |
|
137
|
|
|
$component = new $class; |
|
138
|
|
|
$this->addComponent($component); |
|
139
|
|
|
return $component; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Initializes child components. |
|
144
|
|
|
* |
|
145
|
|
|
* @param Grid $grid |
|
146
|
|
|
*/ |
|
147
|
|
|
public function initializeComponents(Grid $grid) |
|
148
|
|
|
{ |
|
149
|
|
|
foreach ($this->getComponents() as $component) { |
|
150
|
|
|
$component->initialize($grid); |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Prepares child components for rendering. |
|
156
|
|
|
*/ |
|
157
|
|
|
public function prepareComponents() |
|
158
|
|
|
{ |
|
159
|
|
|
foreach ($this->getComponents() as $component) { |
|
160
|
|
|
$component->prepare(); |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: