Skip to content

Commit 8c36d5c

Browse files
committed
Added specific layout serialization methods for different use cases
1 parent 546eb4f commit 8c36d5c

File tree

9 files changed

+119
-23
lines changed

9 files changed

+119
-23
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ This package's only purpose is to build custom repeated layout components, such
99
- `register($layout, $limit)`
1010
- get all registered layouts
1111
- `layouts()`
12+
- `layoutsMenu()`
1213
- limiting layouts
1314
- `limit($instances)`
1415
- `$layout->limit($instances)`

src/Concerns/HasLayouts.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,17 @@ public function layouts() : LayoutsCollection
8989

9090
return $this->layouts;
9191
}
92+
93+
/**
94+
* Get all the defined layouts serialized for display in a menu.
95+
*
96+
* @return array
97+
*/
98+
public function layoutsMenu() : array
99+
{
100+
return $this->layouts()
101+
->map(fn(Layout $layout) => $layout->toButtonArray())
102+
->values()
103+
->all();
104+
}
92105
}

src/Concerns/HasResolver.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,7 @@ public function serializeAs(string $format)
170170
public function serializeAsArray()
171171
{
172172
return $this->instances()
173-
->map(fn(Layout $instance) => [
174-
'key' => $instance->getKey(),
175-
'id' => $instance->getId(),
176-
'attributes' => $instance->getAttributes()
177-
])
173+
->map(fn(Layout $instance) => $instance->toSerializableArray())
178174
->all();
179175
}
180176

src/Contracts/Flexible.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ public function getLayout(string $key) : ?Layout;
3939
*/
4040
public function layouts() : LayoutsCollection;
4141

42+
/**
43+
* Get all the defined layouts serialized for display in a menu.
44+
*
45+
* @return array
46+
*/
47+
public function layoutsMenu() : array;
48+
4249
/**
4350
* Prevent the Flexible container to instanciate more layouts
4451
* than the indicated amount.

src/Contracts/Layout.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,18 @@ public function isInsertable(Flexible $container);
8585
* @return \Whitecube\LaravelFlexibleContent\Contracts\Layout
8686
*/
8787
public function make(?string $id = null, array $attributes = []) : Layout;
88+
89+
/**
90+
* Convert the layout instance's state to an array that can be saved.
91+
*
92+
* @return array
93+
*/
94+
public function toSerializableArray() : array;
95+
96+
/**
97+
* Convert the layout to an array that can be displayed in a user menu.
98+
*
99+
* @return array
100+
*/
101+
public function toButtonArray() : array;
88102
}

src/Layout.php

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -289,17 +289,42 @@ public function toArray()
289289
}
290290

291291
/**
292-
* Transform layout for front-end serialization (AJAX requests usage).
293-
* This method should most probably be extended for proper exploitation.
292+
* Convert the layout instance's state to an array that can be saved.
294293
*
295294
* @return array
296295
*/
297-
public function jsonSerialize()
296+
public function toSerializableArray() : array
298297
{
299298
return [
300299
'key' => $this->getKey(),
301300
'id' => $this->getId(),
301+
'attributes' => $this->getAttributes(),
302+
];
303+
}
304+
305+
/**
306+
* Convert the layout to an array that can be displayed in a user menu.
307+
*
308+
* @return array
309+
*/
310+
public function toButtonArray() : array
311+
{
312+
return [
313+
'key' => $this->getKey(),
302314
'limit' => $this->getLimit(),
303315
];
304316
}
317+
318+
/**
319+
* Transform layout for front-end serialization (AJAX requests usage).
320+
*
321+
* @return array
322+
*/
323+
public function jsonSerialize()
324+
{
325+
return array_merge(
326+
$this->toButtonArray(),
327+
$this->toSerializableArray()
328+
);
329+
}
305330
}

tests/Unit/InstanceResolvingTest.php

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,12 @@
9898
expect($serialized)->toBeArray();
9999

100100
foreach (['foo', 'bar', 'foo'] as $index => $key) {
101-
expect($serialized[$index]['key'] ?? null)->toBe($key);
102-
expect($serialized[$index]['id'] ?? null)->toBeString();
103-
expect($serialized[$index]['attributes']['test'] ?? null)->toBe($index);
101+
$item = $serialized[$index];
102+
103+
expect($item['key'] ?? null)->toBe($key);
104+
expect($item['id'] ?? null)->toBeString();
105+
expect($item['attributes']['test'] ?? null)->toBe($index);
106+
expect(array_key_exists('limit', $item))->toBeFalse();
104107
}
105108
});
106109

@@ -117,9 +120,12 @@
117120
expect($serialized = json_decode($serialized, true))->toBeArray();
118121

119122
foreach (['foo', 'bar', 'foo'] as $index => $key) {
120-
expect($serialized[$index]['key'] ?? null)->toBe($key);
121-
expect($serialized[$index]['id'] ?? null)->toBeString();
122-
expect($serialized[$index]['attributes']['test'] ?? null)->toBe($index);
123+
$item = $serialized[$index];
124+
125+
expect($item['key'] ?? null)->toBe($key);
126+
expect($item['id'] ?? null)->toBeString();
127+
expect($item['attributes']['test'] ?? null)->toBe($index);
128+
expect(array_key_exists('limit', $item))->toBeFalse();
123129
}
124130
});
125131

@@ -135,9 +141,12 @@
135141
expect($serialized)->toBeInstanceOf(Collection::class);
136142

137143
foreach (['foo', 'bar', 'foo'] as $index => $key) {
138-
expect($serialized->get($index)['key'] ?? null)->toBe($key);
139-
expect($serialized->get($index)['id'] ?? null)->toBeString();
140-
expect($serialized->get($index)['attributes']['test'] ?? null)->toBe($index);
144+
$item = $serialized->get($index);
145+
146+
expect($item['key'] ?? null)->toBe($key);
147+
expect($item['id'] ?? null)->toBeString();
148+
expect($item['attributes']['test'] ?? null)->toBe($index);
149+
expect(array_key_exists('limit', $item))->toBeFalse();
141150
}
142151
});
143152

tests/Unit/LayoutManipulationTest.php

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,29 @@
7171
expect($converted['something'])->toBeFalse();
7272
});
7373

74-
it('can convert layout to a JSON response object (for its frontend use)', function() {
74+
it('can convert layout to a saveable array', function() {
75+
$layout = (new Layout())->key('foo')->limit(5)->make(null, ['test' => true, 'something' => false]);
76+
77+
$converted = $layout->toSerializableArray();
78+
79+
expect(array_key_exists('key', $converted))->toBeTrue();
80+
expect(array_key_exists('id', $converted))->toBeTrue();
81+
expect(array_key_exists('attributes', $converted))->toBeTrue();
82+
expect(array_key_exists('limit', $converted))->toBeFalse();
83+
});
84+
85+
it('can convert layout to a displayable (menu) array', function() {
86+
$layout = (new Layout())->key('foo')->limit(5)->make(null, ['test' => true, 'something' => false]);
87+
88+
$converted = $layout->toButtonArray();
89+
90+
expect(array_key_exists('key', $converted))->toBeTrue();
91+
expect(array_key_exists('id', $converted))->toBeFalse();
92+
expect(array_key_exists('attributes', $converted))->toBeFalse();
93+
expect(array_key_exists('limit', $converted))->toBeTrue();
94+
});
95+
96+
it('can convert layout to a JSON response object', function() {
7597
$layout = (new Layout())->key('foo')->make(null, ['test' => true, 'something' => false]);
7698

7799
$converted = json_decode(json_encode($layout), true);
@@ -80,9 +102,5 @@
80102
expect($converted['key'])->toBe('foo');
81103
expect(array_key_exists('id', $converted))->toBeTrue();
82104
expect(array_key_exists('limit', $converted))->toBeTrue();
83-
84-
// In this package, we do not include the layout's attributes in its JSON structure
85-
// since they should probably be manipulated by the extending flexible container. It is
86-
// up to the developer to decide if attributes should be sent to the view.
87-
expect(array_key_exists('attributes', $converted))->toBeFalse();
105+
expect(array_key_exists('attributes', $converted))->toBeTrue();
88106
});

tests/Unit/LayoutRegistrationTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,16 @@
8787
expect($collection)->toBeInstanceOf(LayoutsCollection::class);
8888
expect($collection->keys()->implode(','))->toBe('foo,bar');
8989
});
90+
91+
it('can return all registered layouts as a "menu" array', function() {
92+
$flexible = (new Flexible())
93+
->register(fn ($layout) => $layout->key('foo')->limit(3))
94+
->register(fn ($layout) => $layout->key('bar')->limit(4));
95+
96+
$menu = $flexible->layoutsMenu();
97+
98+
expect($menu)->toBeArray();
99+
expect($menu)->toHaveCount(2);
100+
expect(array_key_exists('id', $menu[0]))->toBeFalse();
101+
expect(array_key_exists('limit', $menu[0]))->toBeTrue();
102+
});

0 commit comments

Comments
 (0)