Add trait Methods

This commit is contained in:
Giuseppe Criscione 2022-11-19 22:37:50 +01:00
parent df0a8c8307
commit 529b542092

View File

@ -0,0 +1,37 @@
<?php
namespace Formwork\Traits;
use BadMethodCallException;
trait Methods
{
/**
* Array of methods
*/
protected array $methods = [];
/**
* Return whether a method is defined in the `$method` property
*/
public function hasMethod(string $method): bool
{
return isset($this->methods[$method]);
}
/**
* Call a method defined in the `$method` property
*/
protected function callMethod(string $method, array $arguments)
{
return $this->methods[$method](...$arguments);
}
public function __call(string $name, array $arguments)
{
if ($this->hasMethod($name)) {
return $this->callMethod($name, $arguments);
}
throw new BadMethodCallException(sprintf('Call to undefined method %s::%s()', static::class, $name));
}
}