1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Riclep\Storyblok; |
5
|
|
|
|
6
|
|
|
use Illuminate\Support\Collection; |
7
|
|
|
use Illuminate\Support\Str; |
8
|
|
|
use Illuminate\View\View; |
9
|
|
|
use Riclep\Storyblok\Exceptions\UnableToRenderException; |
10
|
|
|
use Riclep\Storyblok\Fields\Asset; |
11
|
|
|
use Riclep\Storyblok\Fields\Image; |
12
|
|
|
use Riclep\Storyblok\Fields\MultiAsset; |
13
|
|
|
use Riclep\Storyblok\Fields\RichText; |
14
|
|
|
use Riclep\Storyblok\Fields\Table; |
15
|
|
|
use Riclep\Storyblok\Traits\CssClasses; |
16
|
|
|
use Riclep\Storyblok\Traits\HasChildClasses; |
17
|
|
|
use Riclep\Storyblok\Traits\HasMeta; |
18
|
|
|
|
19
|
|
|
class Block implements \IteratorAggregate |
20
|
|
|
{ |
21
|
|
|
use CssClasses; |
22
|
|
|
use HasChildClasses; |
23
|
|
|
use HasMeta; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var bool resolve UUID relations automatically |
27
|
|
|
*/ |
28
|
|
|
public $_autoResolveRelations = false; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var array list of field names containing relations to resolve |
32
|
|
|
*/ |
33
|
|
|
public $_resolveRelations = []; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var array the path of nested components |
37
|
|
|
*/ |
38
|
|
|
public $_componentPath = []; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var array the path of nested components |
42
|
|
|
*/ |
43
|
|
|
protected $_casts = []; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var Collection all the fields for the Block |
47
|
|
|
*/ |
48
|
|
|
private $_fields; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var Page|Block reference to the parent Block or Page |
52
|
|
|
*/ |
53
|
|
|
private $_parent; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Takes the Block’s content and a reference to the parent |
57
|
|
|
* @param $content |
58
|
|
|
* @param $parent |
59
|
|
|
*/ |
60
|
|
|
public function __construct($content, $parent = null) |
61
|
|
|
{ |
62
|
|
|
$this->_parent = $parent; |
63
|
|
|
$this->preprocess($content); |
64
|
|
|
|
65
|
|
|
if ($parent) { |
66
|
|
|
$this->_componentPath = array_merge($parent->_componentPath, [Str::lower($this->meta()['component'])]); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$this->processFields(); |
70
|
|
|
|
71
|
|
|
// run automatic traits - methods matching initTraitClassName() |
72
|
|
|
foreach (class_uses_recursive($this) as $trait) { |
73
|
|
|
if (method_exists($this, $method = 'init' . class_basename($trait))) { |
74
|
|
|
$this->{$method}(); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Returns the containing every field of content |
81
|
|
|
* |
82
|
|
|
* @return Collection |
83
|
|
|
*/ |
84
|
|
|
public function content() { |
85
|
|
|
return $this->_fields; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Checks if the fields contain the specified key |
90
|
|
|
* |
91
|
|
|
* @param $key |
92
|
|
|
* @return bool |
93
|
|
|
*/ |
94
|
|
|
public function has($key) { |
95
|
|
|
return $this->_fields->has($key); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Returns the parent Block |
100
|
|
|
* |
101
|
|
|
* @return Block |
102
|
|
|
*/ |
103
|
|
|
public function parent() { |
104
|
|
|
return $this->_parent; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Returns the page this Block belongs to |
109
|
|
|
* |
110
|
|
|
* @return Block |
111
|
|
|
*/ |
112
|
|
|
public function page() { |
113
|
|
|
if ($this->parent() instanceof Page) { |
|
|
|
|
114
|
|
|
return $this->parent(); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $this->parent()->page(); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Returns the first matching view, passing it the fields |
122
|
|
|
* |
123
|
|
|
* @return View |
124
|
|
|
* @throws UnableToRenderException |
125
|
|
|
*/ |
126
|
|
|
public function render() { |
127
|
|
|
try { |
128
|
|
|
return view()->first($this->views(), ['block' => $this]); |
129
|
|
|
} catch (\Exception $exception) { |
130
|
|
|
throw new UnableToRenderException('None of the views in the given array exist.', $this); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Returns an array of possible views for the current Block based on |
136
|
|
|
* it’s $componentPath match the component prefixed by each of it’s |
137
|
|
|
* ancestors in turn, starting with the closest, for example: |
138
|
|
|
* |
139
|
|
|
* $componentPath = ['page', 'parent', 'child', 'this_block']; |
140
|
|
|
* |
141
|
|
|
* Becomes a list of possible views like so: |
142
|
|
|
* ['child.this_block', 'parent.this_block', 'page.this_block']; |
143
|
|
|
* |
144
|
|
|
* Override this method with your custom implementation for |
145
|
|
|
* ultimate control |
146
|
|
|
* |
147
|
|
|
* @return array |
148
|
|
|
*/ |
149
|
|
|
public function views() { |
150
|
|
|
$compontentPath = $this->_componentPath; |
151
|
|
|
array_pop($compontentPath); |
152
|
|
|
|
153
|
|
|
$views = array_map(function($path) { |
154
|
|
|
return config('storyblok.view_path') . 'blocks.' . $path . '.' . $this->component(); |
155
|
|
|
}, $compontentPath); |
156
|
|
|
|
157
|
|
|
$views = array_reverse($views); |
158
|
|
|
|
159
|
|
|
$views[] = config('storyblok.view_path') . 'blocks.' . $this->component(); |
160
|
|
|
|
161
|
|
|
return $views; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Returns a component X generations previous |
166
|
|
|
* |
167
|
|
|
* @param $generation int |
168
|
|
|
* @return mixed |
169
|
|
|
*/ |
170
|
|
|
public function ancestorComponentName($generation) |
171
|
|
|
{ |
172
|
|
|
return $this->_componentPath[count($this->_componentPath) - ($generation + 1)]; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Checks if the current component is a child of another |
177
|
|
|
* |
178
|
|
|
* @param $parent string |
179
|
|
|
* @return bool |
180
|
|
|
*/ |
181
|
|
|
public function isChildOf($parent) |
182
|
|
|
{ |
183
|
|
|
return $this->_componentPath[count($this->_componentPath) - 2] === $parent; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Checks if the component is an ancestor of another |
188
|
|
|
* |
189
|
|
|
* @param $parent string |
190
|
|
|
* @return bool |
191
|
|
|
*/ |
192
|
|
|
public function isAncestorOf($parent) |
193
|
|
|
{ |
194
|
|
|
return in_array($parent, $this->parent()->_componentPath); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Returns the current Block’s component name from Storyblok |
199
|
|
|
* |
200
|
|
|
* @return string |
201
|
|
|
*/ |
202
|
|
|
public function component() { |
203
|
|
|
return $this->_meta['component']; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Returns the HTML comment required for making this Block clickable in |
209
|
|
|
* Storyblok’s visual editor. Don’t forget to set comments to true in |
210
|
|
|
* your Vue.js app configuration. |
211
|
|
|
* |
212
|
|
|
* @return string |
213
|
|
|
*/ |
214
|
|
|
public function editorLink() { |
215
|
|
|
if (array_key_exists('_editable', $this->_meta) && config('storyblok.edit_mode')) { |
216
|
|
|
return $this->_meta['_editable']; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
return ''; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Magic accessor to pull content from the _fields collection. Works just like |
225
|
|
|
* Laravel’s model accessors. Matches public methods with the follow naming |
226
|
|
|
* convention getSomeFieldAttribute() - called via $block->some_field |
227
|
|
|
* |
228
|
|
|
* @param $key |
229
|
|
|
* @return null|string |
230
|
|
|
*/ |
231
|
|
|
public function __get($key) { |
232
|
|
|
$accessor = 'get' . Str::studly($key) . 'Attribute'; |
233
|
|
|
|
234
|
|
|
if (method_exists($this, $accessor)) { |
235
|
|
|
return $this->$accessor(); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
if ($this->has($key)) { |
239
|
|
|
return $this->_fields[$key]; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
return null; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Loops over every field to get the ball rolling |
247
|
|
|
*/ |
248
|
|
|
private function processFields() { |
249
|
|
|
$this->_fields->transform(function ($field, $key) { |
250
|
|
|
return $this->getFieldType($field, $key); |
251
|
|
|
}); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* Converts fields into Field Classes based on various properties of their content |
256
|
|
|
* |
257
|
|
|
* @param $field |
258
|
|
|
* @param $key |
259
|
|
|
* @return array|Collection|mixed|Asset|Image|MultiAsset|RichText|Table |
260
|
|
|
* @throws \Storyblok\ApiException |
261
|
|
|
*/ |
262
|
|
|
private function getFieldType($field, $key) { |
263
|
|
|
$factory = new FieldFactory(); |
264
|
|
|
return $factory->build($this, $field, $key); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* Storyblok returns fields and other meta content at the same level so |
269
|
|
|
* let’s do a little tidying up first |
270
|
|
|
* |
271
|
|
|
* @param $content |
272
|
|
|
*/ |
273
|
|
|
private function preprocess($content) { |
274
|
|
|
$this->_fields = collect(array_diff_key($content, array_flip(['_editable', '_uid', 'component']))); |
275
|
|
|
|
276
|
|
|
// remove non-content keys |
277
|
|
|
$this->_meta = array_intersect_key($content, array_flip(['_editable', '_uid', 'component'])); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* Let’s up loop over the fields in Blade without needing to |
282
|
|
|
* delve deep into the content collection |
283
|
|
|
* |
284
|
|
|
* @return \Traversable |
285
|
|
|
*/ |
286
|
|
|
public function getIterator() { |
287
|
|
|
return $this->_fields; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
public function getRelation(RequestStory $request, $relation) { |
291
|
|
|
$response = $request->get($relation); |
292
|
|
|
|
293
|
|
|
$class = $this->getChildClassName('Block', $response['content']['component']); |
294
|
|
|
$relationClass = new $class($response['content'], $this); |
295
|
|
|
|
296
|
|
|
$relationClass->addMeta([ |
297
|
|
|
'name' => $response['name'], |
298
|
|
|
'published_at' => $response['published_at'], |
299
|
|
|
'full_slug' => $response['full_slug'], |
300
|
|
|
]); |
301
|
|
|
|
302
|
|
|
return $relationClass; |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
public function getCasts() { |
306
|
|
|
return $this->_casts; |
307
|
|
|
} |
308
|
|
|
} |