Completed
Push — master ( a20c7b...d885ca )
by Nate
02:36
created

NormalizeValueTrait::normalizeValue()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 0
cts 15
cp 0
rs 9.584
c 0
b 0
f 0
cc 4
nc 3
nop 2
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\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 $id
23
 * @property int|null $limit
24
 * @property bool $allowLimit
25
 */
26
trait NormalizeValueTrait
27
{
28
    abstract protected static function elementType(): string;
29
30
    /**
31
     * @param $value
32
     * @param ElementInterface|null $element
33
     * @return ElementQuery
34
     */
35
    public function normalizeValue(
36
        $value,
37
        ElementInterface $element = null
38
    ) {
39
        if ($value instanceof ElementQuery) {
40
            return $value;
41
        }
42
43
        /** @var Element $elementType */
44
        $elementType = static::elementType();
45
46
        /** @var ElementQuery $query */
47
        $query = $elementType::find();
48
49
        if ($this->allowLimit && $this->limit) {
50
            $query->limit($this->limit);
51
        }
52
53
        $this->normalizeQueryValue($query, $value, $element);
54
        return $query;
55
    }
56
57
    /**
58
     * @param ElementQuery $query
59
     * @param ElementInterface|null $element
60
     */
61
    protected function normalizeQuery(
62
        ElementQuery $query,
63
        ElementInterface $element = null
64
    ) {
65
        /** @var Element|null $element */
66
67
        $source = ($element !== null && $element->getId() !== null ? $element->getId() : false);
68
69
        if ($source !== false) {
70
            $name = Association::tableName();
71
            $alias = Association::tableAlias();
72
73
            $query->innerJoin(
74
                $name . ' ' . $alias,
75
                [
76
                    'and',
77
                    '[[' . $alias . '.targetId]] = [[elements.id]]',
78
                    [
79
                        $alias . '.sourceId' => $source,
80
                        $alias . '.fieldId' => $this->id,
81
                        $alias . '.siteId' => SiteHelper::ensureSiteId($element === null ? null : $element->siteId)
82
                    ]
83
                ]
84
            );
85
        } else {
86
            $query->id(false);
87
        }
88
    }
89
90
    /**
91
     * @param ElementQuery $query
92
     * @param $value
93
     * @param ElementInterface|null $element
94
     */
95
    protected function normalizeQueryValue(
96
        ElementQuery $query,
97
        $value,
98
        ElementInterface $element = null
99
    ) {
100
        $this->normalizeQuery($query, $element);
101
102
        if (is_array($value)) {
103
            $this->normalizeQueryInputValues($query, $value, $element);
104
            return;
105
        }
106
107
        if ($value === '') {
108
            $this->normalizeQueryEmptyValue($query);
109
            return;
110
        }
111
    }
112
113
    /**
114
     * @param ElementQuery $query
115
     * @param array $value
116
     * @param ElementInterface|null $element
117
     */
118
    protected function normalizeQueryInputValues(
119
        ElementQuery $query,
120
        array $value,
121
        ElementInterface $element = null
122
    ) {
123
        $models = [];
124
        $sortOrder = 1;
125
        foreach ($value as $val) {
126
            $models[] = $this->normalizeQueryInputValue($val, $sortOrder, $element);
127
        }
128
        $query->setCachedResult($models);
129
    }
130
131
    /**
132
     * @param $value
133
     * @param int $sortOrder
134
     * @param ElementInterface|Element|null $element
135
     * @return Association
136
     */
137
    protected function normalizeQueryInputValue(
138
        $value,
139
        int &$sortOrder,
140
        ElementInterface $element = null
141
    ): Association {
142
143
        if (is_array($value)) {
144
            $value = StringHelper::toString($value);
145
        }
146
147
        return new Association([
148
            'fieldId' => $this->id,
149
            'targetId' => $value,
150
            'sourceId' => $element === null ? null : $element->getId(),
151
            'siteId' => SiteHelper::ensureSiteId($element === null ? null : $element->siteId),
152
            'sortOrder' => $sortOrder++
153
        ]);
154
    }
155
156
    /**
157
     * @param ElementQuery $query
158
     */
159
    protected function normalizeQueryEmptyValue(
160
        ElementQuery $query
161
    ) {
162
        $query->setCachedResult([]);
163
    }
164
}
165