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 / Acl / Role / Registry.php
blob9cbb8474345dfabc1aee36d4eb0ca6b4149f5cab
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\Acl\Role;
12 use Traversable;
13 use Zend\Permissions\Acl\Exception;
15 class Registry
17 /**
18 * Internal Role registry data storage
20 * @var array
22 protected $roles = array();
24 /**
25 * Adds a Role having an identifier unique to the registry
27 * The $parents parameter may be a reference to, or the string identifier for,
28 * a Role existing in the registry, or $parents may be passed as an array of
29 * these - mixing string identifiers and objects is ok - to indicate the Roles
30 * from which the newly added Role will directly inherit.
32 * In order to resolve potential ambiguities with conflicting rules inherited
33 * from different parents, the most recently added parent takes precedence over
34 * parents that were previously added. In other words, the first parent added
35 * will have the least priority, and the last parent added will have the
36 * highest priority.
38 * @param RoleInterface $role
39 * @param RoleInterface|string|array|Traversable $parents
40 * @throws Exception\InvalidArgumentException
41 * @return Registry Provides a fluent interface
43 public function add(RoleInterface $role, $parents = null)
45 $roleId = $role->getRoleId();
47 if ($this->has($roleId)) {
48 throw new Exception\InvalidArgumentException(sprintf(
49 'Role id "%s" already exists in the registry',
50 $roleId
51 ));
54 $roleParents = array();
56 if (null !== $parents) {
57 if (!is_array($parents) && !$parents instanceof Traversable) {
58 $parents = array($parents);
60 foreach ($parents as $parent) {
61 try {
62 if ($parent instanceof RoleInterface) {
63 $roleParentId = $parent->getRoleId();
64 } else {
65 $roleParentId = $parent;
67 $roleParent = $this->get($roleParentId);
68 } catch (\Exception $e) {
69 throw new Exception\InvalidArgumentException(sprintf(
70 'Parent Role id "%s" does not exist',
71 $roleParentId
72 ), 0, $e);
74 $roleParents[$roleParentId] = $roleParent;
75 $this->roles[$roleParentId]['children'][$roleId] = $role;
79 $this->roles[$roleId] = array(
80 'instance' => $role,
81 'parents' => $roleParents,
82 'children' => array(),
85 return $this;
88 /**
89 * Returns the identified Role
91 * The $role parameter can either be a Role or a Role identifier.
93 * @param RoleInterface|string $role
94 * @throws Exception\InvalidArgumentException
95 * @return RoleInterface
97 public function get($role)
99 if ($role instanceof RoleInterface) {
100 $roleId = $role->getRoleId();
101 } else {
102 $roleId = (string) $role;
105 if (!$this->has($role)) {
106 throw new Exception\InvalidArgumentException("Role '$roleId' not found");
109 return $this->roles[$roleId]['instance'];
113 * Returns true if and only if the Role exists in the registry
115 * The $role parameter can either be a Role or a Role identifier.
117 * @param RoleInterface|string $role
118 * @return bool
120 public function has($role)
122 if ($role instanceof RoleInterface) {
123 $roleId = $role->getRoleId();
124 } else {
125 $roleId = (string) $role;
128 return isset($this->roles[$roleId]);
132 * Returns an array of an existing Role's parents
134 * The array keys are the identifiers of the parent Roles, and the values are
135 * the parent Role instances. The parent Roles are ordered in this array by
136 * ascending priority. The highest priority parent Role, last in the array,
137 * corresponds with the parent Role most recently added.
139 * If the Role does not have any parents, then an empty array is returned.
141 * @param RoleInterface|string $role
142 * @return array
144 public function getParents($role)
146 $roleId = $this->get($role)->getRoleId();
148 return $this->roles[$roleId]['parents'];
152 * Returns true if and only if $role inherits from $inherit
154 * Both parameters may be either a Role or a Role identifier. If
155 * $onlyParents is true, then $role must inherit directly from
156 * $inherit in order to return true. By default, this method looks
157 * through the entire inheritance DAG to determine whether $role
158 * inherits from $inherit through its ancestor Roles.
160 * @param RoleInterface|string $role
161 * @param RoleInterface|string $inherit
162 * @param bool $onlyParents
163 * @throws Exception\InvalidArgumentException
164 * @return bool
166 public function inherits($role, $inherit, $onlyParents = false)
168 try {
169 $roleId = $this->get($role)->getRoleId();
170 $inheritId = $this->get($inherit)->getRoleId();
171 } catch (Exception\ExceptionInterface $e) {
172 throw new Exception\InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
175 $inherits = isset($this->roles[$roleId]['parents'][$inheritId]);
177 if ($inherits || $onlyParents) {
178 return $inherits;
181 foreach ($this->roles[$roleId]['parents'] as $parentId => $parent) {
182 if ($this->inherits($parentId, $inheritId)) {
183 return true;
187 return false;
191 * Removes the Role from the registry
193 * The $role parameter can either be a Role or a Role identifier.
195 * @param RoleInterface|string $role
196 * @throws Exception\InvalidArgumentException
197 * @return Registry Provides a fluent interface
199 public function remove($role)
201 try {
202 $roleId = $this->get($role)->getRoleId();
203 } catch (Exception\ExceptionInterface $e) {
204 throw new Exception\InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
207 foreach ($this->roles[$roleId]['children'] as $childId => $child) {
208 unset($this->roles[$childId]['parents'][$roleId]);
210 foreach ($this->roles[$roleId]['parents'] as $parentId => $parent) {
211 unset($this->roles[$parentId]['children'][$roleId]);
214 unset($this->roles[$roleId]);
216 return $this;
220 * Removes all Roles from the registry
222 * @return Registry Provides a fluent interface
224 public function removeAll()
226 $this->roles = array();
228 return $this;
232 * Get all roles in the registry
234 * @return array
236 public function getRoles()
238 return $this->roles;