forked from solariumphp/solarium
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComponentAwareQueryTrait.php
More file actions
162 lines (142 loc) · 4.3 KB
/
ComponentAwareQueryTrait.php
File metadata and controls
162 lines (142 loc) · 4.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
/*
* This file is part of the Solarium package.
*
* For the full copyright and license information, please view the COPYING
* file that was distributed with this source code.
*/
namespace Solarium\Component;
use Solarium\Exception\OutOfBoundsException;
/**
* Trait query types supporting components.
*/
trait ComponentAwareQueryTrait
{
/**
* Search components.
*
* @var AbstractComponent[]
*/
protected array $components = [];
/**
* Default select query component types.
*/
protected array $componentTypes = [];
/**
* Get all registered component types.
*
* @return array<self::COMPONENT_*|string> An array of self::COMPONENT_* and/or self-registered keys
*/
public function getComponentTypes(): array
{
return $this->componentTypes;
}
/**
* Register a component type.
*
* @param string $key
* @param string $component
*
* @return self Provides fluent interface
*/
public function registerComponentType(string $key, string $component): self
{
$this->componentTypes[$key] = $component;
return $this;
}
/**
* Get all registered components.
*
* @return AbstractComponent[]
*/
public function getComponents(): array
{
return $this->components;
}
/**
* Get a component instance by key.
*
* You can optionally supply an autoload class to create a new component
* instance if there is no registered component for the given key yet.
*
* @param self::COMPONENT_*|string $key A self::COMPONENT_* or self-registered key
* @param bool $autoload Autoload if component needs to be created
* @param array|null $config Configuration to use for autoload
*
* @throws OutOfBoundsException
*
* @return AbstractComponent|null
*/
public function getComponent(string $key, ?bool $autoload = false, ?array $config = null): ?AbstractComponent
{
if (isset($this->components[$key])) {
return $this->components[$key];
}
if (true === $autoload) {
if (!isset($this->componentTypes[$key])) {
throw new OutOfBoundsException(sprintf('Cannot autoload unknown component: %s', $key));
}
$className = $this->componentTypes[$key];
$className = class_exists($className) ? $className : $className.strrchr($className, '\\');
$component = new $className($config);
$this->setComponent($key, $component);
return $component;
}
return null;
}
/**
* Set a component instance.
*
* This overwrites any existing component registered with the same key.
*
* @param self::COMPONENT_*|string $key A self::COMPONENT_* or self-registered key
* @param AbstractComponent $component
*
* @return self Provides fluent interface
*/
public function setComponent(string $key, AbstractComponent $component): self
{
$component->setQueryInstance($this);
$this->components[$key] = $component;
return $this;
}
/**
* Remove a component instance.
*
* You can remove a component by passing its key or the component instance.
*
* @param self::COMPONENT_*|string|AbstractComponent $component
*
* @return self Provides fluent interface
*/
public function removeComponent(string|AbstractComponent $component): self
{
if (\is_object($component)) {
foreach ($this->components as $key => $instance) {
if ($instance === $component) {
unset($this->components[$key]);
break;
}
}
} else {
if (isset($this->components[$component])) {
unset($this->components[$component]);
}
}
return $this;
}
/**
* Build component instances based on config.
*
* @param array $configs
*
* @return self Provides fluent interface
*/
protected function createComponents(array $configs): self
{
foreach ($configs as $type => $config) {
$this->getComponent($type, true, $config);
}
return $this;
}
}