Passed
Push — hans/valid-classes ( 1110df...3598c3 )
by Simon
11:18
created

DataResolveTrait::resolveArrayData()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

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