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
|
|
|
protected function resolveArrayData() |
41
|
|
|
{ |
42
|
|
|
if (empty($this->columnName)) { |
43
|
|
|
return $this->component->toMap(); |
|
|
|
|
44
|
|
|
} |
45
|
|
|
// Inspect component has attribute |
46
|
|
|
if (empty($this->columns) && $this->component->hasField($this->columnName)) { |
|
|
|
|
47
|
|
|
return $this->component->{$this->columnName}; |
48
|
|
|
} |
49
|
|
|
$this->cannotIdentifyException($this->component, array_merge([$this->columnName], $this->columns)); |
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Resolves a DataList values |
54
|
|
|
* @return array|mixed |
55
|
|
|
* @throws LogicException |
56
|
|
|
*/ |
57
|
|
|
protected function resolveList() |
58
|
|
|
{ |
59
|
|
|
if (empty($this->columnName)) { |
60
|
|
|
return $this->component->toNestedArray(); |
61
|
|
|
} |
62
|
|
|
// Inspect $component for element $relation |
63
|
|
|
if ($this->component->hasMethod($this->columnName)) { |
|
|
|
|
64
|
|
|
$relation = $this->columnName; |
65
|
|
|
|
66
|
|
|
return self::identify($this->component->$relation(), $this->columns); |
67
|
|
|
} |
68
|
|
|
$data = []; |
69
|
|
|
array_unshift($this->columns, $this->columnName); |
70
|
|
|
foreach ($this->component as $component) { |
71
|
|
|
$data[] = self::identify($component, $this->columns); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $data; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Resolves a Single field in the database. |
79
|
|
|
* @return mixed |
80
|
|
|
* @throws LogicException |
81
|
|
|
*/ |
82
|
|
|
protected function resolveField() |
83
|
|
|
{ |
84
|
|
|
if ($this->columnName) { |
85
|
|
|
// @todo could be simplified if possible? |
86
|
|
|
if ($this->component->hasMethod($this->columnName)) { |
87
|
|
|
$method = $this->columnName; |
88
|
|
|
} elseif ($this->component->hasMethod("get{$this->columnName}")) { |
89
|
|
|
$method = "get{$this->columnName}"; |
90
|
|
|
} else { |
91
|
|
|
throw new LogicException( |
92
|
|
|
sprintf('Method, "%s" not found on "%s"', $this->columnName, $this->shortName) |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$value = $this->component->$method(); |
97
|
|
|
} else { |
98
|
|
|
$value = $this->component->getValue(); |
|
|
|
|
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
if (!empty($this->columns)) { |
102
|
|
|
$this->cannotIdentifyException($this->component, $this->columns); |
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return $value; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Resolves a DataObject value |
110
|
|
|
* @return mixed |
111
|
|
|
* @throws LogicException |
112
|
|
|
*/ |
113
|
|
|
protected function resolveDataObject() |
114
|
|
|
{ |
115
|
|
|
if (empty($this->columnName)) { |
116
|
|
|
return $this->component->toMap(); |
117
|
|
|
} |
118
|
|
|
// Inspect component for element $relation |
119
|
|
|
if ($this->component->hasMethod($this->columnName)) { |
120
|
|
|
$relation = $this->columnName; |
121
|
|
|
// We hit a direct method that returns a non-object |
122
|
|
|
if (!is_object($this->component->$relation())) { |
123
|
|
|
return $this->component->$relation(); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return self::identify($this->component->$relation(), $this->columns); |
127
|
|
|
} |
128
|
|
|
// Inspect component has attribute |
129
|
|
|
if ($this->component->hasField($this->columnName)) { |
130
|
|
|
$data = $this->component->{$this->columnName}; |
131
|
|
|
$dbObject = $this->component->dbObject($this->columnName); |
|
|
|
|
132
|
|
|
if ($dbObject) { |
133
|
|
|
// @todo do we need to set the value? |
134
|
|
|
$dbObject->setValue($data); |
135
|
|
|
|
136
|
|
|
return self::identify($dbObject, $this->columns); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return $data; |
140
|
|
|
} |
141
|
|
|
$this->cannotIdentifyException($this->component, [$this->columnName]); |
|
|
|
|
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param DataObject|ArrayData|SS_List $component |
146
|
|
|
* @param array $columns |
147
|
|
|
* |
148
|
|
|
* @return void |
149
|
|
|
* @throws LogicException |
150
|
|
|
*/ |
151
|
|
|
abstract protected function cannotIdentifyException($component, $columns = []): void; |
152
|
|
|
} |
153
|
|
|
|