Completed
Push — develop ( 11971b...45f15f )
by Nate
06:59
created

ElementAttributeTrait::resolveElementValue()   A

Complexity

Conditions 5
Paths 9

Size

Total Lines 16

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 9.4222
c 0
b 0
f 0
cc 5
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\craft\ember\queries;
10
11
use Craft;
12
use craft\base\ElementInterface;
13
use flipbox\craft\ember\helpers\QueryHelper;
14
15
/**
16
 * @author Flipbox Factory <[email protected]>
17
 * @since 2.0.0
18
 */
19
trait ElementAttributeTrait
20
{
21
    /**
22
     * The element(s) that the resulting organizations’ elements must have.
23
     *
24
     * @var string|string[]|int|int[]|ElementInterface|ElementInterface[]|null
25
     */
26
    public $element;
27
28
    /**
29
     * @param string|string[]|int|int[]|ElementInterface|ElementInterface[]|null $value
30
     * @return static The query object
31
     */
32
    public function setElement($value)
33
    {
34
        $this->element = $value;
35
        return $this;
36
    }
37
38
    /**
39
     * @param string|string[]|int|int[]|ElementInterface|ElementInterface[]|null $value
40
     * @return static The query object
41
     */
42
    public function element($value)
43
    {
44
        return $this->setElement($value);
45
    }
46
47
    /**
48
     * @param string|string[]|int|int[]|ElementInterface|ElementInterface[]|null $value
49
     * @return static The query object
50
     */
51
    public function setElementId($value)
52
    {
53
        return $this->setElement($value);
54
    }
55
56
    /**
57
     * @param string|string[]|int|int[]|ElementInterface|ElementInterface[]|null $value
58
     * @return static The query object
59
     */
60
    public function elementId($value)
61
    {
62
        return $this->setElement($value);
63
    }
64
65
    /**
66
     * @param $value
67
     * @param string $join
68
     * @return array
69
     */
70
    protected function parseElementValue($value, string $join = 'or'): array
71
    {
72
        if (false === QueryHelper::parseBaseParam($value, $join)) {
73
            foreach ($value as $operator => &$v) {
74
                $this->resolveElementValue($operator, $v);
75
            }
76
        }
77
78
        // Filter null and empties
79
        $value = array_filter($value, function ($arr): bool {
80
            return $arr !== null && $arr !== '';
81
        });
82
83
        if (empty($value)) {
84
            return [];
85
        }
86
87
        return array_merge([$join], $value);
88
    }
89
90
    /**
91
     * @param $operator
92
     * @param $value
93
     */
94
    protected function resolveElementValue($operator, &$value)
95
    {
96
        if (false === QueryHelper::findParamValue($value, $operator)) {
97
            if (is_string($value)) {
98
                $value = $this->resolveElementStringValue($value);
99
            }
100
101
            if ($value instanceof ElementInterface) {
102
                $value = $value->getId();
103
            }
104
105
            if ($value) {
106
                $value = QueryHelper::assembleParamValue($value, $operator);
107
            }
108
        }
109
    }
110
111
    /**
112
     * @param string $value
113
     * @return int|null
114
     */
115
    protected function resolveElementStringValue(string $value)
116
    {
117
        if (!$element = Craft::$app->getElements()->getElementByUri($value)) {
118
            return null;
119
        }
120
        return $element->getId();
121
    }
122
}
123