1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Firesphere\SolrSearch\Traits; |
4
|
|
|
|
5
|
|
|
use LogicException; |
6
|
|
|
use SilverStripe\ORM\ArrayList; |
7
|
|
|
use SilverStripe\ORM\DataObject; |
8
|
|
|
use SilverStripe\ORM\FieldType\DBField; |
9
|
|
|
use SilverStripe\ORM\SS_List; |
10
|
|
|
use SilverStripe\View\ArrayData; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Trait ResolveTrait All resolver methods for the DataResolver |
14
|
|
|
* @package Firesphere\SolrSearch\Traits |
15
|
|
|
*/ |
16
|
|
|
trait DataResolveTrait |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var DataObject|ArrayList|SS_List|DBField |
20
|
|
|
*/ |
21
|
|
|
protected $component; |
22
|
|
|
/** |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
protected $columns = []; |
26
|
|
|
/** |
27
|
|
|
* @var mixed|string|null |
28
|
|
|
*/ |
29
|
|
|
protected $columnName = ''; |
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected $shortName; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Resolves an ArrayData value |
37
|
|
|
* @return mixed |
38
|
|
|
* @throws LogicException |
39
|
|
|
*/ |
40
|
4 |
|
protected function resolveArrayData() |
41
|
|
|
{ |
42
|
4 |
|
if (empty($this->columnName)) { |
43
|
1 |
|
return $this->component->toMap(); |
44
|
|
|
} |
45
|
|
|
// Inspect component has attribute |
46
|
3 |
|
if (empty($this->columns) && $this->component->hasField($this->columnName)) { |
47
|
2 |
|
return $this->component->{$this->columnName}; |
48
|
|
|
} |
49
|
1 |
|
$this->cannotIdentifyException($this->component, array_merge([$this->columnName], $this->columns)); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param DataObject|ArrayData|SS_List $component |
54
|
|
|
* @param array $columns |
55
|
|
|
* |
56
|
|
|
* @return void |
57
|
|
|
* @throws LogicException |
58
|
|
|
*/ |
59
|
|
|
abstract protected function cannotIdentifyException($component, $columns = []): void; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Resolves a DataList values |
63
|
|
|
* @return array|mixed |
64
|
|
|
* @throws LogicException |
65
|
|
|
*/ |
66
|
3 |
|
protected function resolveList() |
67
|
|
|
{ |
68
|
3 |
|
if (empty($this->columnName)) { |
69
|
3 |
|
return $this->component->toNestedArray(); |
70
|
|
|
} |
71
|
|
|
// Inspect $component for element $relation |
72
|
2 |
|
if ($this->component->hasMethod($this->columnName)) { |
73
|
2 |
|
$relation = $this->columnName; |
74
|
|
|
|
75
|
2 |
|
return self::identify($this->component->$relation(), $this->columns); |
76
|
|
|
} |
77
|
1 |
|
$data = []; |
78
|
1 |
|
array_unshift($this->columns, $this->columnName); |
79
|
1 |
|
foreach ($this->component as $component) { |
80
|
1 |
|
$data[] = self::identify($component, $this->columns); |
81
|
|
|
} |
82
|
|
|
|
83
|
1 |
|
return $data; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Resolves a Single field in the database. |
88
|
|
|
* @return mixed |
89
|
|
|
* @throws LogicException |
90
|
|
|
*/ |
91
|
8 |
|
protected function resolveField() |
92
|
|
|
{ |
93
|
8 |
|
if ($this->columnName) { |
94
|
|
|
// @todo could be simplified if possible? |
95
|
4 |
|
if ($this->component->hasMethod($this->columnName)) { |
96
|
3 |
|
$method = $this->columnName; |
97
|
2 |
|
} elseif ($this->component->hasMethod("get{$this->columnName}")) { |
98
|
1 |
|
$method = "get{$this->columnName}"; |
99
|
|
|
} else { |
100
|
1 |
|
throw new LogicException( |
101
|
1 |
|
sprintf('Method, "%s" not found on "%s"', $this->columnName, $this->shortName) |
102
|
|
|
); |
103
|
|
|
} |
104
|
|
|
|
105
|
3 |
|
$value = $this->component->$method(); |
106
|
|
|
} else { |
107
|
6 |
|
$value = $this->component->getValue(); |
108
|
|
|
} |
109
|
|
|
|
110
|
7 |
|
if (!empty($this->columns)) { |
111
|
1 |
|
$this->cannotIdentifyException($this->component, $this->columns); |
112
|
|
|
} |
113
|
|
|
|
114
|
6 |
|
return $value; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Resolves a DataObject value |
119
|
|
|
* @return mixed |
120
|
|
|
* @throws LogicException |
121
|
|
|
*/ |
122
|
13 |
|
protected function resolveDataObject() |
123
|
|
|
{ |
124
|
13 |
|
if (empty($this->columnName)) { |
125
|
2 |
|
return $this->component->toMap(); |
126
|
|
|
} |
127
|
|
|
// Inspect component for element $relation |
128
|
12 |
|
if ($this->component->hasMethod($this->columnName)) { |
129
|
2 |
|
return $this->getMethodValue(); |
130
|
|
|
} |
131
|
|
|
// Inspect component has attribute |
132
|
12 |
|
if ($this->component->hasField($this->columnName)) { |
133
|
11 |
|
return $this->getFieldValue(); |
134
|
|
|
} |
135
|
5 |
|
$this->cannotIdentifyException($this->component, [$this->columnName]); |
|
|
|
|
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @return mixed |
140
|
|
|
*/ |
141
|
2 |
|
protected function getMethodValue() |
142
|
|
|
{ |
143
|
2 |
|
$relation = $this->columnName; |
144
|
|
|
// We hit a direct method that returns a non-object |
145
|
2 |
|
if (!is_object($this->component->$relation())) { |
146
|
1 |
|
return $this->component->$relation(); |
147
|
|
|
} |
148
|
|
|
|
149
|
1 |
|
return self::identify($this->component->$relation(), $this->columns); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @return mixed |
154
|
|
|
*/ |
155
|
11 |
|
protected function getFieldValue() |
156
|
|
|
{ |
157
|
11 |
|
$data = $this->component->{$this->columnName}; |
158
|
11 |
|
$dbObject = $this->component->dbObject($this->columnName); |
159
|
11 |
|
if ($dbObject) { |
160
|
|
|
// @todo do we need to set the value? |
161
|
8 |
|
$dbObject->setValue($data); |
162
|
|
|
|
163
|
8 |
|
return self::identify($dbObject, $this->columns); |
164
|
|
|
} |
165
|
|
|
|
166
|
3 |
|
return $data; |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|