NormalizeValueTrait::recordClass()
last analyzed

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
c 0
b 0
f 0
ccs 0
cts 0
cp 0
nc 1
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\integration\fields;
10
11
use craft\base\Element;
12
use craft\base\ElementInterface;
13
use craft\helpers\StringHelper;
14
use flipbox\craft\ember\helpers\SiteHelper;
15
use flipbox\craft\integration\queries\IntegrationAssociationQuery;
16
use flipbox\craft\integration\records\IntegrationAssociation;
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
    /**
27
     * @return string
28
     */
29
    abstract public static function recordClass(): string;
30
31
    /**
32
     * @param ElementInterface|Element|null $element
33
     * @return IntegrationAssociationQuery
34
     */
35
    protected function getQuery(ElementInterface $element = null): IntegrationAssociationQuery
36
    {
37
        /** @var IntegrationAssociation $recordClass */
38
        $recordClass = static::recordClass();
39
40
        $query = $recordClass::find();
41
42
        if ($this->max !== null) {
43
            $query->limit($this->max);
44
        }
45
46
        $query->field($this)
47
            ->siteId(SiteHelper::ensureSiteId($element === null ? null : $element->siteId))
48
            ->elementId(($element === null || $element->getId() === null
49
            ) ? false : $element->getId());
50
51
        return $query;
52
    }
53
54
    /**
55
     * @param $value
56
     * @param ElementInterface|null $element
57
     * @return IntegrationAssociationQuery
58
     */
59
    public function normalizeValue(
60
        $value,
61
        ElementInterface $element = null
62
    ) {
63
        if ($value instanceof IntegrationAssociationQuery) {
64
            return $value;
65
        }
66
67
        $query = $this->getQuery($element);
68
        $this->normalizeQueryValue($query, $value, $element);
69
        return $query;
70
    }
71
72
    /**
73
     * @param IntegrationAssociationQuery $query
74
     * @param $value
75
     * @param ElementInterface|null $element
76
     */
77
    protected function normalizeQueryValue(
78
        IntegrationAssociationQuery $query,
79
        $value,
80
        ElementInterface $element = null
81
    ) {
82
        if (is_array($value)) {
83
            $this->normalizeQueryInputValues($query, $value, $element);
84
            return;
85
        }
86
87
        if ($value === '') {
88
            $this->normalizeQueryEmptyValue($query);
89
            return;
90
        }
91
    }
92
93
    /**
94
     * @param IntegrationAssociationQuery $query
95
     * @param array $value
96
     * @param ElementInterface|null $element
97
     */
98
    protected function normalizeQueryInputValues(
99
        IntegrationAssociationQuery $query,
100
        array $value,
101
        ElementInterface $element = null
102
    ) {
103
104
105
        $models = [];
106
        $sortOrder = 1;
107
        foreach ($value as $val) {
108
            $models[] = $this->normalizeQueryInputValue($val, $sortOrder, $element);
109
        }
110
        $query->setCachedResult($models);
111
    }
112
113
    /**
114
     * @param $value
115
     * @param int $sortOrder
116
     * @param ElementInterface|Element|null $element
117
     * @return IntegrationAssociation
118
     */
119
    protected function normalizeQueryInputValue(
120
        $value,
121
        int &$sortOrder,
122
        ElementInterface $element = null
123
    ): IntegrationAssociation {
124
125
126
        if (is_array($value)) {
127
            $value = StringHelper::toString($value);
128
        }
129
130
        /** @var IntegrationAssociation $recordClass */
131
        $recordClass = static::recordClass();
132
133
        /** @var IntegrationAssociation $association */
134
        $association = new $recordClass();
135
        $association->setField($this)
136
            ->setElement($element)
137
            ->setSiteId(SiteHelper::ensureSiteId($element === null ? null : $element->siteId));
138
139
        $association->sortOrder = $sortOrder++;
140
        $association->objectId = $value;
141
142
        return $association;
143
    }
144
145
    /**
146
     * @param IntegrationAssociationQuery $query
147
     */
148
    protected function normalizeQueryEmptyValue(
149
        IntegrationAssociationQuery $query
150
    ) {
151
152
        $query->setCachedResult([]);
153
    }
154
}
155