Skip to content

Commit 147f66e

Browse files
committed
Merge branch 'release2.17.0'
2 parents e1bca8a + df0235a commit 147f66e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+821
-413
lines changed

.idea/laravel-storyblok.iml

+5-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/php.xml

+20-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434
"require-dev": {
3535
"mockery/mockery": "^1.2",
3636
"orchestra/testbench": "7.0.*",
37-
"phpunit/phpunit": "^9.0"
37+
"phpunit/phpunit": "^9.0",
38+
"spatie/laravel-ignition": "^1.6"
3839
},
3940
"autoload": {
4041
"psr-4": {

src/Block.php

+72-45
Original file line numberDiff line numberDiff line change
@@ -28,37 +28,37 @@ class Block implements \IteratorAggregate, \JsonSerializable
2828
/**
2929
* @var bool resolve UUID relations automatically
3030
*/
31-
public $_autoResolveRelations = false;
31+
public bool $_autoResolveRelations = false;
3232

3333
/**
3434
* @var array list of field names containing relations to resolve
3535
*/
36-
public $_resolveRelations = [];
36+
public array $_resolveRelations = [];
3737

3838
/**
3939
* @var bool Remove unresolved relations such as those that 404
4040
*/
41-
public $_filterRelations = true;
41+
public bool $_filterRelations = true;
4242

4343
/**
4444
* @var array the path of nested components
4545
*/
46-
public $_componentPath = [];
46+
public array $_componentPath = [];
4747

4848
/**
4949
* @var array the path of nested components
5050
*/
51-
protected $_casts = [];
51+
protected array $_casts = [];
5252

5353
/**
5454
* @var Collection all the fields for the Block
5555
*/
56-
private $_fields;
56+
private Collection $_fields;
5757

5858
/**
5959
* @var Page|Block reference to the parent Block or Page
6060
*/
61-
private $_parent;
61+
private mixed $_parent;
6262

6363
/**
6464
* Takes the Block’s content and a reference to the parent
@@ -93,7 +93,8 @@ public function __construct($content, $parent = null)
9393
*
9494
* @return Collection
9595
*/
96-
public function content() {
96+
public function content(): Collection
97+
{
9798
return $this->_fields;
9899
}
99100

@@ -103,7 +104,8 @@ public function content() {
103104
* @param $key
104105
* @return bool
105106
*/
106-
public function has($key) {
107+
public function has($key): bool
108+
{
107109
return $this->_fields->has($key);
108110
}
109111

@@ -115,7 +117,8 @@ public function has($key) {
115117
* @param $component
116118
* @return boolean
117119
*/
118-
public function hasChildBlock($field, $component) {
120+
public function hasChildBlock($field, $component): bool
121+
{
119122
return $this->content()[$field]->contains(function($item) use ($component) {
120123
return $item->meta('component') === $component;
121124
});
@@ -126,7 +129,8 @@ public function hasChildBlock($field, $component) {
126129
*
127130
* @return Block
128131
*/
129-
public function parent() {
132+
public function parent(): Block|Page|null
133+
{
130134
return $this->_parent;
131135
}
132136

@@ -135,7 +139,8 @@ public function parent() {
135139
*
136140
* @return Block
137141
*/
138-
public function page() {
142+
public function page(): Block|Page|null
143+
{
139144
if ($this->parent() instanceof Page) {
140145
return $this->parent();
141146
}
@@ -150,12 +155,9 @@ public function page() {
150155
* @return View
151156
* @throws UnableToRenderException
152157
*/
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);
159161
}
160162

161163
/**
@@ -166,7 +168,8 @@ public function render($with = []) {
166168
* @return View
167169
* @throws UnableToRenderException
168170
*/
169-
public function renderUsing($views, $with = []) {
171+
public function renderUsing(array|string $views, array $with = []): View
172+
{
170173
try {
171174
return view()->first(Arr::wrap($views), array_merge(['block' => $this], $with));
172175
} catch (\Exception $exception) {
@@ -189,13 +192,14 @@ public function renderUsing($views, $with = []) {
189192
*
190193
* @return array
191194
*/
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);
195199

196200
$views = array_map(function($path) {
197201
return config('storyblok.view_path') . 'blocks.' . $path . '.' . $this->component();
198-
}, $compontentPath);
202+
}, $componentPath);
199203

200204
$views = array_reverse($views);
201205

@@ -210,7 +214,7 @@ public function views() {
210214
* @param $generation int
211215
* @return mixed
212216
*/
213-
public function ancestorComponentName($generation)
217+
public function ancestorComponentName(int $generation): mixed
214218
{
215219
return $this->_componentPath[count($this->_componentPath) - ($generation + 1)];
216220
}
@@ -221,7 +225,7 @@ public function ancestorComponentName($generation)
221225
* @param $parent string
222226
* @return bool
223227
*/
224-
public function isChildOf($parent)
228+
public function isChildOf(string $parent): bool
225229
{
226230
return $this->_componentPath[count($this->_componentPath) - 2] === $parent;
227231
}
@@ -232,17 +236,18 @@ public function isChildOf($parent)
232236
* @param $parent string
233237
* @return bool
234238
*/
235-
public function isAncestorOf($parent)
239+
public function isAncestorOf(string $parent): bool
236240
{
237-
return in_array($parent, $this->parent()->_componentPath);
241+
return in_array($parent, $this->parent()->_componentPath, true);
238242
}
239243

240244
/**
241245
* Returns the current Block’s component name from Storyblok
242246
*
243247
* @return string
244248
*/
245-
public function component() {
249+
public function component(): string
250+
{
246251
return $this->_meta['component'];
247252
}
248253

@@ -254,7 +259,8 @@ public function component() {
254259
*
255260
* @return string
256261
*/
257-
public function editorLink() {
262+
public function editorLink(): string
263+
{
258264
if (array_key_exists('_editable', $this->_meta) && config('storyblok.edit_mode')) {
259265
return $this->_meta['_editable'];
260266
}
@@ -290,17 +296,17 @@ public function __get($key) {
290296
*
291297
* @return string
292298
*/
293-
public function __toString() {
299+
public function __toString(): string
300+
{
294301
return (string) $this->jsonSerialize();
295302
}
296303

297304
/**
298305
* Loops over every field to get the ball rolling
299306
*/
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));
304310
}
305311

306312
/**
@@ -311,7 +317,8 @@ private function processFields() {
311317
* @return array|Collection|mixed|Asset|Image|MultiAsset|RichText|Table
312318
* @throws \Storyblok\ApiException
313319
*/
314-
private function getFieldType($field, $key) {
320+
private function getFieldType($field, $key): mixed
321+
{
315322
return (new FieldFactory())->build($this, $field, $key);
316323
}
317324

@@ -321,7 +328,8 @@ private function getFieldType($field, $key) {
321328
*
322329
* @param $content
323330
*/
324-
private function preprocess($content) {
331+
private function preprocess($content): void
332+
{
325333
// run pre-process traits - methods matching preprocessTraitClassName()
326334
foreach (class_uses_recursive($this) as $trait) {
327335
if (method_exists($this, $method = 'preprocess' . class_basename($trait))) {
@@ -357,11 +365,23 @@ public function getIterator(): \Traversable {
357365
return $this->_fields;
358366
}
359367

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+
{
361376
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+
}
363384

364-
$class = $this->getChildClassName('Block', $response['content']['component']);
365385
$relationClass = new $class($response['content'], $this);
366386

367387
$relationClass->addMeta([
@@ -381,13 +401,14 @@ public function getRelation(RequestStory $request, $relation) {
381401
* Returns an inverse relationship to the current Block. For example if Service has a Multi-Option field
382402
* relationship to People, on People you can request all the Services it has been related to
383403
*
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
388408
* @return array
389409
*/
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+
{
391412
$storyblokClient = resolve('Storyblok\Client');
392413

393414
$type = 'any_in_array';
@@ -422,7 +443,13 @@ public function inverseRelation($foreignRelationshipField, $foreignRelationshipT
422443
];
423444
}
424445

425-
public function getCasts() {
446+
/**
447+
* Returns the casts on this Block
448+
*
449+
* @return array
450+
*/
451+
public function getCasts(): array
452+
{
426453
return $this->_casts;
427454
}
428455
}

0 commit comments

Comments
 (0)