Completed
Push — master ( 11971b...eeafb3 )
by Nate
01:16
created

FieldAttributeTrait::setField()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 5
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://github.com/flipboxfactory/craft-ember/blob/master/LICENSE
6
 * @link       https://github.com/flipboxfactory/craft-ember/
7
 */
8
9
namespace flipbox\craft\ember\queries;
10
11
use Craft;
12
use craft\base\Field;
13
use craft\base\FieldInterface;
14
use flipbox\craft\ember\helpers\QueryHelper;
15
16
/**
17
 * @author Flipbox Factory <[email protected]>
18
 * @since 2.0.0
19
 */
20
trait FieldAttributeTrait
21
{
22
    /**
23
     * The field(s) that the resulting organizations’ fields must have.
24
     *
25
     * @var string|string[]|int|int[]|FieldInterface|FieldInterface[]|null
26
     */
27
    public $field;
28
29
    /**
30
     * @param string|string[]|int|int[]|FieldInterface|FieldInterface[]|null $value
31
     * @return static The query object
32
     */
33
    public function setField($value)
34
    {
35
        $this->field = $value;
36
        return $this;
37
    }
38
39
    /**
40
     * @param string|string[]|int|int[]|FieldInterface|FieldInterface[]|null $value
41
     * @return static The query object
42
     */
43
    public function field($value)
44
    {
45
        return $this->setField($value);
46
    }
47
48
    /**
49
     * @param string|string[]|int|int[]|FieldInterface|FieldInterface[]|null $value
50
     * @return static The query object
51
     */
52
    public function setFieldId($value)
53
    {
54
        return $this->setField($value);
55
    }
56
57
    /**
58
     * @param string|string[]|int|int[]|FieldInterface|FieldInterface[]|null $value
59
     * @return static The query object
60
     */
61
    public function fieldId($value)
62
    {
63
        return $this->setField($value);
64
    }
65
66
    /**
67
     * @param $value
68
     * @param string $join
69
     * @return array
70
     */
71
    protected function parseFieldValue($value, string $join = 'or'): array
72
    {
73
        if (false === QueryHelper::parseBaseParam($value, $join)) {
74
            foreach ($value as $operator => &$v) {
75
                $this->resolveFieldValue($operator, $v);
76
            }
77
        }
78
79
        // Filter null and empties
80
        $value = array_filter($value, function ($arr): bool {
81
            return $arr !== null && $arr !== '';
82
        });
83
84
        if (empty($value)) {
85
            return [];
86
        }
87
88
        return array_merge([$join], $value);
89
    }
90
91
    /**
92
     * @param $operator
93
     * @param $value
94
     */
95
    protected function resolveFieldValue($operator, &$value)
96
    {
97
        if (false === QueryHelper::findParamValue($value, $operator)) {
98
            if (is_string($value)) {
99
                $value = $this->resolveFieldStringValue($value);
100
            }
101
102
            if ($value instanceof FieldInterface) {
103
                /** @var Field $value */
104
                $value = $value->id;
105
            }
106
107
            if ($value) {
108
                $value = QueryHelper::assembleParamValue($value, $operator);
109
            }
110
        }
111
    }
112
113
    /**
114
     * @param string $value
115
     * @return int|null
116
     */
117
    protected function resolveFieldStringValue(string $value)
118
    {
119
        /** @var Field $field */
120
        if (!$field = Craft::$app->getFields()->getFieldByHandle($value)) {
121
            return null;
122
        }
123
        return $field->id;
124
    }
125
}
126