- First commit
This commit is contained in:
commit
2845badd40
5 files changed
+124
No files matched your search
@@ -0,0 +1,50 @@
|
||||
<?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);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user