Skip to content

Commit

Permalink
feat: add new setter/getter method for Entity
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Feb 7, 2023
1 parent 31a2e83 commit 4ca42db
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 3 deletions.
18 changes: 15 additions & 3 deletions system/Entity/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -456,11 +456,19 @@ public function __set(string $key, $value = null)

$value = $this->castAs($value, $key, 'set');

// if a set* method exists for this key, use that method to
// if a setter method exists for this key, use that method to
// insert this value. should be outside $isNullable check,
// so maybe wants to do sth with null value automatically
$method = 'set' . str_replace(' ', '', ucwords(str_replace(['-', '_'], ' ', $key)));

// If a "`_set` + $key" method exists, it is a setter.
if (method_exists($this, '_' . $method)) {
$this->{'_' . $method}($value);

return $this;
}

// If a "`set` + $key" method exists, it is also a setter.
if (method_exists($this, $method) && $method !== 'setAttributes') {
$this->{$method}($value);

Expand Down Expand Up @@ -499,9 +507,13 @@ public function __get(string $key)
// Convert to CamelCase for the method
$method = 'get' . str_replace(' ', '', ucwords(str_replace(['-', '_'], ' ', $key)));

// if a get* method exists for this key,
// if a getter method exists for this key,
// use that method to insert this value.
if (method_exists($this, $method)) {
if (method_exists($this, '_' . $method)) {
// If a "`_get` + $key" method exists, it is a getter.
$result = $this->{'_' . $method}();
} elseif (method_exists($this, $method)) {
// If a "`get` + $key" method exists, it is also a getter.
$result = $this->{$method}();
}

Expand Down
59 changes: 59 additions & 0 deletions tests/system/Entity/EntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,19 @@ public function testGetterSetters()
$this->assertSame('bar:thanks:bar', $entity->bar);
}

public function testNewGetterSetters()
{
$entity = $this->getNewSetterGetterEntity();

$entity->bar = 'thanks';

$this->assertSame('bar:thanks:bar', $entity->bar);

$entity->setBar('BAR');

$this->assertSame('BAR', $entity->getBar());
}

public function testUnsetUnsetsAttribute()
{
$entity = $this->getEntity();
Expand Down Expand Up @@ -1096,6 +1109,52 @@ public function getFakeBar()
};
}

protected function getNewSetterGetterEntity()
{
return new class () extends Entity {
protected $attributes = [
'foo' => null,
'bar' => null,
'default' => 'sumfin',
'created_at' => null,
];
protected $original = [
'foo' => null,
'bar' => null,
'default' => 'sumfin',
'created_at' => null,
];
protected $datamap = [
'createdAt' => 'created_at',
];
private string $bar;

public function setBar($value)
{
$this->bar = $value;

return $this;
}

public function getBar()
{
return $this->bar;
}

public function _setBar($value)
{
$this->attributes['bar'] = "bar:{$value}";

return $this;
}

public function _getBar()
{
return "{$this->attributes['bar']}:bar";
}
};
}

protected function getMappedEntity()
{
return new class () extends Entity {
Expand Down

0 comments on commit 4ca42db

Please sign in to comment.