NormalizeValueTrait::normalizeQueryInputValue()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 0
cts 16
cp 0
rs 9.6666
c 0
b 0
f 0
cc 4
nc 2
nop 3
crap 20
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://github.com/flipboxfactory/craft-element-lists/LICENSE
6
 * @link       https://github.com/flipboxfactory/craft-element-lists/
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\elements\db\ElementQueryInterface;
15
use craft\helpers\StringHelper;
16
use flipbox\craft\element\lists\records\Association;
17
use flipbox\craft\element\lists\relationships\Relationship;
18
use flipbox\craft\element\lists\relationships\RelationshipInterface;
19
use flipbox\craft\ember\helpers\SiteHelper;
20
21
/**
22
 * @author Flipbox Factory <[email protected]>
23
 * @since 2.0.0
24
 *
25
 * @property int|null $id
26
 * @property int|null $limit
27
 * @property bool $allowLimit
28
 *
29
 * @mixin RelationalInterface
30
 */
31
trait NormalizeValueTrait
32
{
33
    abstract protected static function elementType(): string;
34
35
    /**
36
     * @param ElementInterface|null $element
37
     * @return ElementQueryInterface
38
     */
39
    public function getQuery(ElementInterface $element = null): ElementQueryInterface
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) {
48
            $query->limit($this->limit);
49
        }
50
51
        $this->normalizeQuery($query, $element);
52
53
        return $query;
54
    }
55
56
    /**
57
     * @param $value
58
     * @param ElementInterface|null $element
59
     * @return RelationshipInterface
60
     */
61
    public function normalizeValue(
62
        $value,
63
        ElementInterface $element = null
64
    ) {
65
        if ($value instanceof RelationshipInterface) {
66
            return $value;
67
        }
68
69
        $query = $this->getQuery($element);
70
71
        $this->normalizeQueryValue($query, $value, $element);
0 ignored issues
show
Compatibility introduced by
$query of type object<craft\elements\db\ElementQueryInterface> is not a sub-type of object<craft\elements\db\ElementQuery>. It seems like you assume a concrete implementation of the interface craft\elements\db\ElementQueryInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
72
73
        return new Relationship(
74
            $this,
75
            $element
76
        );
77
    }
78
79
    /**
80
     * @param ElementQuery $query
81
     * @param ElementInterface|null $element
82
     */
83
    protected function normalizeQuery(
84
        ElementQuery $query,
85
        ElementInterface $element = null
86
    ) {
87
        /** @var Element|null $element */
88
89
        $source = ($element !== null && $element->getId() !== null ? $element->getId() : false);
90
91
        if ($source !== false) {
92
            $name = Association::tableName();
93
            $alias = Association::tableAlias();
94
95
            $query->innerJoin(
96
                $name . ' ' . $alias,
97
                [
98
                    'and',
99
                    '[[' . $alias . '.targetId]] = [[elements.id]]',
100
                    [
101
                        $alias . '.sourceId' => $source,
102
                        $alias . '.fieldId' => $this->id
103
                    ],
104
                    [
105
                        'or',
106
                        [$alias . '.sourceSiteId' => null],
107
                        [$alias . '.sourceSiteId' => SiteHelper::ensureSiteId($element->siteId ?: null)]
108
                    ]
109
                ]
110
            );
111
        } else {
112
            $query->id(false);
113
        }
114
    }
115
116
    /**
117
     * @param ElementQuery $query
118
     * @param $value
119
     * @param ElementInterface|null $element
120
     */
121
    protected function normalizeQueryValue(
122
        ElementQuery $query,
123
        $value,
124
        ElementInterface $element = null
125
    ) {
126
        if (is_array($value)) {
127
            $this->normalizeQueryInputValues($query, $value, $element);
128
            return;
129
        }
130
131
        if ($value === '') {
132
            $this->normalizeQueryEmptyValue($query);
133
            return;
134
        }
135
    }
136
137
    /**
138
     * @param ElementQuery $query
139
     * @param array $value
140
     * @param ElementInterface|null $element
141
     */
142
    protected function normalizeQueryInputValues(
143
        ElementQuery $query,
144
        array $value,
145
        ElementInterface $element = null
146
    ) {
147
        $models = [];
148
        $sortOrder = 1;
149
        foreach ($value as $val) {
150
            $models[] = $this->normalizeQueryInputValue($val, $sortOrder, $element);
151
        }
152
        $query->setCachedResult($models);
153
    }
154
155
    /**
156
     * @param $value
157
     * @param int $sortOrder
158
     * @param ElementInterface|Element|null $element
159
     * @return Association
160
     */
161
    protected function normalizeQueryInputValue(
162
        $value,
163
        int &$sortOrder,
164
        ElementInterface $element = null
165
    ): Association {
166
167
        if (is_array($value)) {
168
            $value = StringHelper::toString($value);
169
        }
170
171
        return new Association([
172
            'fieldId' => $this->id,
173
            'targetId' => $value,
174
            'sourceId' => $element === null ? null : $element->getId(),
175
            'siteId' => SiteHelper::ensureSiteId($element === null ? null : $element->siteId),
176
            'sortOrder' => $sortOrder++
177
        ]);
178
    }
179
180
    /**
181
     * @param ElementQuery $query
182
     */
183
    protected function normalizeQueryEmptyValue(
184
        ElementQuery $query
185
    ) {
186
        $query->setCachedResult([]);
187
    }
188
}
189