Overview

Packages

  • Group

Classes

  • Horde_Group_Base
  • Horde_Group_Contactlists
  • Horde_Group_Exception
  • Horde_Group_Kolab
  • Horde_Group_Ldap
  • Horde_Group_Mock
  • Horde_Group_Sql
  • Overview
  • Package
  • Class
  • Tree
  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:     /**
 25:      * Creates a new group.
 26:      *
 27:      * @param string $name   A group name.
 28:      * @param string $email  The group's email address.
 29:      *
 30:      * @return mixed  The ID of the created group.
 31:      * @throws Horde_Group_Exception
 32:      */
 33:     public function create($name, $email = null)
 34:     {
 35:         $id = 'group_' . count($this->_groups);
 36:         $this->_groups[$id] = array('name'  => $name,
 37:                                     'email' => $email,
 38:                                     'users' => array());
 39:         return $id;
 40:     }
 41: 
 42:     /**
 43:      * Renames a group.
 44:      *
 45:      * @param mixed $gid    A group ID.
 46:      * @param string $name  The new name.
 47:      *
 48:      * @throws Horde_Group_Exception
 49:      */
 50:     public function rename($gid, $name)
 51:     {
 52:         if (!isset($this->_groups[$gid])) {
 53:             throw new Horde_Exception_NotFound('Group "' . $gid . '" not found');
 54:         }
 55:         $this->_groups[$gid]['name'] = $name;
 56:     }
 57: 
 58:     /**
 59:      * Removes a group.
 60:      *
 61:      * @param mixed $gid  A group ID.
 62:      *
 63:      * @throws Horde_Group_Exception
 64:      */
 65:     public function remove($gid)
 66:     {
 67:         unset($this->_groups[$gid]);
 68:     }
 69: 
 70:     /**
 71:      * Checks if a group exists.
 72:      *
 73:      * @param mixed $gid  A group ID.
 74:      *
 75:      * @return boolean  True if the group exists.
 76:      * @throws Horde_Group_Exception
 77:      */
 78:     public function exists($gid)
 79:     {
 80:         return isset($this->_groups[$gid]);
 81:     }
 82: 
 83:     /**
 84:      * Returns a group name.
 85:      *
 86:      * @param mixed $gid  A group ID.
 87:      *
 88:      * @return string  The group's name.
 89:      * @throws Horde_Group_Exception
 90:      */
 91:     public function getName($gid)
 92:     {
 93:         if (!isset($this->_groups[$gid])) {
 94:             throw new Horde_Exception_NotFound('Group ' . $gid . ' not found');
 95:         }
 96:         return $this->_groups[$gid]['name'];
 97:     }
 98: 
 99:     /**
100:      * Returns all available attributes of a group.
101:      *
102:      * @param mixed $gid  A group ID.
103:      *
104:      * @return array  The group's date.
105:      * @throws Horde_Group_Exception
106:      * @throws Horde_Exception_NotFound
107:      */
108:     public function getData($gid)
109:     {
110:         if (!isset($this->_groups[$gid])) {
111:             throw new Horde_Exception_NotFound('Group ' . $gid . ' not found');
112:         }
113:         return $this->_groups[$gid];
114:     }
115: 
116:     /**
117:      * Sets one or more attributes of a group.
118:      *
119:      * @param mixed $gid               A group ID.
120:      * @param array|string $attribute  An attribute name or a hash of
121:      *                                 attributes.
122:      * @param string $value            An attribute value if $attribute is a
123:      *                                 string.
124:      *
125:      * @throws Horde_Group_Exception
126:      * @throws Horde_Exception_NotFound
127:      */
128:     public function setData($gid, $attribute, $value = null)
129:     {
130:         if (!isset($this->_groups[$gid])) {
131:             throw new Horde_Exception_NotFound('Group ' . $gid . ' not found');
132:         }
133:         if (is_array($attribute)) {
134:             $this->_groups[$gid] = array_merge($this->_groups[$gid], $attribute);
135:         } else {
136:             $this->_groups[$gid][$attribute] = $value;
137:         }
138:     }
139: 
140:     /**
141:      * Returns a list of all groups a user may see, with IDs as keys and names
142:      * as values.
143:      *
144:      * @param string $member  Only return groups that this user is a member of.
145:      *
146:      * @return array  All existing groups.
147:      * @throws Horde_Group_Exception
148:      */
149:     public function listAll($member = null)
150:     {
151:         if (!is_null($member)) {
152:             return $this->listGroups($member);
153:         }
154: 
155:         $groups = array();
156:         foreach ($this->_groups as $gid => $group) {
157:             $groups[$gid] = $group['name'];
158:         }
159:         asort($groups);
160:         return $groups;
161:     }
162: 
163:     /**
164:      * Returns a list of users in a group.
165:      *
166:      * @param mixed $gid  A group ID.
167:      *
168:      * @return array  List of group users.
169:      * @throws Horde_Group_Exception
170:      */
171:     public function listUsers($gid)
172:     {
173:         if (!isset($this->_groups[$gid])) {
174:             throw new Horde_Exception_NotFound('Group ' . $gid . ' not found');
175:         }
176:         return $this->_groups[$gid]['users'];
177:     }
178: 
179:     /**
180:      * Returns a list of groups a user belongs to.
181:      *
182:      * @param string $user  A user name.
183:      *
184:      * @return array  A list of groups, with IDs as keys and names as values.
185:      * @throws Horde_Group_Exception
186:      */
187:     public function listGroups($user)
188:     {
189:         $groups = array();
190:         foreach ($this->_groups as $gid => $group) {
191:             if (in_array($user, $group['users'])) {
192:                 $groups[$gid] = $group['name'];
193:             }
194:         }
195:         asort($groups);
196:         return $groups;
197:     }
198: 
199:     /**
200:      * Add a user to a group.
201:      *
202:      * @param mixed $gid    A group ID.
203:      * @param string $user  A user name.
204:      *
205:      * @throws Horde_Group_Exception
206:      */
207:     public function addUser($gid, $user)
208:     {
209:         if (!isset($this->_groups[$gid])) {
210:             throw new Horde_Exception_NotFound('Group ' . $gid . ' not found');
211:         }
212:         $this->_groups[$gid]['users'][] = $user;
213:     }
214: 
215:     /**
216:      * Removes a user from a group.
217:      *
218:      * @param mixed $gid    A group ID.
219:      * @param string $user  A user name.
220:      *
221:      * @throws Horde_Group_Exception
222:      */
223:     public function removeUser($gid, $user)
224:     {
225:         if (!isset($this->_groups[$gid])) {
226:             throw new Horde_Exception_NotFound('Group ' . $gid . ' not found');
227:         }
228:         $key = array_search($user, $this->_groups[$gid]['users']);
229:         if ($key !== false) {
230:             unset($this->_groups[$gid]['users'][$key]);
231:         }
232:     }
233: 
234:     /**
235:      * Searches for group names.
236:      *
237:      * @param string $name  A search string.
238:      *
239:      * @return array  A list of matching groups, with IDs as keys and names as
240:      *                values.
241:      * @throws Horde_Group_Exception
242:      */
243:     public function search($name)
244:     {
245:         $groups = array();
246:         foreach ($this->_groups as $gid => $group) {
247:             if (strpos($group['name'], $name) !== false) {
248:                 $groups[$gid] = $group['name'];
249:             }
250:         }
251:         asort($groups);
252:         return $groups;
253:     }
254: }
255: 
API documentation generated by ApiGen