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\ember\helpers\traits; |
10
|
|
|
|
11
|
|
|
use craft\db\Query; |
12
|
|
|
use craft\helpers\Db; |
13
|
|
|
use craft\models\UserGroup; |
14
|
|
|
use flipbox\ember\helpers\ArrayHelper; |
15
|
|
|
use flipbox\ember\helpers\QueryHelper; |
16
|
|
|
use craft\records\UserGroup as UserGroupRecord; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @author Flipbox Factory <[email protected]> |
20
|
|
|
* @since 1.0.0 |
21
|
|
|
*/ |
22
|
|
|
trait UserGroupQueryValue |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @param $value |
26
|
|
|
* @param string $join |
27
|
|
|
* @return array |
28
|
|
|
*/ |
29
|
|
|
public static function parseUserGroupValue($value, string $join = 'and'): array |
30
|
|
|
{ |
31
|
|
|
if (false === QueryHelper::parseBaseParam($value, $join)) { |
32
|
|
|
foreach ($value as $operator => &$v) { |
33
|
|
|
self::resolveUserGroupValue($operator, $v); |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
$value = ArrayHelper::filterEmptyAndNullValuesFromArray($value); |
38
|
|
|
|
39
|
|
|
if (empty($value)) { |
40
|
|
|
return []; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
// parse param to allow for mixed variables |
44
|
|
|
return array_merge([$join], $value); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param $operator |
49
|
|
|
* @param $value |
50
|
|
|
*/ |
51
|
|
|
private static function resolveUserGroupValue($operator, &$value) |
52
|
|
|
{ |
53
|
|
|
if (false === QueryHelper::findParamValue($value, $operator)) { |
54
|
|
|
if (is_string($value)) { |
55
|
|
|
$value = static::resolveUserGroupStringValue($value); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if ($value instanceof UserGroup) { |
59
|
|
|
$value = $value->id; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if ($value) { |
63
|
|
|
$value = QueryHelper::assembleParamValue($value, $operator); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param string $value |
70
|
|
|
* @return int|null |
71
|
|
|
*/ |
72
|
|
|
protected static function resolveUserGroupStringValue(string $value) |
73
|
|
|
{ |
74
|
|
|
return (new Query()) |
75
|
|
|
->select(['id']) |
76
|
|
|
->from([UserGroupRecord::tableName()]) |
77
|
|
|
->where(Db::parseParam('handle', $value)) |
|
|
|
|
78
|
|
|
->scalar(); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.