1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Firesphere\SolrSearch\Helpers; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Dev\Debug; |
6
|
|
|
use SilverStripe\ORM\DataObject; |
7
|
|
|
use SilverStripe\ORM\FieldType\DBField; |
8
|
|
|
use SilverStripe\ORM\SS_List; |
9
|
|
|
use SilverStripe\View\ArrayData; |
10
|
|
|
|
11
|
|
|
class Columnar |
12
|
|
|
{ |
13
|
|
|
protected $component; |
14
|
|
|
protected $columns = []; |
15
|
|
|
protected $columnName = ''; |
16
|
|
|
|
17
|
|
|
public function __construct($component, $columns = []) |
18
|
|
|
{ |
19
|
|
|
$this->component = $component; |
20
|
|
|
$this->columns = $columns; |
21
|
|
|
$this->columnName = $this->columns ? array_shift($this->columns) : null; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public static function make($component, $columns) |
25
|
|
|
{ |
26
|
|
|
if (!is_array($columns)) { |
27
|
|
|
$columns = array_filter(explode('.', $columns)); |
28
|
|
|
} |
29
|
|
|
return new self($component, $columns); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param DataObject|ArrayData|SS_List $obj |
34
|
|
|
* @param mixed $columns |
35
|
|
|
* |
36
|
|
|
* @return null |
37
|
|
|
*/ |
38
|
|
|
public static function identify($obj, $columns) |
39
|
|
|
{ |
40
|
|
|
$instance = self::make($obj, $columns); |
41
|
|
|
if ($obj instanceof DataObject || $obj instanceof ArrayData) { |
42
|
|
|
return $instance->resolveData(); |
43
|
|
|
} elseif ($obj instanceof SS_List) { |
|
|
|
|
44
|
|
|
return $instance->resolveList(); |
45
|
|
|
} elseif ($obj instanceof DBField) { |
46
|
|
|
return $instance->resolveField(); |
47
|
|
|
} elseif (is_array($obj)) { |
48
|
|
|
return $obj; |
49
|
|
|
} else { |
50
|
|
|
return null; |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function resolveField() |
55
|
|
|
{ |
56
|
|
|
if ($this->columnName) { |
57
|
|
|
$method = $this->columnName; |
58
|
|
|
return $this->component->$method(); |
59
|
|
|
} |
60
|
|
|
return $this->component->dataValue(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function resolveData() |
64
|
|
|
{ |
65
|
|
|
$data = null; |
66
|
|
|
if (empty($this->columnName)) { |
67
|
|
|
return $this->component->toMap(); |
68
|
|
|
} |
69
|
|
|
// Inspect component for element $relation |
70
|
|
|
if ($this->component->hasMethod($this->columnName)) { |
71
|
|
|
$relation = $this->columnName; |
72
|
|
|
return self::identify($this->component->$relation(), $this->columns); |
|
|
|
|
73
|
|
|
} |
74
|
|
|
// Inspect component has attribute |
75
|
|
|
if ($this->component->hasField($this->columnName)) { |
76
|
|
|
$data = $this->component->{$this->columnName}; |
77
|
|
|
$dbObject = $this->component->dbObject($this->columnName); |
78
|
|
|
if ($dbObject && $this->columns) { |
79
|
|
|
$dbObject->setValue($data); |
80
|
|
|
return self::identify($dbObject, $this->columns); |
|
|
|
|
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
if (!is_scalar($data)) { |
84
|
|
|
return self::identify($data, $this->columns); |
|
|
|
|
85
|
|
|
} |
86
|
|
|
return $data; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function resolveList() |
90
|
|
|
{ |
91
|
|
|
if (empty($this->columnName)) { |
92
|
|
|
return $this->component->toNestedArray(); |
93
|
|
|
} |
94
|
|
|
// Inspect $component for element $relation |
95
|
|
|
if ($this->component->hasMethod($this->columnName)) { |
96
|
|
|
$relation = $this->columnName; |
97
|
|
|
return self::identify($this->component->$relation(), $this->columns); |
|
|
|
|
98
|
|
|
} |
99
|
|
|
return null; |
100
|
|
|
} |
101
|
|
|
} |