3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Permissions\Rbac
;
12 use RecursiveIteratorIterator
;
14 abstract class AbstractRole
extends AbstractIterator
implements RoleInterface
17 * @var null|RoleInterface
29 protected $permissions = array();
32 * Get the name of the role.
36 public function getName()
42 * Add permission to the role.
45 * @return RoleInterface
47 public function addPermission($name)
49 $this->permissions
[$name] = true;
55 * Checks if a permission exists for this role or any child roles.
60 public function hasPermission($name)
62 if (isset($this->permissions
[$name])) {
66 $it = new RecursiveIteratorIterator($this, RecursiveIteratorIterator
::CHILD_FIRST
);
67 foreach ($it as $leaf) {
68 /** @var RoleInterface $leaf */
69 if ($leaf->hasPermission($name)) {
80 * @param RoleInterface|string $child
83 public function addChild($child)
85 if (is_string($child)) {
86 $child = new Role($child);
88 if (!$child instanceof RoleInterface
) {
89 throw new Exception\
InvalidArgumentException(
90 'Child must be a string or implement Zend\Permissions\Rbac\RoleInterface'
94 $child->setParent($this);
95 $this->children
[] = $child;
101 * @param RoleInterface $parent
102 * @return RoleInterface
104 public function setParent($parent)
106 $this->parent
= $parent;
112 * @return null|RoleInterface
114 public function getParent()
116 return $this->parent
;