Skip to content

Commit

Permalink
Merge pull request #18 from alexjoffroy/feature/implements-array-acce…
Browse files Browse the repository at this point in the history
…ss-on-sheet

Implements ArrayAccess on Sheet class
  • Loading branch information
freekmurze authored Jun 5, 2018
2 parents 7e49067 + 758486d commit 324002c
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 6 deletions.
49 changes: 43 additions & 6 deletions src/Sheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Spatie\Sheets;

class Sheet
use ArrayAccess;

class Sheet implements ArrayAccess
{
/** @var array */
protected $attributes;
Expand All @@ -13,6 +15,46 @@ public function __construct(array $attributes = [])
}

public function __get(string $key)
{
return $this->getAttribute($key);
}

public function __set(string $key, $value)
{
$this->attributes[$key] = $value;
}

public function __isset(string $key)
{
return isset($this->attributes[$key]);
}

public function __unset(string $key)
{
unset($this->attributes[$key]);
}

public function offsetExists($key)
{
return !is_null($this->getAttribute($key));
}

public function offsetGet($key)
{
return $this->getAttribute($key);
}

public function offsetSet($key, $value)
{
$this->$key = $value;
}

public function offsetUnset($key)
{
unset($this->attributes[$key]);
}

protected function getAttribute(string $key)
{
$value = $this->attributes[$key] ?? null;

Expand All @@ -24,9 +66,4 @@ public function __get(string $key)

return $value;
}

public function __isset(string $key)
{
return ! is_null($this->$key);
}
}
87 changes: 87 additions & 0 deletions tests/SheetTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

namespace Spatie\Sheets\Tests;

use ArrayAccess;
use ReflectionClass;
use Spatie\Sheets\Sheet;
use PHPUnit\Framework\TestCase;

class SheetTest extends TestCase
{
/** @test */
public function it_can_create_a_sheet_with_attributes()
{
$attributes = [
'foo' => 'bar',
'hello' => 'world',
];
$sheet = new Sheet($attributes);

$reflection = (new ReflectionClass($sheet))->getProperty('attributes');
$reflection->setAccessible(true);

$this->assertEquals($attributes, $reflection->getValue($sheet));
}

/** @test */
public function it_can_get_a_specific_attribute()
{
$sheet = new Sheet(['foo' => 'bar']);

$this->assertEquals('bar', $sheet->foo);
}

/** @test */
public function it_can_get_null_for_a_non_existing_attribute()
{
$sheet = new Sheet();

$this->assertNull($sheet->unknown);
}

/** @test */
public function it_can_set_a_specific_attribute()
{
$sheet = new Sheet();

$sheet->foo = 'bar';

$reflection = (new ReflectionClass($sheet))->getProperty('attributes');
$reflection->setAccessible(true);

$this->assertEquals(['foo' => 'bar'], $reflection->getValue($sheet));
}

/** @test */
public function it_can_be_extended_with_accessor()
{
$child = new class extends Sheet {
public function getFooAttribute($original)
{
return 'baz';
}
};

$sheet = new $child(['foo' => 'bar']);

$this->assertNotEquals('bar', $sheet->foo);
$this->assertEquals('baz', $sheet->foo);
}

/** @test */
public function it_implements_array_access()
{
$sheet = new Sheet(['foo' => 'bar']);

$this->assertInstanceOf(ArrayAccess::class, $sheet);
$this->assertEquals('bar', $sheet['foo']);
$this->assertNull($sheet['unknown']);

unset($sheet['foo']);
$this->assertNull($sheet['foo']);

$sheet['foo'] = 'baz';
$this->assertEquals('baz', $sheet['foo']);
}
}

0 comments on commit 324002c

Please sign in to comment.