|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
|
5
|
|
|
* @license https://flipboxfactory.com/software/patron/license |
|
6
|
|
|
* @link https://www.flipboxfactory.com/patron/domains/ |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace flipbox\patron\queries; |
|
10
|
|
|
|
|
11
|
|
|
use craft\helpers\Db; |
|
12
|
|
|
use flipbox\craft\ember\helpers\QueryHelper; |
|
13
|
|
|
use flipbox\patron\helpers\ProviderHelper; |
|
14
|
|
|
use flipbox\patron\records\Provider as ProviderRecord; |
|
15
|
|
|
use flipbox\patron\records\Token; |
|
16
|
|
|
use League\OAuth2\Client\Provider\AbstractProvider; |
|
17
|
|
|
use yii\db\Query; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @author Flipbox Factory <[email protected]> |
|
21
|
|
|
* @since 1.0.0 |
|
22
|
|
|
*/ |
|
23
|
|
|
trait TokenProviderAttributeTrait |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* The provider(s) that the resulting must have. |
|
27
|
|
|
* |
|
28
|
|
|
* @var string|string[]|int|int[]|ProviderRecord|ProviderRecord[]|AbstractProvider|AbstractProvider[]|null |
|
29
|
|
|
*/ |
|
30
|
|
|
public $provider; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param string|string[]|int|int[]|ProviderRecord|ProviderRecord[]|AbstractProvider|AbstractProvider[]|null $value |
|
34
|
|
|
* @return static The query object |
|
35
|
|
|
*/ |
|
36
|
|
|
public function setProvider($value) |
|
37
|
|
|
{ |
|
38
|
|
|
$this->provider = $value; |
|
39
|
|
|
return $this; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param string|string[]|int|int[]|ProviderRecord|ProviderRecord[]|AbstractProvider|AbstractProvider[]|null $value |
|
44
|
|
|
* @return static The query object |
|
45
|
|
|
*/ |
|
46
|
|
|
public function provider($value) |
|
47
|
|
|
{ |
|
48
|
|
|
return $this->setProvider($value); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param string|string[]|int|int[]|ProviderRecord|ProviderRecord[]|AbstractProvider|AbstractProvider[]|null $value |
|
53
|
|
|
* @return static The query object |
|
54
|
|
|
*/ |
|
55
|
|
|
public function setProviderId($value) |
|
56
|
|
|
{ |
|
57
|
|
|
return $this->setProvider($value); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param string|string[]|int|int[]|ProviderRecord|ProviderRecord[]|AbstractProvider|AbstractProvider[]|null $value |
|
62
|
|
|
* @return static The query object |
|
63
|
|
|
*/ |
|
64
|
|
|
public function providerId($value) |
|
65
|
|
|
{ |
|
66
|
|
|
return $this->setProvider($value); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param $value |
|
71
|
|
|
* @param string $join |
|
72
|
|
|
* @return array |
|
73
|
|
|
*/ |
|
74
|
|
|
protected function parseProviderValue($value, string $join = 'or'): array |
|
75
|
|
|
{ |
|
76
|
|
|
if (false === QueryHelper::parseBaseParam($value, $join)) { |
|
77
|
|
|
foreach ($value as $operator => &$v) { |
|
78
|
|
|
$this->resolveProviderValue($operator, $v); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
// Filter null and empties |
|
83
|
|
|
$value = array_filter($value, function ($arr): bool { |
|
84
|
|
|
return $arr !== null && $arr !== ''; |
|
85
|
|
|
}); |
|
86
|
|
|
|
|
87
|
|
|
if (empty($value)) { |
|
88
|
|
|
return []; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return array_merge([$join], $value); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @param $operator |
|
96
|
|
|
* @param $value |
|
97
|
|
|
*/ |
|
98
|
|
|
protected function resolveProviderValue($operator, &$value) |
|
99
|
|
|
{ |
|
100
|
|
|
if ($value instanceof AbstractProvider) { |
|
101
|
|
|
$value = ProviderHelper::lookupId($value); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
if (false === QueryHelper::findParamValue($value, $operator)) { |
|
105
|
|
|
if (is_string($value)) { |
|
106
|
|
|
$value = $this->resolveProviderStringValue($value); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
if ($value instanceof ProviderRecord) { |
|
110
|
|
|
$value = $value->id; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
if ($value) { |
|
114
|
|
|
$value = QueryHelper::assembleParamValue($value, $operator); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @param string $value |
|
121
|
|
|
* @return int|null |
|
122
|
|
|
*/ |
|
123
|
|
|
protected function resolveProviderStringValue(string $value) |
|
124
|
|
|
{ |
|
125
|
|
|
if (is_numeric($value)) { |
|
126
|
|
|
$condition = ['id' => $value]; |
|
127
|
|
|
} else { |
|
128
|
|
|
$condition = ['handle' => $value]; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
$id = (new Query()) |
|
132
|
|
|
->select(['id']) |
|
133
|
|
|
->from([ProviderRecord::tableName()]) |
|
134
|
|
|
->where($condition) |
|
135
|
|
|
->scalar(); |
|
136
|
|
|
|
|
137
|
|
|
return $id ? (int)$id : null; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* |
|
142
|
|
|
*/ |
|
143
|
|
|
protected function applyProviderConditions() |
|
144
|
|
|
{ |
|
145
|
|
|
if (empty($this->provider)) { |
|
146
|
|
|
return; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
$this->andWhere( |
|
|
|
|
|
|
150
|
|
|
Db::parseParam(Token::tableAlias() . '.providerId', $this->parseProviderValue($this->provider)) |
|
151
|
|
|
); |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.