1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
5
|
|
|
* @license https://github.com/flipbox/spark/blob/master/LICENSE |
6
|
|
|
* @link https://github.com/flipbox/spark |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace flipbox\spark\services\traits; |
10
|
|
|
|
11
|
|
|
use craft\base\Element as BaseElement; |
12
|
|
|
use craft\base\ElementInterface; |
13
|
|
|
use flipbox\spark\exceptions\ElementNotFoundException; |
14
|
|
|
use flipbox\spark\helpers\SiteHelper; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @author Flipbox Factory <[email protected]> |
18
|
|
|
* @since 2.0.0 |
19
|
|
|
*/ |
20
|
|
|
trait ElementByString |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var [ElementInterface[]] |
25
|
|
|
*/ |
26
|
|
|
protected $cacheByString = []; |
27
|
|
|
|
28
|
|
|
/******************************************* |
29
|
|
|
* ABSTRACTS |
30
|
|
|
*******************************************/ |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @return string |
34
|
|
|
*/ |
35
|
|
|
abstract protected function stringProperty(): string; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param $string |
39
|
|
|
* @param int|null $siteId |
40
|
|
|
* @return BaseElement|ElementInterface|null |
41
|
|
|
*/ |
42
|
|
|
abstract protected function freshFindByString(string $string, int $siteId = null); |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param ElementInterface $element |
46
|
|
|
* @return string |
47
|
|
|
*/ |
48
|
|
|
protected function stringValue(ElementInterface $element) |
49
|
|
|
{ |
50
|
|
|
|
51
|
|
|
$property = $this->stringProperty(); |
52
|
|
|
|
53
|
|
|
return $element->{$property}; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param string $string |
58
|
|
|
* @param int|null $siteId |
59
|
|
|
* @return BaseElement|ElementInterface|null |
60
|
|
|
*/ |
61
|
|
|
public function findByString(string $string, int $siteId = null) |
62
|
|
|
{ |
63
|
|
|
|
64
|
|
|
// Check cache |
65
|
|
|
if (!$element = $this->findCacheByString($string)) { |
66
|
|
|
// Find new element |
67
|
|
|
if ($element = $this->freshFindByString($string, $siteId)) { |
68
|
|
|
// Cache it |
69
|
|
|
$this->cacheByString($element); |
70
|
|
|
} else { |
71
|
|
|
// Cache nothing |
72
|
|
|
$this->cacheByString[$string] = $element; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $element; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param string $string |
81
|
|
|
* @param int|null $siteId |
82
|
|
|
* @return BaseElement|ElementInterface |
83
|
|
|
* @throws ElementNotFoundException |
84
|
|
|
*/ |
85
|
|
|
public function getByString(string $string, int $siteId = null) |
86
|
|
|
{ |
87
|
|
|
|
88
|
|
|
// Find by Handle |
89
|
|
|
if (!$element = $this->findByString($string, $siteId)) { |
90
|
|
|
$this->notFoundByStringException($string); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $element; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param string $string |
99
|
|
|
* @param int|null $siteId |
100
|
|
|
* @return BaseElement|ElementInterface |
101
|
|
|
* @throws ElementNotFoundException |
102
|
|
|
*/ |
103
|
|
|
public function freshGetByString(string $string, int $siteId = null) |
104
|
|
|
{ |
105
|
|
|
|
106
|
|
|
if (!$element = $this->freshFindByString($string, $siteId)) { |
107
|
|
|
$this->notFoundByStringException($string); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $element; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
|
114
|
|
|
/******************************************* |
115
|
|
|
* CACHE |
116
|
|
|
*******************************************/ |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Find an existing cache by Handle |
120
|
|
|
* |
121
|
|
|
* @param string $string |
122
|
|
|
* @param int|null $siteId |
123
|
|
|
* @return BaseElement|ElementInterface|null |
124
|
|
|
*/ |
125
|
|
|
public function findCacheByString(string $string, int $siteId = null) |
126
|
|
|
{ |
127
|
|
|
|
128
|
|
|
// Check if already in cache |
129
|
|
|
if ($this->isCachedByString($string, $siteId)) { |
130
|
|
|
return $this->cacheByString[$siteId][$string]; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return null; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Identify whether in cached by string |
138
|
|
|
* |
139
|
|
|
* @param $string |
140
|
|
|
* @param int|null $siteId |
141
|
|
|
* @return bool |
142
|
|
|
*/ |
143
|
|
|
protected function isCachedByString($string, int $siteId = null) |
144
|
|
|
{ |
145
|
|
|
|
146
|
|
|
// Resolve siteId |
147
|
|
|
$siteId = SiteHelper::resolveSiteId($siteId); |
148
|
|
|
|
149
|
|
|
if (!isset($this->cacheByString[$siteId])) { |
150
|
|
|
$this->cacheByString[$siteId] = []; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return array_key_exists($string, $this->cacheByString[$siteId]); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param ElementInterface $element |
158
|
|
|
* @return $this |
159
|
|
|
*/ |
160
|
|
|
protected function cacheByString(ElementInterface $element) |
161
|
|
|
{ |
162
|
|
|
|
163
|
|
|
/** @var BaseElement $element */ |
164
|
|
|
|
165
|
|
|
$stringValue = $this->stringValue($element); |
166
|
|
|
|
167
|
|
|
if (null === $stringValue) { |
168
|
|
|
return $this; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
// Resolve siteId |
172
|
|
|
$siteId = SiteHelper::resolveSiteId($element->siteId); |
173
|
|
|
|
174
|
|
|
// Check if already in cache |
175
|
|
|
if ($stringValue && !$this->isCachedByString($stringValue, $siteId)) { |
176
|
|
|
// Cache it |
177
|
|
|
$this->cacheByString[$siteId][$stringValue] = $element; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
return $this; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
|
184
|
|
|
|
185
|
|
|
|
186
|
|
|
/******************************************* |
187
|
|
|
* EXCEPTIONS |
188
|
|
|
*******************************************/ |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @param null $string |
192
|
|
|
* @throws ElementNotFoundException |
193
|
|
|
*/ |
194
|
|
|
protected function notFoundByStringException($string = null) |
195
|
|
|
{ |
196
|
|
|
|
197
|
|
|
throw new ElementNotFoundException( |
198
|
|
|
sprintf( |
199
|
|
|
'Element does not exist with the string "%s".', |
200
|
|
|
(string)$string |
201
|
|
|
) |
202
|
|
|
); |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|