Compare commits
1 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
e134e81334 |
@ -2,26 +2,25 @@
|
|||||||
|
|
||||||
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(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;
|
foreach(array_filter([ 'function' => $function, 'success' => $success, 'error' => $error ]) as $item) {
|
||||||
$this->description = $description;
|
if ( ! is_callable($item) ) {
|
||||||
$this->function = $function;
|
throw new RuntimeException("Your {$item} property must be Callable");
|
||||||
$this->success = $success;
|
}
|
||||||
$this->error = $error;
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,7 @@ namespace Taxus;
|
|||||||
class Taxus
|
class Taxus
|
||||||
{
|
{
|
||||||
# @ArrayOf Privilege
|
# @ArrayOf Privilege
|
||||||
protected array $list = [];
|
public 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) $item;
|
$item = array_pad((array) $item, 2, null);
|
||||||
|
|
||||||
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,7 +56,6 @@ 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 ?");
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user