@@ -28,37 +28,37 @@ class Block implements \IteratorAggregate, \JsonSerializable
28
28
/**
29
29
* @var bool resolve UUID relations automatically
30
30
*/
31
- public $ _autoResolveRelations = false ;
31
+ public bool $ _autoResolveRelations = false ;
32
32
33
33
/**
34
34
* @var array list of field names containing relations to resolve
35
35
*/
36
- public $ _resolveRelations = [];
36
+ public array $ _resolveRelations = [];
37
37
38
38
/**
39
39
* @var bool Remove unresolved relations such as those that 404
40
40
*/
41
- public $ _filterRelations = true ;
41
+ public bool $ _filterRelations = true ;
42
42
43
43
/**
44
44
* @var array the path of nested components
45
45
*/
46
- public $ _componentPath = [];
46
+ public array $ _componentPath = [];
47
47
48
48
/**
49
49
* @var array the path of nested components
50
50
*/
51
- protected $ _casts = [];
51
+ protected array $ _casts = [];
52
52
53
53
/**
54
54
* @var Collection all the fields for the Block
55
55
*/
56
- private $ _fields ;
56
+ private Collection $ _fields ;
57
57
58
58
/**
59
59
* @var Page|Block reference to the parent Block or Page
60
60
*/
61
- private $ _parent ;
61
+ private mixed $ _parent ;
62
62
63
63
/**
64
64
* Takes the Block’s content and a reference to the parent
@@ -93,7 +93,8 @@ public function __construct($content, $parent = null)
93
93
*
94
94
* @return Collection
95
95
*/
96
- public function content () {
96
+ public function content (): Collection
97
+ {
97
98
return $ this ->_fields ;
98
99
}
99
100
@@ -103,7 +104,8 @@ public function content() {
103
104
* @param $key
104
105
* @return bool
105
106
*/
106
- public function has ($ key ) {
107
+ public function has ($ key ): bool
108
+ {
107
109
return $ this ->_fields ->has ($ key );
108
110
}
109
111
@@ -115,7 +117,8 @@ public function has($key) {
115
117
* @param $component
116
118
* @return boolean
117
119
*/
118
- public function hasChildBlock ($ field , $ component ) {
120
+ public function hasChildBlock ($ field , $ component ): bool
121
+ {
119
122
return $ this ->content ()[$ field ]->contains (function ($ item ) use ($ component ) {
120
123
return $ item ->meta ('component ' ) === $ component ;
121
124
});
@@ -126,7 +129,8 @@ public function hasChildBlock($field, $component) {
126
129
*
127
130
* @return Block
128
131
*/
129
- public function parent () {
132
+ public function parent (): Block |Page |null
133
+ {
130
134
return $ this ->_parent ;
131
135
}
132
136
@@ -135,7 +139,8 @@ public function parent() {
135
139
*
136
140
* @return Block
137
141
*/
138
- public function page () {
142
+ public function page (): Block |Page |null
143
+ {
139
144
if ($ this ->parent () instanceof Page) {
140
145
return $ this ->parent ();
141
146
}
@@ -150,12 +155,9 @@ public function page() {
150
155
* @return View
151
156
* @throws UnableToRenderException
152
157
*/
153
- public function render ($ with = []) {
154
- try {
155
- return view ()->first ($ this ->views (), array_merge (['block ' => $ this ], $ with ));
156
- } catch (\Exception $ exception ) {
157
- throw new UnableToRenderException ('None of the views in the given array exist. ' , $ this );
158
- }
158
+ public function render (array $ with = []): View
159
+ {
160
+ return $ this ->renderUsing ($ this ->views (), $ with );
159
161
}
160
162
161
163
/**
@@ -166,7 +168,8 @@ public function render($with = []) {
166
168
* @return View
167
169
* @throws UnableToRenderException
168
170
*/
169
- public function renderUsing ($ views , $ with = []) {
171
+ public function renderUsing (array |string $ views , array $ with = []): View
172
+ {
170
173
try {
171
174
return view ()->first (Arr::wrap ($ views ), array_merge (['block ' => $ this ], $ with ));
172
175
} catch (\Exception $ exception ) {
@@ -189,13 +192,14 @@ public function renderUsing($views, $with = []) {
189
192
*
190
193
* @return array
191
194
*/
192
- public function views () {
193
- $ compontentPath = $ this ->_componentPath ;
194
- array_pop ($ compontentPath );
195
+ public function views (): array
196
+ {
197
+ $ componentPath = $ this ->_componentPath ;
198
+ array_pop ($ componentPath );
195
199
196
200
$ views = array_map (function ($ path ) {
197
201
return config ('storyblok.view_path ' ) . 'blocks. ' . $ path . '. ' . $ this ->component ();
198
- }, $ compontentPath );
202
+ }, $ componentPath );
199
203
200
204
$ views = array_reverse ($ views );
201
205
@@ -210,7 +214,7 @@ public function views() {
210
214
* @param $generation int
211
215
* @return mixed
212
216
*/
213
- public function ancestorComponentName ($ generation )
217
+ public function ancestorComponentName (int $ generation ): mixed
214
218
{
215
219
return $ this ->_componentPath [count ($ this ->_componentPath ) - ($ generation + 1 )];
216
220
}
@@ -221,7 +225,7 @@ public function ancestorComponentName($generation)
221
225
* @param $parent string
222
226
* @return bool
223
227
*/
224
- public function isChildOf ($ parent )
228
+ public function isChildOf (string $ parent ): bool
225
229
{
226
230
return $ this ->_componentPath [count ($ this ->_componentPath ) - 2 ] === $ parent ;
227
231
}
@@ -232,17 +236,18 @@ public function isChildOf($parent)
232
236
* @param $parent string
233
237
* @return bool
234
238
*/
235
- public function isAncestorOf ($ parent )
239
+ public function isAncestorOf (string $ parent ): bool
236
240
{
237
- return in_array ($ parent , $ this ->parent ()->_componentPath );
241
+ return in_array ($ parent , $ this ->parent ()->_componentPath , true );
238
242
}
239
243
240
244
/**
241
245
* Returns the current Block’s component name from Storyblok
242
246
*
243
247
* @return string
244
248
*/
245
- public function component () {
249
+ public function component (): string
250
+ {
246
251
return $ this ->_meta ['component ' ];
247
252
}
248
253
@@ -254,7 +259,8 @@ public function component() {
254
259
*
255
260
* @return string
256
261
*/
257
- public function editorLink () {
262
+ public function editorLink (): string
263
+ {
258
264
if (array_key_exists ('_editable ' , $ this ->_meta ) && config ('storyblok.edit_mode ' )) {
259
265
return $ this ->_meta ['_editable ' ];
260
266
}
@@ -290,17 +296,17 @@ public function __get($key) {
290
296
*
291
297
* @return string
292
298
*/
293
- public function __toString () {
299
+ public function __toString (): string
300
+ {
294
301
return (string ) $ this ->jsonSerialize ();
295
302
}
296
303
297
304
/**
298
305
* Loops over every field to get the ball rolling
299
306
*/
300
- private function processFields () {
301
- $ this ->_fields ->transform (function ($ field , $ key ) {
302
- return $ this ->getFieldType ($ field , $ key );
303
- });
307
+ private function processFields (): void
308
+ {
309
+ $ this ->_fields ->transform (fn ($ field , $ key ) => $ this ->getFieldType ($ field , $ key ));
304
310
}
305
311
306
312
/**
@@ -311,7 +317,8 @@ private function processFields() {
311
317
* @return array|Collection|mixed|Asset|Image|MultiAsset|RichText|Table
312
318
* @throws \Storyblok\ApiException
313
319
*/
314
- private function getFieldType ($ field , $ key ) {
320
+ private function getFieldType ($ field , $ key ): mixed
321
+ {
315
322
return (new FieldFactory ())->build ($ this , $ field , $ key );
316
323
}
317
324
@@ -321,7 +328,8 @@ private function getFieldType($field, $key) {
321
328
*
322
329
* @param $content
323
330
*/
324
- private function preprocess ($ content ) {
331
+ private function preprocess ($ content ): void
332
+ {
325
333
// run pre-process traits - methods matching preprocessTraitClassName()
326
334
foreach (class_uses_recursive ($ this ) as $ trait ) {
327
335
if (method_exists ($ this , $ method = 'preprocess ' . class_basename ($ trait ))) {
@@ -357,11 +365,23 @@ public function getIterator(): \Traversable {
357
365
return $ this ->_fields ;
358
366
}
359
367
360
- public function getRelation (RequestStory $ request , $ relation ) {
368
+ /**
369
+ * @param RequestStory $requestStory
370
+ * @param $relation
371
+ * @param $className
372
+ * @return mixed|null
373
+ */
374
+ public function getRelation (RequestStory $ requestStory , $ relation , $ className = null ): mixed
375
+ {
361
376
try {
362
- $ response = $ request ->get ($ relation );
377
+ $ response = $ requestStory ->get ($ relation );
378
+
379
+ if (!$ className ) {
380
+ $ class = $ this ->getChildClassName ('Block ' , $ response ['content ' ]['component ' ]);
381
+ } else {
382
+ $ class = $ className ;
383
+ }
363
384
364
- $ class = $ this ->getChildClassName ('Block ' , $ response ['content ' ]['component ' ]);
365
385
$ relationClass = new $ class ($ response ['content ' ], $ this );
366
386
367
387
$ relationClass ->addMeta ([
@@ -381,13 +401,14 @@ public function getRelation(RequestStory $request, $relation) {
381
401
* Returns an inverse relationship to the current Block. For example if Service has a Multi-Option field
382
402
* relationship to People, on People you can request all the Services it has been related to
383
403
*
384
- * @param $foreignRelationshipField
385
- * @param $foreignRelationshipType
386
- * @param $components
387
- * @param $options
404
+ * @param string $foreignRelationshipField
405
+ * @param string $foreignRelationshipType
406
+ * @param array|null $components
407
+ * @param array|null $options
388
408
* @return array
389
409
*/
390
- public function inverseRelation ($ foreignRelationshipField , $ foreignRelationshipType = 'multi ' , $ components = null , $ options = null ) {
410
+ public function inverseRelation (string $ foreignRelationshipField , string $ foreignRelationshipType = 'multi ' , array $ components = null , array $ options = null ): array
411
+ {
391
412
$ storyblokClient = resolve ('Storyblok\Client ' );
392
413
393
414
$ type = 'any_in_array ' ;
@@ -422,7 +443,13 @@ public function inverseRelation($foreignRelationshipField, $foreignRelationshipT
422
443
];
423
444
}
424
445
425
- public function getCasts () {
446
+ /**
447
+ * Returns the casts on this Block
448
+ *
449
+ * @return array
450
+ */
451
+ public function getCasts (): array
452
+ {
426
453
return $ this ->_casts ;
427
454
}
428
455
}
0 commit comments