Skip to content

Commit da95a76

Browse files
committed
Added instancesValues method
1 parent 8c36d5c commit da95a76

File tree

7 files changed

+75
-12
lines changed

7 files changed

+75
-12
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ This package's only purpose is to build custom repeated layout components, such
2424
- `serializeUsing($callback)`
2525
- get all inserted instances, and manipulate the LayoutCollection :
2626
- `instances()`
27+
- `instancesValues()`
2728
- `$instances->find($key)`
2829
- `$instances->whereKey($key)`
2930
- `$instances->whereKeyNot($key)`

src/Concerns/HasLayoutInstances.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,19 @@ public function instances() : LayoutsCollection
113113
return $this->instances;
114114
}
115115

116+
/**
117+
* Get all the inserted layout instances serialized for display in a user interface.
118+
*
119+
* @return array
120+
*/
121+
public function instancesValues() : array
122+
{
123+
return $this->instances()
124+
->map(fn(Layout $instance) => $instance->toDisplayableArray())
125+
->values()
126+
->all();
127+
}
128+
116129
/**
117130
* Get the amount of inserted layout instances, total or per layout key.
118131
*

src/Contracts/Flexible.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,13 @@ public function insert(string $key, array $attributes = [], ?int $index = null,
8181
*/
8282
public function instances() : LayoutsCollection;
8383

84+
/**
85+
* Get all the inserted layout instances serialized for display in a user interface.
86+
*
87+
* @return array
88+
*/
89+
public function instancesValues() : array;
90+
8491
/**
8592
* Get the amount of inserted layout instances, total or per layout key.
8693
*

src/Contracts/Layout.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,17 @@ public function make(?string $id = null, array $attributes = []) : Layout;
9393
*/
9494
public function toSerializableArray() : array;
9595

96+
/**
97+
* Convert the layout instance's state to an array that can be displayed in an user interface.
98+
* It is intended to be extended and filled with the frontend component's required attributes.
99+
*
100+
* @return array
101+
*/
102+
public function toDisplayableArray() : array;
103+
96104
/**
97105
* Convert the layout to an array that can be displayed in a user menu.
106+
* It is intended to be extended and filled with the frontend component's required attributes.
98107
*
99108
* @return array
100109
*/

src/Layout.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,8 +302,23 @@ public function toSerializableArray() : array
302302
];
303303
}
304304

305+
/**
306+
* Convert the layout instance's state to an array that can be displayed in an user interface.
307+
* It is intended to be extended and filled with the frontend component's required attributes.
308+
*
309+
* @return array
310+
*/
311+
public function toDisplayableArray() : array
312+
{
313+
return [
314+
'key' => $this->getKey(),
315+
'id' => $this->getId(),
316+
];
317+
}
318+
305319
/**
306320
* Convert the layout to an array that can be displayed in a user menu.
321+
* It is intended to be extended and filled with the frontend component's required attributes.
307322
*
308323
* @return array
309324
*/

tests/Unit/InstanceInsertingTest.php

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,23 @@
66
use Whitecube\LaravelFlexibleContent\Exceptions\LayoutNotFoundException;
77

88
it('can push a new empty layout instance to its value', function() {
9-
$flexible = (new Flexible())->register(function($layout) {
10-
$layout->key('foo');
11-
});
9+
$flexible = (new Flexible())->register(fn ($layout) => $layout->key('foo'));
1210

1311
$flexible->insert('foo');
1412

1513
expect($flexible->count())->toBe(1);
1614
});
1715

1816
it('can push a layout instance with defined attributes to its value', function() {
19-
$flexible = (new Flexible())->register(function($layout) {
20-
$layout->key('foo');
21-
});
17+
$flexible = (new Flexible())->register(fn ($layout) => $layout->key('foo'));
2218

2319
$flexible->insert('foo', ['bar' => 'test'], null, 'some-id');
2420

2521
expect($flexible->count())->toBe(1);
2622
});
2723

2824
it('can insert a layout at a certain index in its value', function() {
29-
$flexible = (new Flexible())->register(function($layout) {
30-
$layout->key('foo');
31-
});
25+
$flexible = (new Flexible())->register(fn ($layout) => $layout->key('foo'));
3226

3327
$flexible->insert('foo', [], null, 'one');
3428
$flexible->insert('foo', [], null, 'three');
@@ -39,9 +33,22 @@
3933
});
4034

4135
it('cannot add an unregistered layout key to its value', function() {
42-
$flexible = (new Flexible())->register(function($layout) {
43-
$layout->key('foo');
44-
});
36+
$flexible = (new Flexible())->register(fn ($layout) => $layout->key('foo'));
4537

4638
$flexible->insert('bar');
4739
})->throws(LayoutNotFoundException::class);
40+
41+
it('can return all inserted layout instances as a displayable array', function() {
42+
$flexible = (new Flexible())->register(fn ($layout) => $layout->key('foo'));
43+
44+
$flexible->insert('foo', [], null, 'one');
45+
$flexible->insert('foo', [], null, 'two');
46+
$flexible->insert('foo', [], null, 'three');
47+
48+
$value = $flexible->instancesValues();
49+
50+
expect($value)->toBeArray();
51+
expect($value)->toHaveCount(3);
52+
expect(array_key_exists('id', $value[0]))->toBeTrue();
53+
expect(array_key_exists('limit', $value[0]))->toBeFalse();
54+
});

tests/Unit/LayoutManipulationTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,17 @@
8282
expect(array_key_exists('limit', $converted))->toBeFalse();
8383
});
8484

85+
it('can convert layout to a displayable (UI) array', function() {
86+
$layout = (new Layout())->key('foo')->limit(5)->make(null, ['test' => true, 'something' => false]);
87+
88+
$converted = $layout->toDisplayableArray();
89+
90+
expect(array_key_exists('key', $converted))->toBeTrue();
91+
expect(array_key_exists('id', $converted))->toBeTrue();
92+
expect(array_key_exists('attributes', $converted))->toBeFalse();
93+
expect(array_key_exists('limit', $converted))->toBeFalse();
94+
});
95+
8596
it('can convert layout to a displayable (menu) array', function() {
8697
$layout = (new Layout())->key('foo')->limit(5)->make(null, ['test' => true, 'something' => false]);
8798

0 commit comments

Comments
 (0)