Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / Permissions / Rbac / AbstractRole.php
blobfff1a9acd9cdc156b6683fa1d9c1da07650b539f
1 <?php
2 /**
3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
8 */
10 namespace Zend\Permissions\Rbac;
12 use RecursiveIteratorIterator;
14 abstract class AbstractRole extends AbstractIterator implements RoleInterface
16 /**
17 * @var null|RoleInterface
19 protected $parent;
21 /**
22 * @var string
24 protected $name;
26 /**
27 * @var array
29 protected $permissions = array();
31 /**
32 * Get the name of the role.
34 * @return string
36 public function getName()
38 return $this->name;
41 /**
42 * Add permission to the role.
44 * @param $name
45 * @return RoleInterface
47 public function addPermission($name)
49 $this->permissions[$name] = true;
51 return $this;
54 /**
55 * Checks if a permission exists for this role or any child roles.
57 * @param string $name
58 * @return bool
60 public function hasPermission($name)
62 if (isset($this->permissions[$name])) {
63 return true;
66 $it = new RecursiveIteratorIterator($this, RecursiveIteratorIterator::CHILD_FIRST);
67 foreach ($it as $leaf) {
68 /** @var RoleInterface $leaf */
69 if ($leaf->hasPermission($name)) {
70 return true;
74 return false;
77 /**
78 * Add a child.
80 * @param RoleInterface|string $child
81 * @return Role
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;
97 return $this;
101 * @param RoleInterface $parent
102 * @return RoleInterface
104 public function setParent($parent)
106 $this->parent = $parent;
108 return $this;
112 * @return null|RoleInterface
114 public function getParent()
116 return $this->parent;