Compare commits

..

No commits in common. "master" and "v1.0.0" have entirely different histories.

2 changed files with 19 additions and 17 deletions

View File

@ -2,25 +2,26 @@
namespace Taxus; namespace Taxus;
use http\Exception\RuntimeException;
class Privilege class Privilege
{ {
public string $name;
public string $description;
public mixed $error;
public $success;
public $function;
public ? bool $granted = false; public ? bool $granted = false;
public function __construct( public function __construct(string $name, string $description = "", ? Callable $function = null, ? Callable $success = null, ? Callable $error = null)
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,
)
{ {
foreach(array_filter([ 'function' => $function, 'success' => $success, 'error' => $error ]) as $item) { $this->name = $name;
if ( ! is_callable($item) ) { $this->description = $description;
throw new RuntimeException("Your {$item} property must be Callable"); $this->function = $function;
} $this->success = $success;
} $this->error = $error;
} }
} }

View File

@ -5,7 +5,7 @@ namespace Taxus;
class Taxus class Taxus
{ {
# @ArrayOf Privilege # @ArrayOf Privilege
public array $list = []; protected array $list = [];
# @ArrayOf PermissionGrantInterface # @ArrayOf PermissionGrantInterface
protected array $gates = []; protected array $gates = [];
@ -28,7 +28,7 @@ class Taxus
public function add(...$privileges) : self public function add(...$privileges) : self
{ {
foreach($privileges as $item) { foreach($privileges as $item) {
$item = array_pad((array) $item, 2, null); $item = (array) $item;
if ( ! $item[0] instanceof Privilege ) { if ( ! $item[0] instanceof Privilege ) {
throw new \InvalidArgumentException("First element must be an instance of '" . Privilege::class . "'"); throw new \InvalidArgumentException("First element must be an instance of '" . Privilege::class . "'");
@ -56,6 +56,7 @@ class Taxus
} }
$name = $name instanceof \BackedEnum ? $name->value : $name; $name = $name instanceof \BackedEnum ? $name->value : $name;
if ( ! isset($this->list[$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 ?"); throw new \InvalidArgumentException("Privilege '{$name}' could not be found. Did you forgot to add it to your Taxus object ?");
} }