From 4ca42db8505a3852f9605d6e5470129ea0eb4c9b Mon Sep 17 00:00:00 2001 From: kenjis Date: Tue, 7 Feb 2023 19:05:40 +0900 Subject: [PATCH] feat: add new setter/getter method for Entity --- system/Entity/Entity.php | 18 +++++++-- tests/system/Entity/EntityTest.php | 59 ++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 3 deletions(-) diff --git a/system/Entity/Entity.php b/system/Entity/Entity.php index 5220ae5b0aa8..ffb693cae467 100644 --- a/system/Entity/Entity.php +++ b/system/Entity/Entity.php @@ -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); @@ -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}(); } diff --git a/tests/system/Entity/EntityTest.php b/tests/system/Entity/EntityTest.php index 3e4a196fc2ed..867c02b1f597 100644 --- a/tests/system/Entity/EntityTest.php +++ b/tests/system/Entity/EntityTest.php @@ -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(); @@ -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 {