1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Trait DataResolveTrait|Firesphere\SolrSearch\Traits\DataResolveTrait Used to extract methods from |
4
|
|
|
* the {@link \Firesphere\SolrSearch\Helpers\DataResolver} to make the code more readable |
5
|
|
|
* |
6
|
|
|
* @package Firesphere\Search\Backend |
7
|
|
|
* @author Simon `Firesphere` Erkelens; Marco `Sheepy` Hermo |
8
|
|
|
* @copyright Copyright (c) 2018 - now() Firesphere & Sheepy |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Firesphere\SearchBackend\Traits\IntrospectionTraits; |
12
|
|
|
|
13
|
|
|
use LogicException; |
14
|
|
|
use SilverStripe\ORM\ArrayList; |
15
|
|
|
use SilverStripe\ORM\DataObject; |
16
|
|
|
use SilverStripe\ORM\FieldType\DBField; |
17
|
|
|
use SilverStripe\ORM\SS_List; |
18
|
|
|
use SilverStripe\View\ArrayData; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Trait ResolveTrait All resolver methods for the DataResolver |
22
|
|
|
* |
23
|
|
|
* @package Firesphere\Search\Backend |
24
|
|
|
*/ |
25
|
|
|
trait DataResolveTrait |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* Component to resolve |
29
|
|
|
* |
30
|
|
|
* @var DataObject|ArrayList|SS_List|DBField |
31
|
|
|
*/ |
32
|
|
|
protected $component; |
33
|
|
|
/** |
34
|
|
|
* Columns to resolve |
35
|
|
|
* |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
protected $columns = []; |
39
|
|
|
/** |
40
|
|
|
* Column to resolve |
41
|
|
|
* |
42
|
|
|
* @var mixed|string|null |
43
|
|
|
*/ |
44
|
|
|
protected $columnName = ''; |
45
|
|
|
/** |
46
|
|
|
* ShortName of a class |
47
|
|
|
* |
48
|
|
|
* @var string |
49
|
|
|
*/ |
50
|
|
|
protected $shortName; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Resolves an ArrayData value |
54
|
|
|
* |
55
|
|
|
* @return mixed |
56
|
|
|
* @throws LogicException |
57
|
|
|
*/ |
58
|
|
|
protected function resolveArrayData() |
59
|
|
|
{ |
60
|
|
|
if (empty($this->columnName)) { |
61
|
|
|
return $this->component->toMap(); |
|
|
|
|
62
|
|
|
} |
63
|
|
|
// Inspect component has attribute |
64
|
|
|
if (empty($this->columns) && $this->component->hasField($this->columnName)) { |
|
|
|
|
65
|
|
|
return $this->component->{$this->columnName}; |
66
|
|
|
} |
67
|
|
|
$this->cannotIdentifyException($this->component, array_merge([$this->columnName], $this->columns)); |
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Each class using this trait should have a way to throw unidentifiable objects or items |
72
|
|
|
* |
73
|
|
|
* @param DataObject|ArrayData|SS_List $component |
74
|
|
|
* @param array $columns |
75
|
|
|
* |
76
|
|
|
* @return void |
77
|
|
|
* @throws LogicException |
78
|
|
|
*/ |
79
|
|
|
abstract protected function cannotIdentifyException($component, $columns = []): void; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Resolves a DataList values |
83
|
|
|
* |
84
|
|
|
* @return array|mixed |
85
|
|
|
* @throws LogicException |
86
|
|
|
*/ |
87
|
|
|
protected function resolveList() |
88
|
|
|
{ |
89
|
|
|
if (empty($this->columnName)) { |
90
|
|
|
return $this->component->toNestedArray(); |
91
|
|
|
} |
92
|
|
|
// Inspect $component for element $relation |
93
|
|
|
if ($this->component->hasMethod($this->columnName)) { |
|
|
|
|
94
|
|
|
$relation = $this->columnName; |
95
|
|
|
|
96
|
|
|
return self::identify($this->component->$relation(), $this->columns); |
97
|
|
|
} |
98
|
|
|
$data = []; |
99
|
|
|
array_unshift($this->columns, $this->columnName); |
100
|
|
|
foreach ($this->component as $component) { |
101
|
|
|
$data[] = self::identify($component, $this->columns); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $data; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Resolves a Single field in the database. |
109
|
|
|
* |
110
|
|
|
* @return mixed |
111
|
|
|
* @throws LogicException |
112
|
|
|
*/ |
113
|
|
|
protected function resolveField() |
114
|
|
|
{ |
115
|
|
|
if ($this->columnName) { |
116
|
|
|
$method = $this->checkHasMethod(); |
117
|
|
|
|
118
|
|
|
$value = $this->component->$method(); |
119
|
|
|
} else { |
120
|
|
|
$value = $this->component->getValue(); |
|
|
|
|
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
if (!empty($this->columns)) { |
124
|
|
|
$this->cannotIdentifyException($this->component, $this->columns); |
|
|
|
|
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $value; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Check if a component has the method instead of it being a property |
132
|
|
|
* |
133
|
|
|
* @return null|mixed|string |
134
|
|
|
* @throws LogicException |
135
|
|
|
*/ |
136
|
|
|
protected function checkHasMethod() |
137
|
|
|
{ |
138
|
|
|
if ($this->component->hasMethod($this->columnName)) { |
139
|
|
|
$method = $this->columnName; |
140
|
|
|
} elseif ($this->component->hasMethod("get{$this->columnName}")) { |
141
|
|
|
$method = "get{$this->columnName}"; |
142
|
|
|
} else { |
143
|
|
|
throw new LogicException( |
144
|
|
|
sprintf('Method, "%s" not found on "%s"', $this->columnName, $this->shortName) |
145
|
|
|
); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return $method; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Resolves a DataObject value |
153
|
|
|
* |
154
|
|
|
* @return mixed |
155
|
|
|
* @throws LogicException |
156
|
|
|
*/ |
157
|
|
|
protected function resolveDataObject() |
158
|
|
|
{ |
159
|
|
|
if (empty($this->columnName)) { |
160
|
|
|
return $this->component->toMap(); |
161
|
|
|
} |
162
|
|
|
// Inspect component for element $relation |
163
|
|
|
if ($this->component->hasMethod($this->columnName)) { |
164
|
|
|
return $this->getMethodValue(); |
165
|
|
|
} |
166
|
|
|
// Inspect component has attribute |
167
|
|
|
if ($this->component->hasField($this->columnName)) { |
168
|
|
|
return $this->getFieldValue(); |
169
|
|
|
} |
170
|
|
|
$this->cannotIdentifyException($this->component, [$this->columnName]); |
|
|
|
|
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Get the value for a method |
175
|
|
|
* |
176
|
|
|
* @return mixed |
177
|
|
|
*/ |
178
|
|
|
protected function getMethodValue() |
179
|
|
|
{ |
180
|
|
|
$relation = $this->columnName; |
181
|
|
|
// We hit a direct method that returns a non-object |
182
|
|
|
if (!is_object($this->component->$relation())) { |
183
|
|
|
return $this->component->$relation(); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
return self::identify($this->component->$relation(), $this->columns); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Get the value for a field |
191
|
|
|
* |
192
|
|
|
* @return mixed |
193
|
|
|
*/ |
194
|
|
|
protected function getFieldValue() |
195
|
|
|
{ |
196
|
|
|
$data = $this->component->{$this->columnName}; |
197
|
|
|
$dbObject = $this->component->dbObject($this->columnName); |
|
|
|
|
198
|
|
|
if ($dbObject) { |
199
|
|
|
$dbObject->setValue($data); |
200
|
|
|
|
201
|
|
|
return self::identify($dbObject, $this->columns); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
return $data; |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|