Completed
Push — master ( 05d808...8c0ee8 )
by Nate
11:51 queued 10:09
created

ElementAttribute::resolveElementValue()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 0
cts 14
cp 0
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 8
nc 9
nop 2
crap 30
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://github.com/flipboxfactory/craft-ember/blob/master/LICENSE
6
 * @link       https://github.com/flipboxfactory/craft-ember
7
 */
8
9
namespace flipbox\ember\db\traits;
10
11
use Craft;
12
use craft\base\ElementInterface;
13
use flipbox\ember\helpers\ArrayHelper;
14
use flipbox\ember\helpers\QueryHelper;
15
16
/**
17
 * @author Flipbox Factory <[email protected]>
18
 * @since 1.0.0
19
 */
20
trait ElementAttribute
21
{
22
    /**
23
     * The element(s) that the resulting organizations’ elements must have.
24
     *
25
     * @var string|string[]|int|int[]|ElementInterface|ElementInterface[]|null
26
     */
27
    public $element;
28
29
    /**
30
     * @param string|string[]|int|int[]|ElementInterface|ElementInterface[]|null $value
31
     * @return static The query object
32
     */
33
    public function setElement($value)
34
    {
35
        $this->element = $value;
36
        return $this;
37
    }
38
39
    /**
40
     * @param string|string[]|int|int[]|ElementInterface|ElementInterface[]|null $value
41
     * @return static The query object
42
     */
43
    public function element($value)
44
    {
45
        return $this->setElement($value);
46
    }
47
48
    /**
49
     * @param string|string[]|int|int[]|ElementInterface|ElementInterface[]|null $value
50
     * @return static The query object
51
     */
52
    public function setElementId($value)
53
    {
54
        return $this->setElement($value);
55
    }
56
57
    /**
58
     * @param string|string[]|int|int[]|ElementInterface|ElementInterface[]|null $value
59
     * @return static The query object
60
     */
61
    public function elementId($value)
62
    {
63
        return $this->setElement($value);
64
    }
65
66
    /**
67
     * @param $value
68
     * @param string $join
69
     * @return array
70
     */
71
    public function parseElementValue($value, string $join = 'or'): array
72
    {
73
        if (false === QueryHelper::parseBaseParam($value, $join)) {
74
            foreach ($value as $operator => &$v) {
75
                $this->resolveElementValue($operator, $v);
76
            }
77
        }
78
79
        $value = ArrayHelper::filterEmptyAndNullValuesFromArray($value);
80
81
        if (empty($value)) {
82
            return [];
83
        }
84
85
        return array_merge([$join], $value);
86
    }
87
88
    /**
89
     * @param $operator
90
     * @param $value
91
     */
92
    protected function resolveElementValue($operator, &$value)
93
    {
94
        if (false === QueryHelper::findParamValue($value, $operator)) {
95
            if (is_string($value)) {
96
                $value = $this->resolveElementStringValue($value);
97
            }
98
99
            if ($value instanceof ElementInterface) {
100
                $value = $value->getId();
101
            }
102
103
            if ($value) {
104
                $value = QueryHelper::assembleParamValue($value, $operator);
105
            }
106
        }
107
    }
108
109
    /**
110
     * @param string $value
111
     * @return int|null
112
     */
113
    protected function resolveElementStringValue(string $value)
114
    {
115
        if (!$element = Craft::$app->getElements()->getElementByUri($value)) {
116
            return null;
117
        }
118
        return $element->getId();
119
    }
120
}
121