Skip to content

Commit 57d6b4a

Browse files
robinmalburndavidhemphill
authored andcommitted
Adds array access interface to presenter class and supporting unit tests (#17)
* Adds array access interface to presenter class and supporting unit tests. * Updates unit test to use annotation based exception expectation.
1 parent 61bcbf7 commit 57d6b4a

File tree

2 files changed

+118
-2
lines changed

2 files changed

+118
-2
lines changed

src/Presenter.php

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
use Illuminate\Support\Str;
66
use Illuminate\Contracts\Support\Jsonable;
77
use Illuminate\Contracts\Support\Arrayable;
8+
use ArrayAccess;
9+
use BadMethodCallException;
810

9-
abstract class Presenter implements Jsonable, Arrayable
11+
abstract class Presenter implements Jsonable, Arrayable, ArrayAccess
1012
{
1113
/**
1214
* The attributes that should be visible in arrays.
@@ -261,4 +263,66 @@ protected function getArrayableItems($values)
261263

262264
return $values;
263265
}
264-
}
266+
267+
/**
268+
* Return true if the property is set and not null.
269+
*
270+
* @param string $name
271+
* @return bool
272+
*/
273+
public function __isset($name)
274+
{
275+
return $this->offsetExists($name);
276+
}
277+
278+
/**
279+
* Return true if the offset exists and is not null.
280+
*
281+
* @param mixed $offset
282+
* @return bool
283+
*/
284+
public function offsetExists($offset)
285+
{
286+
return $this->{$offset} !== null;
287+
}
288+
289+
/**
290+
* Return the value at the specified offset.
291+
*
292+
* @param mixed $offset
293+
* @return mixed
294+
*/
295+
public function offsetGet($offset)
296+
{
297+
return $this->{$offset};
298+
}
299+
300+
/**
301+
* Required implementation to satisfy the ArrayAccess interface,
302+
* but throws as a BadMethodCallException as this is a read only
303+
* implementation.
304+
*
305+
* @param mixed $offset
306+
* @param mixed $value
307+
* @return void
308+
* @throws \BadMethodCallException
309+
*/
310+
public function offsetSet($offset, $value)
311+
{
312+
throw new BadMethodCallException('Not implemented - read only implementation.');
313+
}
314+
315+
/**
316+
* Required implementation to satisfy the ArrayAccess interface,
317+
* but throws as a BadMethodCallException as this is a read only
318+
* implementation.
319+
*
320+
* @param mixed $offset
321+
* @return void
322+
* @throws \BadMethodCallException
323+
*/
324+
public function offsetUnset($offset)
325+
{
326+
throw new BadMethodCallException('Not implemented - read only implementation.');
327+
}
328+
}

tests/PresenterTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,23 @@ public function you_can_transform_a_collection_of_eloquent_models()
213213
$this->assertEquals('David Lee Hemphill', $firstUser->full_name);
214214
}
215215

216+
/** @test */
217+
public function you_can_access_a_collection_of_eloquent_models()
218+
{
219+
$sampleModel = $this->createModel();
220+
221+
$users = collect([$sampleModel])->present(SamplePresenter::class);
222+
223+
$this->assertEquals(
224+
collect(['David']),
225+
$users->pluck('first_name')
226+
);
227+
228+
$this->assertEquals(
229+
collect(['David Lee Hemphill']),
230+
$users->pluck('full_name'));
231+
}
232+
216233
/** @test */
217234
public function you_can_present_a_collection_of_models_using_a_closure()
218235
{
@@ -497,6 +514,41 @@ public function a_presenter_can_specify_attributes_to_show_in_json_or_array_outp
497514

498515
$this->assertEquals($desired, (string) $presentedModel);
499516
}
517+
518+
/** @test */
519+
public function it_can_be_array_accessed()
520+
{
521+
$sampleModel = TestModel::create([
522+
'first_name' => 'David',
523+
'last_name' => 'Hemphill',
524+
'created_at' => '2015-10-14 12:00:00',
525+
'updated_at' => '2015-10-14 12:00:00',
526+
]);
527+
528+
$presenter = $sampleModel->present(SamplePresenter::class);
529+
530+
$this->assertEquals('David', $presenter['first_name']);
531+
532+
$this->assertEquals('David Lee Hemphill', $presenter['full_name']);
533+
}
534+
535+
/**
536+
* @test
537+
* @expectedException BadMethodCallException
538+
* */
539+
public function it_cannot_be_written_to_via_array_access()
540+
{
541+
$sampleModel = TestModel::create([
542+
'first_name' => 'David',
543+
'last_name' => 'Hemphill',
544+
'created_at' => '2015-10-14 12:00:00',
545+
'updated_at' => '2015-10-14 12:00:00',
546+
]);
547+
548+
$presenter = $sampleModel->present(SamplePresenter::class);
549+
550+
$presenter['first_name'] = 'should not update';
551+
}
500552
}
501553

502554
class TestModel extends Model

0 commit comments

Comments
 (0)