collection-crud/src/Entity/GetSetTrait.php

34 lines
703 B
PHP

<?php declare(strict_types=1);
namespace App\Entity;
trait GetSetTrait {
public function __call(string $name, array $arguments): mixed
{
if (method_exists($this, $name))
{
return $this->$name(...$arguments);
}
// Apparently Doctrine first tries the method with the same
// name as the property, instead of with the get prefix
if (property_exists($this, $name) && empty($arguments))
{
return $this->$name;
}
if (str_starts_with('set', $name))
{
$var = lcfirst(str_replace('set', '', $name));
if (property_exists($this, $var))
{
$this->$name = $arguments[0];
}
return $this;
}
throw new \InvalidArgumentException("Undefined method: {$name}");
}
}