Completed
Push — master ( fa2880...7af5aa )
by Nate
01:12
created

NormalizeValueTrait::getQuery()   A

Complexity

Conditions 5
Paths 2

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 0
cts 13
cp 0
rs 9.3554
c 0
b 0
f 0
nc 2
cc 5
nop 1
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\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 1.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
    
64
65
        if ($value instanceof IntegrationAssociationQuery) {
66
            return $value;
67
        }
68
69
        $query = $this->getQuery($element);
70
        $this->normalizeQueryValue($query, $value, $element);
71
        return $query;
72
    }
73
74
    /**
75
     * @param IntegrationAssociationQuery $query
76
     * @param $value
77
     * @param ElementInterface|null $element
78
     */
79
    protected function normalizeQueryValue(
80
        IntegrationAssociationQuery $query,
81
        $value,
82
        ElementInterface $element = null
83
    ) {
84
    
85
86
        if (is_array($value)) {
87
            $this->normalizeQueryInputValues($query, $value, $element);
88
            return;
89
        }
90
91
        if ($value === '') {
92
            $this->normalizeQueryEmptyValue($query);
93
            return;
94
        }
95
    }
96
97
    /**
98
     * @param IntegrationAssociationQuery $query
99
     * @param array $value
100
     * @param ElementInterface|null $element
101
     */
102
    protected function normalizeQueryInputValues(
103
        IntegrationAssociationQuery $query,
104
        array $value,
105
        ElementInterface $element = null
106
    ) {
107
    
108
109
        $models = [];
110
        $sortOrder = 1;
111
        foreach ($value as $val) {
112
            $models[] = $this->normalizeQueryInputValue($val, $sortOrder, $element);
113
        }
114
        $query->setCachedResult($models);
115
    }
116
117
    /**
118
     * @param $value
119
     * @param int $sortOrder
120
     * @param ElementInterface|Element|null $element
121
     * @return IntegrationAssociation
122
     */
123
    protected function normalizeQueryInputValue(
124
        $value,
125
        int &$sortOrder,
126
        ElementInterface $element = null
127
    ): IntegrationAssociation {
128
    
129
130
        if (is_array($value)) {
131
            $value = StringHelper::toString($value);
132
        }
133
134
        /** @var IntegrationAssociation $recordClass */
135
        $recordClass = static::recordClass();
136
137
        /** @var IntegrationAssociation $association */
138
        $association = new $recordClass();
139
        $association->setField($this)
140
            ->setElement($element)
141
            ->setSiteId(SiteHelper::ensureSiteId($element === null ? null : $element->siteId));
142
143
        $association->sortOrder = $sortOrder++;
144
        $association->objectId = $value;
145
146
        return $association;
147
    }
148
149
    /**
150
     * @param IntegrationAssociationQuery $query
151
     */
152
    protected function normalizeQueryEmptyValue(
153
        IntegrationAssociationQuery $query
154
    ) {
155
    
156
        $query->setCachedResult([]);
157
    }
158
}
159