Skip to content

Commit 4ca42db

Browse files
committed
feat: add new setter/getter method for Entity
1 parent 31a2e83 commit 4ca42db

File tree

2 files changed

+74
-3
lines changed

2 files changed

+74
-3
lines changed

system/Entity/Entity.php

+15-3
Original file line numberDiff line numberDiff line change
@@ -456,11 +456,19 @@ public function __set(string $key, $value = null)
456456

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

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

464+
// If a "`_set` + $key" method exists, it is a setter.
465+
if (method_exists($this, '_' . $method)) {
466+
$this->{'_' . $method}($value);
467+
468+
return $this;
469+
}
470+
471+
// If a "`set` + $key" method exists, it is also a setter.
464472
if (method_exists($this, $method) && $method !== 'setAttributes') {
465473
$this->{$method}($value);
466474

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

502-
// if a get* method exists for this key,
510+
// if a getter method exists for this key,
503511
// use that method to insert this value.
504-
if (method_exists($this, $method)) {
512+
if (method_exists($this, '_' . $method)) {
513+
// If a "`_get` + $key" method exists, it is a getter.
514+
$result = $this->{'_' . $method}();
515+
} elseif (method_exists($this, $method)) {
516+
// If a "`get` + $key" method exists, it is also a getter.
505517
$result = $this->{$method}();
506518
}
507519

tests/system/Entity/EntityTest.php

+59
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,19 @@ public function testGetterSetters()
8282
$this->assertSame('bar:thanks:bar', $entity->bar);
8383
}
8484

85+
public function testNewGetterSetters()
86+
{
87+
$entity = $this->getNewSetterGetterEntity();
88+
89+
$entity->bar = 'thanks';
90+
91+
$this->assertSame('bar:thanks:bar', $entity->bar);
92+
93+
$entity->setBar('BAR');
94+
95+
$this->assertSame('BAR', $entity->getBar());
96+
}
97+
8598
public function testUnsetUnsetsAttribute()
8699
{
87100
$entity = $this->getEntity();
@@ -1096,6 +1109,52 @@ public function getFakeBar()
10961109
};
10971110
}
10981111

1112+
protected function getNewSetterGetterEntity()
1113+
{
1114+
return new class () extends Entity {
1115+
protected $attributes = [
1116+
'foo' => null,
1117+
'bar' => null,
1118+
'default' => 'sumfin',
1119+
'created_at' => null,
1120+
];
1121+
protected $original = [
1122+
'foo' => null,
1123+
'bar' => null,
1124+
'default' => 'sumfin',
1125+
'created_at' => null,
1126+
];
1127+
protected $datamap = [
1128+
'createdAt' => 'created_at',
1129+
];
1130+
private string $bar;
1131+
1132+
public function setBar($value)
1133+
{
1134+
$this->bar = $value;
1135+
1136+
return $this;
1137+
}
1138+
1139+
public function getBar()
1140+
{
1141+
return $this->bar;
1142+
}
1143+
1144+
public function _setBar($value)
1145+
{
1146+
$this->attributes['bar'] = "bar:{$value}";
1147+
1148+
return $this;
1149+
}
1150+
1151+
public function _getBar()
1152+
{
1153+
return "{$this->attributes['bar']}:bar";
1154+
}
1155+
};
1156+
}
1157+
10991158
protected function getMappedEntity()
11001159
{
11011160
return new class () extends Entity {

0 commit comments

Comments
 (0)