-
-
Save muhamed-didovic/e2d2f3d204a895cc9a0e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace My\Role; | |
use RoleId as BaseId; | |
class RoleId extends BaseId | |
{ | |
const ADMIN = 'Admin'; | |
const GUEST = 'Guest'; | |
protected static $validNames = [ | |
self::ADMIN, | |
self::GUEST | |
]; | |
public function isAdmin() | |
{ | |
return $this->name == static::ADMIN; | |
} | |
public function isGuest() | |
{ | |
return $this->name == static::GUEST; | |
} | |
} | |
class AdminRole extends AbstractRole | |
{ | |
public function roleId() | |
{ | |
return RoleId::Admin(); | |
} | |
} | |
class GuestRole extends AbstractRole | |
{ | |
public function roleId() | |
{ | |
return RoleId::Guest(); | |
} | |
} | |
abstract class AbstractRole implements RoleInterface | |
{ | |
protected $permissions = []; | |
public function __construct(array $permissions) | |
{ | |
$this->setPermissions($permissions); | |
} | |
public function isPermitted(Permission $permission) | |
{ | |
return in_array($permission, $this->permissions); | |
} | |
private function addPermission(Permission $permission) | |
{ | |
if (!$this->isPermitted($permission) { | |
$this->permissions[] = $permission; | |
} | |
} | |
private function setPermissions(array $permissions) | |
{ | |
foreach ($permissions as $permission) { | |
$this->addPermission($permission); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class RoleId | |
{ | |
protected static $validNames = []; | |
protected $name; | |
public function __construct($name) | |
{ | |
$this->name = trim($name); | |
if (!empty(static::$validNames) && !in_array($this->name, static::$validNames)) { | |
throw new DomainException("Invalid name [$name]"); | |
} | |
} | |
public static function __callStatic($name, array $arguments []) | |
{ | |
return new static($name); | |
} | |
public function equals(self $other) | |
{ | |
return $this->name == $other->name; | |
} | |
public function name() | |
{ | |
return $this->name; | |
} | |
public function __toString() | |
{ | |
return $this->name(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment