Skip to content

Instantly share code, notes, and snippets.

@texdc
Last active August 9, 2017 13:24
Show Gist options
  • Save texdc/7708682 to your computer and use it in GitHub Desktop.
Save texdc/7708682 to your computer and use it in GitHub Desktop.
<?php
namespace My\Project\Permissions;
use Zend\Permissions\Role as ZendRole;
use Zend\Permissions\Resource as ZendResource;
/**
* @method static Guest
* @method static Admin
*/
class Role extends ZendRole
{
private const GUEST = 'Guest';
private const ADMIN = 'Admin';
protected static $validNames = [
self::GUEST,
self::ADMIN
];
}
$guest = Role::Guest();
$admin = Role::Admin();
assert($guest == $admin); // false
/**
* @method static User
* @method static Blog
*/
class Resource extends ZendResource
{
private const USER = 'User';
private const BLOG = 'Blog';
protected static $validNames = [
self::USER,
self::BLOG
];
}
$user = Resource::User();
$blog = Resource::Blog();
assert($user == $blog); // false
<?php
namespace My\Project\User;
use My\Project\Permissions\Resource;
use Zend\Permissions\Resource\ProviderInterface as ResourceProvider;
class Service implements ResourceProvider
{
private const RESOURCE = Resource::User();
public function getResource() : Resource
{
return RESOURCE;
}
}
use My\Project\Permissions\Role;
use Zend\Permissions\Role\ProviderInterface as RoleProvider;
class GuestEntity implements RoleProvider
{
private const ROLE = Role::Guest();
public function getRole() : Role
{
return ROLE;
}
}
<?php
// generic helpers
interface NameProvider
{
public function getName();
}
trait NameProviderTrait
{
private $name;
public function getName()
{
return $this->name;
}
}
interface ValidNamesProvider
{
public static function getValidNames();
}
trait ValidNamesProviderTrait
{
protected static $validNames = [];
public static function getValidNames() : array
{
return static::$validNames;
}
protected function assertValidName(string $name)
{
if (in_array($name, static::$validNames)) {
throw new InvalidArgumentException("Invalid name [$name]");
}
}
}
interface Entity
{
public function getId();
}
<?php
namespace Zend\Permissions;
interface NamedEntityInterface extends Entity, NameProvider, ValidNamesProvider
{
public function __toString();
}
abstract class NamedEntity implements NamedEntityInterface
{
use \NameProviderTrait;
use \ValidNamesProviderTrait;
public static function __callStatic(string $method, array $arguments = null) : self
{
return new static($method);
}
public function __construct(string $name)
{
$this->assertValidName($name);
$this->name = $name;
}
public function getId() : string
{
return $this->getName();
}
public function __toString() : string
{
return $this->getName();
}
}
<?php
namespace Zend\Permissions;
interface ResourceInterface extends NamedEntityInterface
{
}
abstract class Resource extends NamedEntity implements ResourceInterface
{
protected static $validNames = [];
}
interface RoleInterface extends NamedEntityInterface
{
}
abstract class Role extends NamedEntity implements RoleInterface
{
protected static $validNames = [];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment