Completed
Branch develop (17e8cf)
by Nate
10:01
created

ModifyElementQueryTrait::modifyElementsQuery()   A

Complexity

Conditions 5
Paths 4

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 15
cp 0
rs 9.3554
c 0
b 0
f 0
cc 5
nc 4
nop 2
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\element\lists\fields;
10
11
use craft\elements\db\ElementQuery;
12
use craft\elements\db\ElementQueryInterface;
13
use craft\helpers\Db;
14
use flipbox\craft\element\lists\records\Association;
15
16
/**
17
 * @author Flipbox Factory <[email protected]>
18
 * @since 2.0.0
19
 *
20
 * @property int|null id
21
 */
22
trait ModifyElementQueryTrait
23
{
24
    /**
25
     * @inheritdoc
26
     */
27
    public function modifyElementsQuery(ElementQueryInterface $query, $value)
28
    {
29
        if ($value === null || !$query instanceof ElementQuery) {
30
            return null;
31
        }
32
33
        if ($value === false) {
34
            return false;
35
        }
36
37
        if (is_string($value)) {
38
            $this->modifyElementsQueryForStringValue($query, $value);
39
            return null;
40
        }
41
42
        $this->modifyElementsQueryForTargetValue($query, $value);
43
        return null;
44
    }
45
46
    /**
47
     * @param ElementQuery $query
48
     * @param string $value
49
     */
50
    protected function modifyElementsQueryForStringValue(
51
        ElementQuery $query,
52
        string $value
53
    ) {
54
        if ($value === 'not :empty:') {
55
            $value = ':notempty:';
56
        }
57
58
        if ($value === ':notempty:' || $value === ':empty:') {
59
            $this->modifyElementsQueryForEmptyValue($query, $value);
60
            return;
61
        }
62
63
        $this->modifyElementsQueryForTargetValue($query, $value);
64
    }
65
66
    /**
67
     * @param ElementQuery $query
68
     * @param $value
69
     */
70
    protected function modifyElementsQueryForTargetValue(
71
        ElementQuery $query,
72
        $value
73
    ) {
74
        $alias = Association::tableAlias();
75
        $name = Association::tableName();
76
77
        $joinTable = "{$name} {$alias}";
78
        $query->query->innerJoin($joinTable, "[[{$alias}.targetId]] = [[subquery.elementsId]]");
79
        $query->subQuery->innerJoin($joinTable, "[[{$alias}.targetId]] = [[elements.id]]");
80
81
        $query->subQuery->andWhere(
82
            Db::parseParam($alias . '.fieldId', $this->id)
83
        );
84
85
        $query->subQuery->andWhere(
86
            Db::parseParam($alias . '.sourceId', $value)
87
        );
88
89
        $query->query->distinct(true);
90
    }
91
92
    /**
93
     * @param ElementQuery $query
94
     * @param string $value
95
     */
96
    protected function modifyElementsQueryForEmptyValue(
97
        ElementQuery $query,
98
        string $value
99
    ) {
100
        $alias = Association::tableAlias();
101
        $name = Association::tableName();
102
103
        $operator = ($value === ':notempty:' ? '!=' : '=');
104
        $query->subQuery->andWhere(
105
            "(select count([[{$alias}.targetId]]) from " .
1 ignored issue
show
Documentation introduced by
"(select count([[{$alias...is->id}) {$operator} 0" is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
106
            $name .
107
            " {{{$alias}}} where [[{$alias}.targetId" .
108
            "]] = [[elements.id]] and [[{$alias}.fieldId]] = {$this->id}) {$operator} 0"
109
        );
110
    }
111
}
112