Skip to content

Commit

Permalink
make Data class array accessibe
Browse files Browse the repository at this point in the history
  • Loading branch information
Arno Visker committed Nov 18, 2024
1 parent ed65b46 commit b6862d5
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 1 deletion.
62 changes: 61 additions & 1 deletion src/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace Avmg\PhpSimpleUtilities;

use ArrayAccess;
use BackedEnum;
use InvalidArgumentException;
use JsonSerializable;
Expand All @@ -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.
Expand Down Expand Up @@ -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)
);
}
}
26 changes: 26 additions & 0 deletions tests/Unit/DataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
}

0 comments on commit b6862d5

Please sign in to comment.