]> git.mxchange.org Git - friendica-addons.git/blob - dav/SabreDAV/tests/Sabre/CalDAV/Backend/Mock.php
Merge branch 'master' of git://github.com/friendica/friendica-addons
[friendica-addons.git] / dav / SabreDAV / tests / Sabre / CalDAV / Backend / Mock.php
1 <?php
2
3 class Sabre_CalDAV_Backend_Mock extends Sabre_CalDAV_Backend_Abstract implements Sabre_CalDAV_Backend_NotificationSupport {
4
5     private $calendarData;
6     private $calendars;
7     private $notifications;
8
9     function __construct(array $calendars, array $calendarData, array $notifications = array()) {
10
11         $this->calendars = $calendars;
12         $this->calendarData = $calendarData;
13         $this->notifications = $notifications;
14
15     }
16
17     /**
18      * Returns a list of calendars for a principal.
19      *
20      * Every project is an array with the following keys:
21      *  * id, a unique id that will be used by other functions to modify the
22      *    calendar. This can be the same as the uri or a database key.
23      *  * uri, which the basename of the uri with which the calendar is
24      *    accessed.
25      *  * principalUri. The owner of the calendar. Almost always the same as
26      *    principalUri passed to this method.
27      *
28      * Furthermore it can contain webdav properties in clark notation. A very
29      * common one is '{DAV:}displayname'.
30      *
31      * @param string $principalUri
32      * @return array
33      */
34     function getCalendarsForUser($principalUri) {
35
36         $r = array();
37         foreach($this->calendars as $row) {
38             if ($row['principaluri'] == $principalUri) {
39                 $r[] = $row;
40             }
41         }
42
43         return $r;
44
45     }
46
47     /**
48      * Creates a new calendar for a principal.
49      *
50      * If the creation was a success, an id must be returned that can be used to reference
51      * this calendar in other methods, such as updateCalendar.
52      *
53      * This function must return a server-wide unique id that can be used
54      * later to reference the calendar.
55      *
56      * @param string $principalUri
57      * @param string $calendarUri
58      * @param array $properties
59      * @return string|int
60      */
61     function createCalendar($principalUri,$calendarUri,array $properties) {
62
63         $id = Sabre_DAV_UUIDUtil::getUUID();
64         $this->calendars[] = array_merge(array(
65             'id' => $id,
66             'principaluri' => $principalUri,
67             'uri' => $calendarUri,
68             '{' . Sabre_CalDAV_Plugin::NS_CALDAV . '}supported-calendar-component-set' => new Sabre_CalDAV_Property_SupportedCalendarComponentSet(array('VEVENT','VTODO')),
69         ), $properties);
70
71         return $id;
72
73     }
74
75     /**
76      * Updates properties on this node,
77      *
78      * The properties array uses the propertyName in clark-notation as key,
79      * and the array value for the property value. In the case a property
80      * should be deleted, the property value will be null.
81      *
82      * This method must be atomic. If one property cannot be changed, the
83      * entire operation must fail.
84      *
85      * If the operation was successful, true can be returned.
86      * If the operation failed, false can be returned.
87      *
88      * Deletion of a non-existent property is always successful.
89      *
90      * Lastly, it is optional to return detailed information about any
91      * failures. In this case an array should be returned with the following
92      * structure:
93      *
94      * array(
95      *   403 => array(
96      *      '{DAV:}displayname' => null,
97      *   ),
98      *   424 => array(
99      *      '{DAV:}owner' => null,
100      *   )
101      * )
102      *
103      * In this example it was forbidden to update {DAV:}displayname.
104      * (403 Forbidden), which in turn also caused {DAV:}owner to fail
105      * (424 Failed Dependency) because the request needs to be atomic.
106      *
107      * @param string $calendarId
108      * @param array $properties
109      * @return bool|array
110      */
111     public function updateCalendar($calendarId, array $properties) {
112
113         return false;
114
115     }
116
117     /**
118      * Delete a calendar and all it's objects
119      *
120      * @param string $calendarId
121      * @return void
122      */
123     public function deleteCalendar($calendarId) {
124
125         foreach($this->calendars as $k=>$calendar) {
126             if ($calendar['id'] === $calendarId) {
127                 unset($this->calendars[$k]);
128             }
129         }
130
131     }
132
133     /**
134      * Returns all calendar objects within a calendar object.
135      *
136      * Every item contains an array with the following keys:
137      *   * id - unique identifier which will be used for subsequent updates
138      *   * calendardata - The iCalendar-compatible calendar data
139      *   * uri - a unique key which will be used to construct the uri. This can be any arbitrary string.
140      *   * lastmodified - a timestamp of the last modification time
141      *   * etag - An arbitrary string, surrounded by double-quotes. (e.g.:
142      *   '  "abcdef"')
143      *   * calendarid - The calendarid as it was passed to this function.
144      *
145      * Note that the etag is optional, but it's highly encouraged to return for
146      * speed reasons.
147      *
148      * The calendardata is also optional. If it's not returned
149      * 'getCalendarObject' will be called later, which *is* expected to return
150      * calendardata.
151      *
152      * @param string $calendarId
153      * @return array
154      */
155     public function getCalendarObjects($calendarId) {
156
157         if (!isset($this->calendarData[$calendarId]))
158             return array();
159
160         $objects = $this->calendarData[$calendarId];
161         foreach($objects as $uri => &$object) {
162             $object['calendarid'] = $calendarId;
163             $object['uri'] = $uri;
164
165         }
166         return $objects;
167
168     }
169
170     /**
171      * Returns information from a single calendar object, based on it's object
172      * uri.
173      *
174      * The returned array must have the same keys as getCalendarObjects. The
175      * 'calendardata' object is required here though, while it's not required
176      * for getCalendarObjects.
177      *
178      * @param string $calendarId
179      * @param string $objectUri
180      * @return array
181      */
182     function getCalendarObject($calendarId,$objectUri) {
183
184         if (!isset($this->calendarData[$calendarId][$objectUri])) {
185             throw new Sabre_DAV_Exception_NotFound('Object could not be found');
186         }
187         $object = $this->calendarData[$calendarId][$objectUri];
188         $object['calendarid'] = $calendarId;
189         $object['uri'] = $objectUri;
190         return $object;
191
192     }
193
194     /**
195      * Creates a new calendar object.
196      *
197      * @param string $calendarId
198      * @param string $objectUri
199      * @param string $calendarData
200      * @return void
201      */
202     function createCalendarObject($calendarId,$objectUri,$calendarData) {
203
204         $this->calendarData[$calendarId][$objectUri] = array(
205             'calendardata' => $calendarData,
206             'calendarid' => $calendarId,
207             'uri' => $objectUri,
208         );
209
210     }
211
212     /**
213      * Updates an existing calendarobject, based on it's uri.
214      *
215      * @param string $calendarId
216      * @param string $objectUri
217      * @param string $calendarData
218      * @return void
219      */
220     function updateCalendarObject($calendarId,$objectUri,$calendarData) {
221
222         $this->calendarData[$calendarId][$objectUri] = array(
223             'calendardata' => $calendarData,
224             'calendarid' => $calendarId,
225             'uri' => $objectUri,
226         );
227
228     }
229
230     /**
231      * Deletes an existing calendar object.
232      *
233      * @param string $calendarId
234      * @param string $objectUri
235      * @return void
236      */
237     function deleteCalendarObject($calendarId,$objectUri) {
238
239         throw new Exception('Not implemented');
240
241
242     }
243
244     /**
245      * Returns a list of notifications for a given principal url.
246      *
247      * The returned array should only consist of implementations of
248      * Sabre_CalDAV_Notifications_INotificationType.
249      *
250      * @param string $principalUri
251      * @return array
252      */
253     public function getNotificationsForPrincipal($principalUri) {
254
255         if (isset($this->notifications[$principalUri])) {
256             return $this->notifications[$principalUri];
257         }
258         return array();
259
260     }
261
262     /**
263      * This deletes a specific notifcation.
264      *
265      * This may be called by a client once it deems a notification handled.
266      *
267      * @param string $principalUri
268      * @param Sabre_CalDAV_Notifications_INotificationType $notification
269      * @return void
270      */
271     public function deleteNotification($principalUri, Sabre_CalDAV_Notifications_INotificationType $notification) {
272
273         throw new Sabre_DAV_Exception_NotImplemented('This doesn\'t work!');
274
275     }
276
277 }