taxus/src/Taxus.php
2021-08-27 18:08:35 +00:00

51 lines
1.3 KiB
PHP

<?php
namespace Taxus;
class Taxus
{
protected PermissionGrantInterface $gate;
public function __construct(PermissionGrantInterface $gate)
{
$this->gate = $gate;
}
/**
* @var array
*/
protected $list;
public function add(...$privileges) : self
{
foreach($privileges as $item) {
$item = (array) $item;
if ( ! $item[0] instanceof Privilege ) {
throw new \InvalidArgumentException("First element must be an instance of '" . Privilege::class . "'");
}
if ( ( $item[1] ?? false ) && ! is_string($item[1]) ) {
throw new \InvalidArgumentException("Second element must be a callable function from your Permission Handler");
}
$this->list[ $item[0]->name ] = $item;
}
return $this;
}
public function granted($name, ...$arguments) : bool
{
if ( ! isset($this->list[$name]) ) {
throw new \InvalidArgumentException("Privilege '{$name}' could not be found. Did you forgot to add it to your Taxus object ?");
}
$obj = $this->list[$name][0];
$callback = $this->list[$name][1] ?? 'default';
return $this->gate->$callback(...$arguments);
}
}