Skip to content

Commit d696026

Browse files
committed
Added layout manipulation methods (get, set, unset, isset & offset)
1 parent c009c39 commit d696026

File tree

2 files changed

+175
-2
lines changed

2 files changed

+175
-2
lines changed

src/Layout.php

Lines changed: 125 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22

33
namespace Whitecube\LaravelFlexibleContent;
44

5-
use Whitecube\LaravelFlexibleContent\Contracts\Layout as LayoutInterface;
5+
use ArrayAccess;
66
use Illuminate\Support\Str;
77
use Illuminate\Database\Eloquent\Concerns\HasAttributes;
8+
use Illuminate\Database\Eloquent\Concerns\HidesAttributes;
9+
use Whitecube\LaravelFlexibleContent\Contracts\Layout as LayoutInterface;
810

9-
class Layout implements LayoutInterface
11+
class Layout implements LayoutInterface, ArrayAccess
1012
{
1113
use HasAttributes;
14+
use HidesAttributes;
1215

1316
/**
1417
* A short unique name for this Layout, usually used by front-end components
@@ -134,4 +137,124 @@ public function make(?string $id = null, array $attributes = []) : LayoutInterfa
134137
->attributes($attributes, true)
135138
->limit($this->limit);
136139
}
140+
141+
/**
142+
* Get the value indicating whether the IDs are incrementing (they are not since this is not a model).
143+
*
144+
* @return bool
145+
*/
146+
public function getIncrementing()
147+
{
148+
return false;
149+
}
150+
151+
/**
152+
* Determine if the layout uses timestamps (it doesn't since this is not a model).
153+
*
154+
* @return bool
155+
*/
156+
public function usesTimestamps()
157+
{
158+
return false;
159+
}
160+
161+
/**
162+
* Check if relation exists (it doesn't since this is not a model).
163+
*
164+
* @return bool
165+
*/
166+
protected function relationLoaded()
167+
{
168+
return false;
169+
}
170+
171+
/**
172+
* Dynamically retrieve attributes on the layout.
173+
*
174+
* @param string $attribute
175+
* @return mixed
176+
*/
177+
public function __get($attribute)
178+
{
179+
return $this->getAttribute($attribute);
180+
}
181+
182+
/**
183+
* Dynamically set attributes on the layout.
184+
*
185+
* @param string $attribute
186+
* @param mixed $value
187+
* @return void
188+
*/
189+
public function __set($attribute, $value)
190+
{
191+
$this->setAttribute($attribute, $value);
192+
}
193+
194+
/**
195+
* Determine if an attribute exists on the layout.
196+
*
197+
* @param string $attribute
198+
* @return bool
199+
*/
200+
public function __isset($attribute)
201+
{
202+
return $this->offsetExists($attribute);
203+
}
204+
205+
/**
206+
* Unset an attribute on the layout.
207+
*
208+
* @param string $attribute
209+
* @return void
210+
*/
211+
public function __unset($attribute)
212+
{
213+
$this->offsetUnset($attribute);
214+
}
215+
216+
/**
217+
* Determine if the given attribute exists.
218+
*
219+
* @param mixed $attribute
220+
* @return bool
221+
*/
222+
public function offsetExists($attribute)
223+
{
224+
return ! is_null($this->getAttribute($attribute));
225+
}
226+
227+
/**
228+
* Get the value for a given offset.
229+
*
230+
* @param mixed $attribute
231+
* @return mixed
232+
*/
233+
public function offsetGet($attribute)
234+
{
235+
return $this->getAttribute($attribute);
236+
}
237+
238+
/**
239+
* Set the value for a given offset.
240+
*
241+
* @param mixed $attribute
242+
* @param mixed $value
243+
* @return void
244+
*/
245+
public function offsetSet($attribute, $value)
246+
{
247+
$this->setAttribute($attribute, $value);
248+
}
249+
250+
/**
251+
* Unset the value for a given offset.
252+
*
253+
* @param mixed $attribute
254+
* @return void
255+
*/
256+
public function offsetUnset($attribute)
257+
{
258+
unset($this->attributes[$attribute]);
259+
}
137260
}

tests/Unit/LayoutManipulationTest.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace Tests\Unit;
4+
5+
use Whitecube\LaravelFlexibleContent\Layout;
6+
7+
it('can generate random uuid as layout ID and keep layout key', function() {
8+
$layout = (new Layout())->key('foo')->make(null, ['test' => true]);
9+
10+
expect($layout->getKey())->toBe('foo');
11+
expect($layout->getId())->not->toBeNull();
12+
});
13+
14+
it('can access layout attributes', function() {
15+
$layout = (new Layout())->key('foo')->make(null, ['test' => true]);
16+
17+
expect($layout->getAttribute('test'))->toBeTrue();
18+
expect($layout['test'])->toBeTrue();
19+
expect($layout->test)->toBeTrue();
20+
});
21+
22+
it('cannot access undefined layout attributes', function() {
23+
$layout = (new Layout())->key('foo')->make(null, ['test' => true]);
24+
25+
expect($layout->getAttribute('something'))->toBeNull();
26+
expect($layout['something'])->toBeNull();
27+
expect($layout->something)->toBeNull();
28+
});
29+
30+
it('can test layout attribute existence', function() {
31+
$layout = (new Layout())->key('foo')->make(null, ['test' => true]);
32+
33+
expect(isset($layout->test))->toBeTrue();
34+
expect(isset($layout->something))->toBeFalse();
35+
expect(isset($layout['test']))->toBeTrue();
36+
expect(isset($layout['something']))->toBeFalse();
37+
});
38+
39+
it('can unset layout attributes', function() {
40+
$layout = (new Layout())->key('foo')->make(null, ['test' => true, 'something' => true]);
41+
42+
expect($layout->test)->toBeTrue();
43+
expect($layout['something'])->toBeTrue();
44+
45+
unset($layout->test);
46+
unset($layout['something']);
47+
48+
expect($layout->test)->toBeNull();
49+
expect($layout['something'])->toBeNull();
50+
});

0 commit comments

Comments
 (0)