From b6862d5a60a02e6b842af54a03b8e5ca873c93ca Mon Sep 17 00:00:00 2001 From: Arno Visker Date: Mon, 18 Nov 2024 09:22:35 +0100 Subject: [PATCH] make Data class array accessibe --- src/Data.php | 62 ++++++++++++++++++++++++++++++++++++++++- tests/Unit/DataTest.php | 26 +++++++++++++++++ 2 files changed, 87 insertions(+), 1 deletion(-) diff --git a/src/Data.php b/src/Data.php index 7bf65d7..7de1699 100644 --- a/src/Data.php +++ b/src/Data.php @@ -3,6 +3,7 @@ namespace Avmg\PhpSimpleUtilities; +use ArrayAccess; use BackedEnum; use InvalidArgumentException; use JsonSerializable; @@ -19,7 +20,7 @@ * It includes functionality for creating instances from arrays, type casting, * and converting objects back to arrays. */ -abstract class Data implements JsonSerializable +abstract class Data implements JsonSerializable, ArrayAccess { /** * Create a new instance from an array of attributes. @@ -241,4 +242,63 @@ private function itemToArray(mixed $item): mixed return $item; } + + /** + * ArrayAccess implementation + */ + + /** + * Whether an offset exists + * + * @param mixed $offset + * @return bool + */ + public function offsetExists(mixed $offset): bool + { + return property_exists($this, $offset); + } + + /** + * Get an offset + * + * @param mixed $offset + * @return mixed + */ + public function offsetGet(mixed $offset): mixed + { + return property_exists($this, $offset) ? $this->{$offset} : null; + } + + /** + * Set an offset + * + * @param mixed $offset + * @param mixed $value + * @return void + * @throws InvalidArgumentException + */ + public function offsetSet(mixed $offset, mixed $value): void + { + if (!property_exists($this, $offset)) { + throw new InvalidArgumentException( + sprintf("Cannot set non-existent property '%s' in %s.", $offset, static::class) + ); + } + + $this->{$offset} = $value; + } + + /** + * Unset an offset + * + * @param mixed $offset + * @return void + * @throws InvalidArgumentException + */ + public function offsetUnset(mixed $offset): void + { + throw new InvalidArgumentException( + sprintf("Cannot unset property '%s' in %s. Properties in Data objects are immutable.", $offset, static::class) + ); + } } \ No newline at end of file diff --git a/tests/Unit/DataTest.php b/tests/Unit/DataTest.php index ae45fdf..1a05aa7 100644 --- a/tests/Unit/DataTest.php +++ b/tests/Unit/DataTest.php @@ -297,4 +297,30 @@ public function testFromJson() $productData = ProductData::fromJson($json); $this->assertEquals('Gadget', $productData->name); } + + /** + * Test array access functionality + */ + public function testArrayAccess(): void + { + // Create a concrete implementation of the abstract Data class for testing + $testData = UserData::from(['name' => 'John', 'age' => 30]); + + // Test array access functionality + $this->assertTrue(isset($testData['name'])); + $this->assertFalse(isset($testData['nonexistent'])); + $this->assertEquals('John', $testData['name']); + $this->assertEquals(30, $testData['age']); + + // Test setting a value + $testData['name'] = 'Jane'; + $this->assertEquals('Jane', $testData['name']); + + // Test accessing non-existent property + $this->assertNull($testData['nonexistent']); + + // Test unsetting a property (should throw exception) + $this->expectException(\InvalidArgumentException::class); + unset($testData['name']); + } } \ No newline at end of file