diff --git a/src/Sheet.php b/src/Sheet.php index e654667..1f8b0d7 100644 --- a/src/Sheet.php +++ b/src/Sheet.php @@ -2,7 +2,9 @@ namespace Spatie\Sheets; -class Sheet +use ArrayAccess; + +class Sheet implements ArrayAccess { /** @var array */ protected $attributes; @@ -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; @@ -24,9 +66,4 @@ public function __get(string $key) return $value; } - - public function __isset(string $key) - { - return ! is_null($this->$key); - } } diff --git a/tests/SheetTest.php b/tests/SheetTest.php new file mode 100644 index 0000000..633d80a --- /dev/null +++ b/tests/SheetTest.php @@ -0,0 +1,87 @@ + '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']); + } +}