Skip to content

Commit

Permalink
add ArrayAccess and magic methods for better accessibility
Browse files Browse the repository at this point in the history
  • Loading branch information
teikun-86 committed Dec 25, 2023
1 parent 4089f91 commit 89bdfa4
Showing 1 changed file with 47 additions and 2 deletions.
49 changes: 47 additions & 2 deletions src/Entities/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

namespace Teikun86\Tripay\Entities;

use ArrayAccess;
use Illuminate\Contracts\Support\Arrayable;

abstract class Entity implements Arrayable
abstract class Entity implements Arrayable, ArrayAccess
{
protected $attributes = [];

Expand All @@ -22,7 +23,6 @@ public function __get(string $key): mixed
{
return $this->attributes[$key];
}

public function toArray(): array
{
$result = [];
Expand Down Expand Up @@ -73,4 +73,49 @@ public function isEmpty(): bool
{
return empty($this->getAttributes());
}

public function offsetExists($key): bool
{
return isset($this->attributes[$key]);
}

public function offsetGet($key): mixed
{
return $this->attributes[$key];
}

public function offsetSet($key, $value): void
{
if (is_null($key)) {
$this->attributes[] = $value;
} else {
$this->attributes[$key] = $value;
}
}

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

public function __unset(string $key): void
{
$this->remove($key);
}

public function __isset(string $key): bool
{
return $this->has($key);
}

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

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

}

0 comments on commit 89bdfa4

Please sign in to comment.