Skip to content

Commit

Permalink
Merge pull request #9 from Gman98ish/array-access
Browse files Browse the repository at this point in the history
Change ArrayAccess to load from object rather than array
  • Loading branch information
Gman98ish authored Jan 24, 2023
2 parents eec7913 + 19dd41b commit 4d746b8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,22 @@ public function toArray()

public function offsetExists($offset)
{
return isset($this->props[$offset]);
return isset($this->props->{$offset});
}

public function offsetGet($offset)
{
return $this->props[$offset];
return $this->props->{$offset};
}

public function offsetSet($offset, $value)
{
$this->props[$offset] = $value;
$this->props->{$offset} = $value;
}

public function offsetUnset($offset)
{
unset($this->props[$offset]);
unset($this->props->{$offset});
}

private function findSubEntities($props)
Expand Down
18 changes: 18 additions & 0 deletions tests/EntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public function sets_attributes_from_an_array()
$this->assertEquals(1, $this->entity->id);
$this->assertEquals('John', $this->entity->first_name);
$this->assertEquals('Doe', $this->entity->last_name);
$this->assertEquals('[email protected]', $this->entity->email->personal);
}

/**
Expand All @@ -33,6 +34,20 @@ public function can_convert_to_an_array()
$this->assertEquals(1, $array['id']);
$this->assertEquals('John', $array['first_name']);
$this->assertEquals('Doe', $array['last_name']);
$this->assertEquals('[email protected]', $array['email']['personal']);
}

/**
* @test
*/
public function can_access_as_array()
{
$this->loadSampleEntity();

$this->assertEquals(1, $this->entity['id']);
$this->assertEquals('John', $this->entity['first_name']);
$this->assertEquals('Doe', $this->entity['last_name']);
$this->assertEquals('[email protected]', $this->entity['email']['personal']);
}

protected function loadSampleEntity()
Expand All @@ -41,6 +56,9 @@ protected function loadSampleEntity()
'id' => 1,
'first_name' => 'John',
'last_name' => 'Doe',
'email' => (object) [
'personal' => '[email protected]'
]
]);
}
}

0 comments on commit 4d746b8

Please sign in to comment.