mirror of
https://github.com/getformwork/formwork.git
synced 2025-01-17 21:49:04 +01:00
Add trait Methods
This commit is contained in:
parent
df0a8c8307
commit
529b542092
37
formwork/src/Traits/Methods.php
Normal file
37
formwork/src/Traits/Methods.php
Normal 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));
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user