Completed
Branch develop (17e8cf)
by Nate
10:01
created

NormalizeValueTrait::normalizeQuery()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 28
ccs 0
cts 24
cp 0
rs 9.1608
c 0
b 0
f 0
cc 5
nc 8
nop 2
crap 30
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://github.com/flipboxfactory/craft-integration/blob/master/LICENSE
6
 * @link       https://github.com/flipboxfactory/craft-integration/
7
 */
8
9
namespace flipbox\craft\element\lists\fields;
10
11
use craft\base\Element;
12
use craft\base\ElementInterface;
13
use craft\elements\db\ElementQuery;
14
use craft\helpers\StringHelper;
15
use flipbox\craft\element\lists\records\Association;
16
use flipbox\craft\ember\helpers\SiteHelper;
17
18
/**
19
 * @author Flipbox Factory <[email protected]>
20
 * @since 2.0.0
21
 *
22
 * @property int|null $max
23
 */
24
trait NormalizeValueTrait
25
{
26
    abstract protected static function elementType(): string;
27
28
    /**
29
     * @param $value
30
     * @param ElementInterface|null $element
31
     * @return ElementQuery
32
     */
33
    public function normalizeValue(
34
        $value,
35
        ElementInterface $element = null
36
    ) {
37
        if ($value instanceof ElementQuery) {
38
            return $value;
39
        }
40
41
        /** @var Element $elementType */
42
        $elementType = static::elementType();
43
44
        /** @var ElementQuery $query */
45
        $query = $elementType::find();
46
47
        if ($this->allowLimit && $this->limit) {
0 ignored issues
show
Bug introduced by
The property allowLimit does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
48
            $query->limit($this->limit);
0 ignored issues
show
Bug introduced by
The property limit does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
49
        }
50
51
        $this->normalizeQueryValue($query, $value, $element);
52
        return $query;
53
    }
54
55
    /**
56
     * @param ElementQuery $query
57
     * @param ElementInterface|null $element
58
     */
59
    protected function normalizeQuery(
60
        ElementQuery $query,
61
        ElementInterface $element = null
62
    ) {
63
        /** @var Element|null $element */
64
65
        $source = ($element !== null && $element->getId() !== null ? $element->getId() : false);
66
67
        if ($source !== false) {
68
            $name = Association::tableName();
69
            $alias = Association::tableAlias();
70
71
            $query->innerJoin(
72
                $name . ' ' . $alias,
73
                [
74
                    'and',
75
                    '[[' . $alias . '.targetId]] = [[elements.id]]',
76
                    [
77
                        $alias . '.sourceId' => $source,
78
                        $alias . '.fieldId' => $this->id,
0 ignored issues
show
Bug introduced by
The property id does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
79
                        $alias . '.siteId' => SiteHelper::ensureSiteId($element === null ? null : $element->siteId)
80
                    ]
81
                ]
82
            );
83
        } else {
84
            $query->id(false);
85
        }
86
    }
87
88
    /**
89
     * @param ElementQuery $query
90
     * @param $value
91
     * @param ElementInterface|null $element
92
     */
93
    protected function normalizeQueryValue(
94
        ElementQuery $query,
95
        $value,
96
        ElementInterface $element = null
97
    ) {
98
        $this->normalizeQuery($query, $element);
99
100
        if (is_array($value)) {
101
            $this->normalizeQueryInputValues($query, $value, $element);
102
            return;
103
        }
104
105
        if ($value === '') {
106
            $this->normalizeQueryEmptyValue($query);
107
            return;
108
        }
109
    }
110
111
    /**
112
     * @param ElementQuery $query
113
     * @param array $value
114
     * @param ElementInterface|null $element
115
     */
116
    protected function normalizeQueryInputValues(
117
        ElementQuery $query,
118
        array $value,
119
        ElementInterface $element = null
120
    ) {
121
        $models = [];
122
        $sortOrder = 1;
123
        foreach ($value as $val) {
124
            $models[] = $this->normalizeQueryInputValue($val, $sortOrder, $element);
125
        }
126
        $query->setCachedResult($models);
127
    }
128
129
    /**
130
     * @param $value
131
     * @param int $sortOrder
132
     * @param ElementInterface|Element|null $element
133
     * @return Association
134
     */
135
    protected function normalizeQueryInputValue(
136
        $value,
137
        int &$sortOrder,
138
        ElementInterface $element = null
139
    ): Association {
140
141
        if (is_array($value)) {
142
            $value = StringHelper::toString($value);
143
        }
144
145
        return new Association([
146
            'fieldId' => $this->id,
147
            'targetId' => $value,
148
            'sourceId' => $element->getId(),
0 ignored issues
show
Bug introduced by
It seems like $element is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
149
            'siteId' => SiteHelper::ensureSiteId($element === null ? null : $element->siteId),
150
            'sortOrder' => $sortOrder++
151
        ]);
152
    }
153
154
    /**
155
     * @param ElementQuery $query
156
     */
157
    protected function normalizeQueryEmptyValue(
158
        ElementQuery $query
159
    ) {
160
        $query->setCachedResult([]);
161
    }
162
}
163