Skip to content

Commit 3b2fcb8

Browse files
committed
Added Arrayable (toArray method) to Layout
1 parent d696026 commit 3b2fcb8

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/Layout.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44

55
use ArrayAccess;
66
use Illuminate\Support\Str;
7+
use Illuminate\Contracts\Support\Arrayable;
78
use Illuminate\Database\Eloquent\Concerns\HasAttributes;
89
use Illuminate\Database\Eloquent\Concerns\HidesAttributes;
910
use Whitecube\LaravelFlexibleContent\Contracts\Layout as LayoutInterface;
1011

11-
class Layout implements LayoutInterface, ArrayAccess
12+
class Layout implements LayoutInterface, ArrayAccess, Arrayable
1213
{
1314
use HasAttributes;
1415
use HidesAttributes;
@@ -257,4 +258,14 @@ public function offsetUnset($attribute)
257258
{
258259
unset($this->attributes[$attribute]);
259260
}
261+
262+
/**
263+
* Convert the layout instance to an array.
264+
*
265+
* @return array
266+
*/
267+
public function toArray()
268+
{
269+
return $this->attributesToArray();
270+
}
260271
}

tests/Unit/LayoutManipulationTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,13 @@
4848
expect($layout->test)->toBeNull();
4949
expect($layout['something'])->toBeNull();
5050
});
51+
52+
it('can convert layout attributes to array', function() {
53+
$layout = (new Layout())->key('foo')->make(null, ['test' => true, 'something' => false]);
54+
55+
$converted = $layout->toArray();
56+
57+
expect($converted)->toBeArray();
58+
expect($converted['test'])->toBeTrue();
59+
expect($converted['something'])->toBeFalse();
60+
});

0 commit comments

Comments
 (0)