1 <?php 2 3 namespace Sabre\CalDAV\Backend; 4 use Sabre\DAV; 5 use Sabre\CalDAV; 6 7 class Mock extends AbstractBackend { 8 9 protected $calendarData; 10 protected $calendars; 11 12 function __construct(array $calendars = [], array $calendarData = []) { 13 14 foreach($calendars as &$calendar) { 15 if (!isset($calendar['id'])) { 16 $calendar['id'] = DAV\UUIDUtil::getUUID(); 17 } 18 } 19 20 $this->calendars = $calendars; 21 $this->calendarData = $calendarData; 22 23 } 24 25 /** 26 * Returns a list of calendars for a principal. 27 * 28 * Every project is an array with the following keys: 29 * * id, a unique id that will be used by other functions to modify the 30 * calendar. This can be the same as the uri or a database key. 31 * * uri, which the basename of the uri with which the calendar is 32 * accessed. 33 * * principalUri. The owner of the calendar. Almost always the same as 34 * principalUri passed to this method. 35 * 36 * Furthermore it can contain webdav properties in clark notation. A very 37 * common one is '{DAV:}displayname'. 38 * 39 * @param string $principalUri 40 * @return array 41 */ 42 function getCalendarsForUser($principalUri) { 43 44 $r = array(); 45 foreach($this->calendars as $row) { 46 if ($row['principaluri'] == $principalUri) { 47 $r[] = $row; 48 } 49 } 50 51 return $r; 52 53 } 54 55 /** 56 * Creates a new calendar for a principal. 57 * 58 * If the creation was a success, an id must be returned that can be used to reference 59 * this calendar in other methods, such as updateCalendar. 60 * 61 * This function must return a server-wide unique id that can be used 62 * later to reference the calendar. 63 * 64 * @param string $principalUri 65 * @param string $calendarUri 66 * @param array $properties 67 * @return string|int 68 */ 69 function createCalendar($principalUri,$calendarUri,array $properties) { 70 71 $id = DAV\UUIDUtil::getUUID(); 72 $this->calendars[] = array_merge([ 73 'id' => $id, 74 'principaluri' => $principalUri, 75 'uri' => $calendarUri, 76 '{' . CalDAV\Plugin::NS_CALDAV . '}supported-calendar-component-set' => new CalDAV\Xml\Property\SupportedCalendarComponentSet(['VEVENT','VTODO']), 77 ], $properties); 78 79 return $id; 80 81 } 82 83 /** 84 * Delete a calendar and all it's objects 85 * 86 * @param string $calendarId 87 * @return void 88 */ 89 public function deleteCalendar($calendarId) { 90 91 foreach($this->calendars as $k=>$calendar) { 92 if ($calendar['id'] === $calendarId) { 93 unset($this->calendars[$k]); 94 } 95 } 96 97 } 98 99 /** 100 * Returns all calendar objects within a calendar object. 101 * 102 * Every item contains an array with the following keys: 103 * * id - unique identifier which will be used for subsequent updates 104 * * calendardata - The iCalendar-compatible calendar data 105 * * uri - a unique key which will be used to construct the uri. This can be any arbitrary string. 106 * * lastmodified - a timestamp of the last modification time 107 * * etag - An arbitrary string, surrounded by double-quotes. (e.g.: 108 * ' "abcdef"') 109 * * calendarid - The calendarid as it was passed to this function. 110 * 111 * Note that the etag is optional, but it's highly encouraged to return for 112 * speed reasons. 113 * 114 * The calendardata is also optional. If it's not returned 115 * 'getCalendarObject' will be called later, which *is* expected to return 116 * calendardata. 117 * 118 * @param string $calendarId 119 * @return array 120 */ 121 public function getCalendarObjects($calendarId) { 122 123 if (!isset($this->calendarData[$calendarId])) 124 return array(); 125 126 $objects = $this->calendarData[$calendarId]; 127 128 foreach($objects as $uri => &$object) { 129 $object['calendarid'] = $calendarId; 130 $object['uri'] = $uri; 131 $object['lastmodified'] = null; 132 } 133 return $objects; 134 135 } 136 137 /** 138 * Returns information from a single calendar object, based on it's object 139 * uri. 140 * 141 * The returned array must have the same keys as getCalendarObjects. The 142 * 'calendardata' object is required here though, while it's not required 143 * for getCalendarObjects. 144 * 145 * @param string $calendarId 146 * @param string $objectUri 147 * @return array 148 */ 149 function getCalendarObject($calendarId,$objectUri) { 150 151 if (!isset($this->calendarData[$calendarId][$objectUri])) { 152 throw new DAV\Exception\NotFound('Object could not be found'); 153 } 154 $object = $this->calendarData[$calendarId][$objectUri]; 155 $object['calendarid'] = $calendarId; 156 $object['uri'] = $objectUri; 157 $object['lastmodified'] = null; 158 return $object; 159 160 } 161 162 /** 163 * Creates a new calendar object. 164 * 165 * @param string $calendarId 166 * @param string $objectUri 167 * @param string $calendarData 168 * @return void 169 */ 170 function createCalendarObject($calendarId,$objectUri,$calendarData) { 171 172 $this->calendarData[$calendarId][$objectUri] = array( 173 'calendardata' => $calendarData, 174 'calendarid' => $calendarId, 175 'uri' => $objectUri, 176 ); 177 return '"' . md5($calendarData) . '"'; 178 179 } 180 181 /** 182 * Updates an existing calendarobject, based on it's uri. 183 * 184 * @param string $calendarId 185 * @param string $objectUri 186 * @param string $calendarData 187 * @return void 188 */ 189 function updateCalendarObject($calendarId,$objectUri,$calendarData) { 190 191 $this->calendarData[$calendarId][$objectUri] = array( 192 'calendardata' => $calendarData, 193 'calendarid' => $calendarId, 194 'uri' => $objectUri, 195 ); 196 return '"' . md5($calendarData) . '"'; 197 198 } 199 200 /** 201 * Deletes an existing calendar object. 202 * 203 * @param string $calendarId 204 * @param string $objectUri 205 * @return void 206 */ 207 function deleteCalendarObject($calendarId,$objectUri) { 208 209 throw new Exception('Not implemented'); 210 211 212 } 213 214 } 215