Completed
Push — master ( 11971b...eeafb3 )
by Nate
01:16
created

ElementAttributeTrait   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 2
dl 0
loc 100
ccs 0
cts 50
cp 0
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setElement() 0 5 1
A element() 0 4 1
A setElementId() 0 4 1
A elementId() 0 4 1
A parseElementValue() 0 19 5
A resolveElementValue() 0 12 4
A resolveElementStringValue() 0 7 2
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) {
102
                $value = QueryHelper::assembleParamValue($value, $operator);
103
            }
104
        }
105
    }
106
107
    /**
108
     * @param string $value
109
     * @return int|null
110
     */
111
    protected function resolveElementStringValue(string $value)
112
    {
113
        if (!$element = Craft::$app->getElements()->getElementByUri($value)) {
114
            return null;
115
        }
116
        return $element->getId();
117
    }
118
}
119