/framework/Group/lib/Horde/Group/Mock.php

https://github.com/ewandor/horde · PHP · 254 lines · 111 code · 14 blank · 129 comment · 13 complexity · c0dc35567df3ecb91ea957afdecd525d MD5 · raw file

  1. <?php
  2. /**
  3. * This class provides a mock driver for the Horde group system.
  4. *
  5. * Copyright 2008-2012 Horde LLC (http://www.horde.org/)
  6. *
  7. * See the enclosed file COPYING for license information (LGPL). If you
  8. * did not receive this file, see http://www.horde.org/licenses/lgpl21.
  9. *
  10. * @author Duck <duck@obala.net>
  11. * @category Horde
  12. * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
  13. * @package Group
  14. */
  15. class Horde_Group_Mock extends Horde_Group_Base
  16. {
  17. /**
  18. * List of groups.
  19. *
  20. * @var array
  21. */
  22. protected $_groups = array();
  23. /**
  24. * Creates a new group.
  25. *
  26. * @param string $name A group name.
  27. * @param string $email The group's email address.
  28. *
  29. * @return mixed The ID of the created group.
  30. * @throws Horde_Group_Exception
  31. */
  32. public function create($name, $email = null)
  33. {
  34. $id = 'group_' . count($this->_groups);
  35. $this->_groups[$id] = array('name' => $name,
  36. 'email' => $email,
  37. 'users' => array());
  38. return $id;
  39. }
  40. /**
  41. * Renames a group.
  42. *
  43. * @param mixed $gid A group ID.
  44. * @param string $name The new name.
  45. *
  46. * @throws Horde_Group_Exception
  47. */
  48. public function rename($gid, $name)
  49. {
  50. if (!isset($this->_groups[$gid])) {
  51. throw new Horde_Exception_NotFound('Group "' . $gid . '" not found');
  52. }
  53. $this->_groups[$gid]['name'] = $name;
  54. }
  55. /**
  56. * Removes a group.
  57. *
  58. * @param mixed $gid A group ID.
  59. *
  60. * @throws Horde_Group_Exception
  61. */
  62. public function remove($gid)
  63. {
  64. unset($this->_groups[$gid]);
  65. }
  66. /**
  67. * Checks if a group exists.
  68. *
  69. * @param mixed $gid A group ID.
  70. *
  71. * @return boolean True if the group exists.
  72. * @throws Horde_Group_Exception
  73. */
  74. public function exists($gid)
  75. {
  76. return isset($this->_groups[$gid]);
  77. }
  78. /**
  79. * Returns a group name.
  80. *
  81. * @param mixed $gid A group ID.
  82. *
  83. * @return string The group's name.
  84. * @throws Horde_Group_Exception
  85. */
  86. public function getName($gid)
  87. {
  88. if (!isset($this->_groups[$gid])) {
  89. throw new Horde_Exception_NotFound('Group ' . $gid . ' not found');
  90. }
  91. return $this->_groups[$gid]['name'];
  92. }
  93. /**
  94. * Returns all available attributes of a group.
  95. *
  96. * @param mixed $gid A group ID.
  97. *
  98. * @return array The group's date.
  99. * @throws Horde_Group_Exception
  100. * @throws Horde_Exception_NotFound
  101. */
  102. public function getData($gid)
  103. {
  104. if (!isset($this->_groups[$gid])) {
  105. throw new Horde_Exception_NotFound('Group ' . $gid . ' not found');
  106. }
  107. return $this->_groups[$gid];
  108. }
  109. /**
  110. * Sets one or more attributes of a group.
  111. *
  112. * @param mixed $gid A group ID.
  113. * @param array|string $attribute An attribute name or a hash of
  114. * attributes.
  115. * @param string $value An attribute value if $attribute is a
  116. * string.
  117. *
  118. * @throws Horde_Group_Exception
  119. * @throws Horde_Exception_NotFound
  120. */
  121. public function setData($gid, $attribute, $value = null)
  122. {
  123. if (!isset($this->_groups[$gid])) {
  124. throw new Horde_Exception_NotFound('Group ' . $gid . ' not found');
  125. }
  126. if (is_array($attribute)) {
  127. $this->_groups[$gid] = array_merge($this->_groups[$gid], $attribute);
  128. } else {
  129. $this->_groups[$gid][$attribute] = $value;
  130. }
  131. }
  132. /**
  133. * Returns a list of all groups a user may see, with IDs as keys and names
  134. * as values.
  135. *
  136. * @param string $member Only return groups that this user is a member of.
  137. *
  138. * @return array All existing groups.
  139. * @throws Horde_Group_Exception
  140. */
  141. public function listAll($member = null)
  142. {
  143. if (!is_null($member)) {
  144. return $this->listGroups($member);
  145. }
  146. $groups = array();
  147. foreach ($this->_groups as $gid => $group) {
  148. $groups[$gid] = $group['name'];
  149. }
  150. asort($groups);
  151. return $groups;
  152. }
  153. /**
  154. * Returns a list of users in a group.
  155. *
  156. * @param mixed $gid A group ID.
  157. *
  158. * @return array List of group users.
  159. * @throws Horde_Group_Exception
  160. */
  161. public function listUsers($gid)
  162. {
  163. if (!isset($this->_groups[$gid])) {
  164. throw new Horde_Exception_NotFound('Group ' . $gid . ' not found');
  165. }
  166. return $this->_groups[$gid]['users'];
  167. }
  168. /**
  169. * Returns a list of groups a user belongs to.
  170. *
  171. * @param string $user A user name.
  172. *
  173. * @return array A list of groups, with IDs as keys and names as values.
  174. * @throws Horde_Group_Exception
  175. */
  176. public function listGroups($user)
  177. {
  178. $groups = array();
  179. foreach ($this->_groups as $gid => $group) {
  180. if (in_array($user, $group['users'])) {
  181. $groups[$gid] = $group['name'];
  182. }
  183. }
  184. asort($groups);
  185. return $groups;
  186. }
  187. /**
  188. * Add a user to a group.
  189. *
  190. * @param mixed $gid A group ID.
  191. * @param string $user A user name.
  192. *
  193. * @throws Horde_Group_Exception
  194. */
  195. public function addUser($gid, $user)
  196. {
  197. if (!isset($this->_groups[$gid])) {
  198. throw new Horde_Exception_NotFound('Group ' . $gid . ' not found');
  199. }
  200. $this->_groups[$gid]['users'][] = $user;
  201. }
  202. /**
  203. * Removes a user from a group.
  204. *
  205. * @param mixed $gid A group ID.
  206. * @param string $user A user name.
  207. *
  208. * @throws Horde_Group_Exception
  209. */
  210. public function removeUser($gid, $user)
  211. {
  212. if (!isset($this->_groups[$gid])) {
  213. throw new Horde_Exception_NotFound('Group ' . $gid . ' not found');
  214. }
  215. $key = array_search($user, $this->_groups[$gid]['users']);
  216. if ($key !== false) {
  217. unset($this->_groups[$gid]['users'][$key]);
  218. }
  219. }
  220. /**
  221. * Searches for group names.
  222. *
  223. * @param string $name A search string.
  224. *
  225. * @return array A list of matching groups, with IDs as keys and names as
  226. * values.
  227. * @throws Horde_Group_Exception
  228. */
  229. public function search($name)
  230. {
  231. $groups = array();
  232. foreach ($this->_groups as $gid => $group) {
  233. if (strpos($group['name'], $name) !== false) {
  234. $groups[$gid] = $group['name'];
  235. }
  236. }
  237. asort($groups);
  238. return $groups;
  239. }
  240. }