From e134e8133432933ee2bb84da764ec83f404656c8 Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Fri, 23 May 2025 19:02:14 +0000 Subject: [PATCH] - Some minor bug fixes made and redefined constructor to match PHP 8+ --- src/Privilege.php | 31 +++++++++++++++---------------- src/Taxus.php | 5 ++--- 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/src/Privilege.php b/src/Privilege.php index d14fefe..64ceccf 100644 --- a/src/Privilege.php +++ b/src/Privilege.php @@ -2,26 +2,25 @@ namespace Taxus; +use http\Exception\RuntimeException; + class Privilege { - public string $name; - - public string $description; - - public mixed $error; - - public $success; - - public $function; - public ? bool $granted = false; - public function __construct(string $name, string $description = "", ? Callable $function = null, ? Callable $success = null, ? Callable $error = null) + public function __construct( + public string $name, + public string $description = "", + public mixed /* Callable */ $function = null, + public mixed /*? Callable*/ $success = null, + public mixed /*? Callable*/ $error = null, + public ? array $testableArguments = null, + ) { - $this->name = $name; - $this->description = $description; - $this->function = $function; - $this->success = $success; - $this->error = $error; + foreach(array_filter([ 'function' => $function, 'success' => $success, 'error' => $error ]) as $item) { + if ( ! is_callable($item) ) { + throw new RuntimeException("Your {$item} property must be Callable"); + } + } } } diff --git a/src/Taxus.php b/src/Taxus.php index 36d4a6c..99eba55 100644 --- a/src/Taxus.php +++ b/src/Taxus.php @@ -5,7 +5,7 @@ namespace Taxus; class Taxus { # @ArrayOf Privilege - protected array $list = []; + public array $list = []; # @ArrayOf PermissionGrantInterface protected array $gates = []; @@ -28,7 +28,7 @@ class Taxus public function add(...$privileges) : self { foreach($privileges as $item) { - $item = (array) $item; + $item = array_pad((array) $item, 2, null); if ( ! $item[0] instanceof Privilege ) { throw new \InvalidArgumentException("First element must be an instance of '" . Privilege::class . "'"); @@ -56,7 +56,6 @@ class Taxus } $name = $name instanceof \BackedEnum ? $name->value : $name; - if ( ! isset($this->list[$name]) ) { throw new \InvalidArgumentException("Privilege '{$name}' could not be found. Did you forgot to add it to your Taxus object ?"); }