|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
|
5
|
|
|
* @license https://flipboxfactory.com/software/hubspot/license |
|
6
|
|
|
* @link https://www.flipboxfactory.com/software/hubspot/ |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace flipbox\hubspot\criteria; |
|
10
|
|
|
|
|
11
|
|
|
use flipbox\hubspot\HubSpot; |
|
12
|
|
|
use flipbox\hubspot\services\Cache; |
|
13
|
|
|
use flipbox\hubspot\services\Connections; |
|
14
|
|
|
use flipbox\hubspot\transformers\collections\DynamicTransformerCollection; |
|
15
|
|
|
use flipbox\ember\helpers\ObjectHelper; |
|
16
|
|
|
use flipbox\hubspot\connections\ConnectionInterface; |
|
17
|
|
|
use flipbox\hubspot\helpers\TransformerHelper; |
|
18
|
|
|
use flipbox\hubspot\transformers\collections\TransformerCollectionInterface; |
|
19
|
|
|
use Psr\SimpleCache\CacheInterface; |
|
20
|
|
|
use yii\base\BaseObject; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @author Flipbox Factory <[email protected]> |
|
24
|
|
|
* @since 1.0.0 |
|
25
|
|
|
*/ |
|
26
|
|
|
class BaseCriteria extends BaseObject implements CriteriaInterface |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @var ConnectionInterface|string |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $connection = Connections::DEFAULT_CONNECTION; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var CacheInterface|string|null |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $cache = Cache::DEFAULT_CACHE; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var TransformerCollectionInterface|null |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $transformer = ['class' => DynamicTransformerCollection::class]; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param $value |
|
45
|
|
|
* @return $this |
|
46
|
|
|
*/ |
|
47
|
|
|
public function connection($value) |
|
48
|
|
|
{ |
|
49
|
|
|
return $this->setConnection($value); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param $value |
|
54
|
|
|
* @return $this |
|
55
|
|
|
*/ |
|
56
|
|
|
public function setConnection($value) |
|
57
|
|
|
{ |
|
58
|
|
|
$this->connection = $value; |
|
59
|
|
|
return $this; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @inheritdoc |
|
64
|
|
|
*/ |
|
65
|
|
|
public function getConnection(): ConnectionInterface |
|
66
|
|
|
{ |
|
67
|
|
|
if ($this->connection instanceof ConnectionInterface) { |
|
68
|
|
|
return $this->connection; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
if ($this->connection === null) { |
|
72
|
|
|
$this->connection = Connections::DEFAULT_CONNECTION; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return $this->connection = HubSpot::getInstance()->getConnections()->get($this->connection); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param $value |
|
80
|
|
|
* @return $this |
|
81
|
|
|
*/ |
|
82
|
|
|
public function cache($value) |
|
83
|
|
|
{ |
|
84
|
|
|
return $this->setCache($value); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param $value |
|
89
|
|
|
* @return $this |
|
90
|
|
|
*/ |
|
91
|
|
|
public function setCache($value) |
|
92
|
|
|
{ |
|
93
|
|
|
$this->cache = $value; |
|
94
|
|
|
return $this; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @inheritdoc |
|
99
|
|
|
*/ |
|
100
|
|
|
public function getCache(): CacheInterface |
|
101
|
|
|
{ |
|
102
|
|
|
if ($this->cache instanceof CacheInterface) { |
|
103
|
|
|
return $this->cache; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
if ($this->cache === null) { |
|
107
|
|
|
$this->cache = Cache::DEFAULT_CACHE; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
return $this->cache = HubSpot::getInstance()->getCache()->get($this->cache); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @param $value |
|
115
|
|
|
* @return $this |
|
116
|
|
|
*/ |
|
117
|
|
|
public function transformer($value) |
|
118
|
|
|
{ |
|
119
|
|
|
return $this->setTransformer($value); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @inheritdoc |
|
124
|
|
|
*/ |
|
125
|
|
|
public function setTransformer($value) |
|
126
|
|
|
{ |
|
127
|
|
|
if (empty($value)) { |
|
128
|
|
|
$this->transformer = null; |
|
129
|
|
|
return $this; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
if (is_string($value)) { |
|
133
|
|
|
if (TransformerHelper::isTransformerCollectionClass($value)) { |
|
134
|
|
|
$value = ['class' => $value]; |
|
135
|
|
|
} else { |
|
136
|
|
|
$value = ['handle' => [$value]]; |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
if (array_key_exists('class', $value)) { |
|
141
|
|
|
$this->transformer = $value; |
|
142
|
|
|
return $this; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
TransformerHelper::populateTransformerCollection( |
|
146
|
|
|
$this->getTransformer(), |
|
|
|
|
|
|
147
|
|
|
$value |
|
148
|
|
|
); |
|
149
|
|
|
|
|
150
|
|
|
return $this; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* @inheritdoc |
|
155
|
|
|
*/ |
|
156
|
|
|
public function getTransformer() |
|
157
|
|
|
{ |
|
158
|
|
|
if ($this->transformer === false) { |
|
159
|
|
|
return null; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
if ($this->transformer instanceof TransformerCollectionInterface) { |
|
163
|
|
|
return $this->transformer; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
// Prevent subsequent resolves (since it already didn't) |
|
167
|
|
|
if (null === ($this->transformer = TransformerHelper::resolveCollection($this->transformer))) { |
|
|
|
|
|
|
168
|
|
|
$this->transformer = false; |
|
|
|
|
|
|
169
|
|
|
return null; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
return $this->transformer; |
|
|
|
|
|
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* @inheritdoc |
|
177
|
|
|
*/ |
|
178
|
|
|
protected function prepare(array $criteria = []) |
|
179
|
|
|
{ |
|
180
|
|
|
ObjectHelper::populate( |
|
181
|
|
|
$this, |
|
182
|
|
|
$criteria |
|
183
|
|
|
); |
|
184
|
|
|
} |
|
185
|
|
|
} |
|
186
|
|
|
|
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.