Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add new setter/getter for Entity #7230

Merged
merged 2 commits into from
Feb 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
3 changes: 3 additions & 0 deletions user_guide_src/source/changelogs/v4.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ Others
Model
=====

- Added special getter/setter to Entity to avoid method name conflicts.
See :ref:`entities-special-getter-setter`.

Libraries
=========

Expand Down
19 changes: 19 additions & 0 deletions user_guide_src/source/models/entities.rst
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,25 @@ business logic and create objects that are pleasant to use.

.. literalinclude:: entities/007.php

.. _entities-special-getter-setter:

Special Getter/Setter
---------------------

.. versionadded:: 4.4.0

For example, if your Entity's parent class already has a ``getParent()`` method
defined, and your Entity also has a column named ``parent``, when you try to add
business logic to the ``getParent()`` method in your Entity class, the method is
already defined.

In such a case, you can use the special getter/setter. Instead of ``getX()``/``setX()``,
set ``_getX()``/``_setX()``.

In the above example, if your Entity has the ``_getParent()`` method, the method
will be used when you get ``$entity->parent``, and the ``_setParent()`` method
will be used when you set ``$entity->parent``.

Data Mapping
============

Expand Down