2022-09-29 20:09:31 -04:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace App\Entity;
|
|
|
|
|
2022-09-30 10:49:02 -04:00
|
|
|
use InvalidArgumentException;
|
2022-09-29 20:09:31 -04:00
|
|
|
|
2022-10-14 11:16:53 -04:00
|
|
|
/**
|
|
|
|
* Remove the need for all the Doctrine getter/setter Entity boilerplate
|
|
|
|
*/
|
2022-10-14 11:54:45 -04:00
|
|
|
trait GetSetTrait {
|
|
|
|
public function __get(string $name): mixed
|
|
|
|
{
|
|
|
|
if (property_exists($this, $name))
|
|
|
|
{
|
|
|
|
return $this->$name;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function __set(string $name, mixed $value): void
|
|
|
|
{
|
|
|
|
if ( ! property_exists($this, $name))
|
|
|
|
{
|
|
|
|
throw new InvalidArgumentException("Undefined property: {$name}");
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->$name = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function __call(string $name, array $arguments): mixed
|
|
|
|
{
|
|
|
|
if (method_exists($this, $name))
|
|
|
|
{
|
|
|
|
return $this->$name(...$arguments);
|
|
|
|
}
|
2022-09-29 20:09:31 -04:00
|
|
|
|
2022-10-14 11:16:53 -04:00
|
|
|
// Getters
|
|
|
|
if (empty($arguments))
|
|
|
|
{
|
2022-10-14 11:54:45 -04:00
|
|
|
// The property as a method is required for Twig it appears
|
2022-10-14 11:16:53 -04:00
|
|
|
if (property_exists($this, $name))
|
|
|
|
{
|
2022-10-14 11:54:45 -04:00
|
|
|
return $this->$name;
|
2022-10-14 11:16:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (str_starts_with($name, 'get'))
|
|
|
|
{
|
2022-10-14 11:54:45 -04:00
|
|
|
return $this->__get(lcfirst(substr($name, 3)));
|
2022-10-14 11:16:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (str_starts_with($name, 'is'))
|
|
|
|
{
|
2022-10-14 11:54:45 -04:00
|
|
|
return $this->__get(lcfirst(substr($name, 2)));
|
2022-10-14 11:16:53 -04:00
|
|
|
}
|
2022-10-14 11:54:45 -04:00
|
|
|
|
|
|
|
throw new InvalidArgumentException("Undefined method: {$name}");
|
2022-10-14 11:16:53 -04:00
|
|
|
}
|
2022-09-29 20:09:31 -04:00
|
|
|
|
2022-10-14 11:16:53 -04:00
|
|
|
// Setters
|
2022-10-14 11:54:45 -04:00
|
|
|
if (str_starts_with($name, 'set'))
|
|
|
|
{
|
|
|
|
$var = lcfirst(substr($name, 3));
|
|
|
|
|
|
|
|
$this->__set($var, ...$arguments);
|
2022-09-29 20:09:31 -04:00
|
|
|
|
2022-10-14 11:54:45 -04:00
|
|
|
return $this;
|
|
|
|
}
|
2022-09-30 10:49:02 -04:00
|
|
|
|
2022-10-14 11:54:45 -04:00
|
|
|
throw new InvalidArgumentException("Undefined method: {$name}");
|
|
|
|
}
|
2022-09-29 20:09:31 -04:00
|
|
|
}
|