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

UserGroupAttributeTrait::setUserGroupId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
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\db\Query;
12
use craft\helpers\Db;
13
use craft\models\UserGroup;
14
use craft\records\UserGroup as UserGroupRecord;
15
use flipbox\craft\ember\helpers\QueryHelper;
16
17
/**
18
 * @author Flipbox Factory <[email protected]>
19
 * @since 2.0.0
20
 */
21
trait UserGroupAttributeTrait
22
{
23
    /**
24
     * The user group(s) that the resulting organizations’ users must be in.
25
     *
26
     * @var string|string[]|int|int[]|UserGroup|UserGroup[]|null
27
     */
28
    public $userGroup;
29
30
    /**
31
     * @param string|string[]|int|int[]|UserGroup|UserGroup[]|null $value
32
     * @return static The query object
33
     */
34
    public function setUserGroup($value)
35
    {
36
        $this->userGroup = $value;
37
        return $this;
38
    }
39
40
    /**
41
     * @param string|string[]|int|int[]|UserGroup|UserGroup[]|null $value
42
     * @return static The query object
43
     */
44
    public function userGroup($value)
45
    {
46
        return $this->setUserGroup($value);
47
    }
48
49
    /**
50
     * @param string|string[]|int|int[]|UserGroup|UserGroup[]|null $value
51
     * @return static The query object
52
     */
53
    public function setUserGroupId($value)
54
    {
55
        return $this->setUserGroup($value);
56
    }
57
58
    /**
59
     * @param string|string[]|int|int[]|UserGroup|UserGroup[]|null $value
60
     * @return static The query object
61
     */
62
    public function userGroupId($value)
63
    {
64
        return $this->setUserGroup($value);
65
    }
66
67
    /**
68
     * @param $value
69
     * @param string $join
70
     * @return array
71
     */
72
    protected function parseUserGroupValue($value, string $join = 'or'): array
73
    {
74
        if (false === QueryHelper::parseBaseParam($value, $join)) {
75
            foreach ($value as $operator => &$v) {
76
                $this->resolveUserGroupValue($operator, $v);
77
            }
78
        }
79
80
        // Filter null and empties
81
        $value = array_filter($value, function ($arr): bool {
82
            return $arr !== null && $arr !== '';
83
        });
84
85
        if (empty($value)) {
86
            return [];
87
        }
88
89
        // parse param to allow for mixed variables
90
        return array_merge([$join], $value);
91
    }
92
93
    /**
94
     * @param $operator
95
     * @param $value
96
     */
97
    protected function resolveUserGroupValue($operator, &$value)
98
    {
99
        if (false === QueryHelper::findParamValue($value, $operator)) {
100
            if (is_string($value)) {
101
                $value = $this->resolveUserGroupStringValue($value);
102
            }
103
104
            if ($value) {
105
                $value = QueryHelper::assembleParamValue($value, $operator);
106
            }
107
        }
108
    }
109
110
    /**
111
     * @param string $value
112
     * @return string|null
113
     */
114
    protected function resolveUserGroupStringValue(string $value)
115
    {
116
        $value = (new Query())
117
            ->select(['id'])
118
            ->from([UserGroupRecord::tableName()])
119
            ->where(Db::parseParam('handle', $value))
120
            ->scalar();
121
        return empty($value) ? false : $value;
122
    }
123
}
124