CalAddress.php
7 months ago
Date.php
7 months ago
DateTime.php
7 months ago
Duration.php
7 months ago
Period.php
7 months ago
Recur.php
7 months ago
Recur.php
345 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AmeliaVendor\Sabre\VObject\Property\ICalendar; |
| 4 | |
| 5 | use AmeliaVendor\Sabre\VObject\InvalidDataException; |
| 6 | use AmeliaVendor\Sabre\VObject\Property; |
| 7 | use AmeliaVendor\Sabre\Xml; |
| 8 | |
| 9 | /** |
| 10 | * Recur property. |
| 11 | * |
| 12 | * This object represents RECUR properties. |
| 13 | * These values are just used for RRULE and the now deprecated EXRULE. |
| 14 | * |
| 15 | * The RRULE property may look something like this: |
| 16 | * |
| 17 | * RRULE:FREQ=MONTHLY;BYDAY=1,2,3;BYHOUR=5. |
| 18 | * |
| 19 | * This property exposes this as a key=>value array that is accessible using |
| 20 | * getParts, and may be set using setParts. |
| 21 | * |
| 22 | * @copyright Copyright (C) fruux GmbH (https://fruux.com/) |
| 23 | * @author Evert Pot (http://evertpot.com/) |
| 24 | * @license http://sabre.io/license/ Modified BSD License |
| 25 | */ |
| 26 | class Recur extends Property |
| 27 | { |
| 28 | /** |
| 29 | * Updates the current value. |
| 30 | * |
| 31 | * This may be either a single, or multiple strings in an array. |
| 32 | * |
| 33 | * @param string|array $value |
| 34 | */ |
| 35 | public function setValue($value) |
| 36 | { |
| 37 | // If we're getting the data from json, we'll be receiving an object |
| 38 | if ($value instanceof \StdClass) { |
| 39 | $value = (array) $value; |
| 40 | } |
| 41 | |
| 42 | if (is_array($value)) { |
| 43 | $newVal = []; |
| 44 | foreach ($value as $k => $v) { |
| 45 | if (is_string($v)) { |
| 46 | $v = strtoupper($v); |
| 47 | |
| 48 | // The value had multiple sub-values |
| 49 | if (false !== strpos($v, ',')) { |
| 50 | $v = explode(',', $v); |
| 51 | } |
| 52 | if (0 === strcmp($k, 'until')) { |
| 53 | $v = strtr($v, [':' => '', '-' => '']); |
| 54 | } |
| 55 | } elseif (is_array($v)) { |
| 56 | $v = array_map('strtoupper', $v); |
| 57 | } |
| 58 | |
| 59 | $newVal[strtoupper($k)] = $v; |
| 60 | } |
| 61 | $this->value = $newVal; |
| 62 | } elseif (is_string($value)) { |
| 63 | $this->value = self::stringToArray($value); |
| 64 | } else { |
| 65 | throw new \InvalidArgumentException('You must either pass a string, or a key=>value array'); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Returns the current value. |
| 71 | * |
| 72 | * This method will always return a singular value. If this was a |
| 73 | * multi-value object, some decision will be made first on how to represent |
| 74 | * it as a string. |
| 75 | * |
| 76 | * To get the correct multi-value version, use getParts. |
| 77 | * |
| 78 | * @return string |
| 79 | */ |
| 80 | public function getValue() |
| 81 | { |
| 82 | $out = []; |
| 83 | foreach ($this->value as $key => $value) { |
| 84 | $out[] = $key.'='.(is_array($value) ? implode(',', $value) : $value); |
| 85 | } |
| 86 | |
| 87 | return strtoupper(implode(';', $out)); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Sets a multi-valued property. |
| 92 | */ |
| 93 | public function setParts(array $parts) |
| 94 | { |
| 95 | $this->setValue($parts); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Returns a multi-valued property. |
| 100 | * |
| 101 | * This method always returns an array, if there was only a single value, |
| 102 | * it will still be wrapped in an array. |
| 103 | * |
| 104 | * @return array |
| 105 | */ |
| 106 | public function getParts() |
| 107 | { |
| 108 | return $this->value; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Sets a raw value coming from a mimedir (iCalendar/vCard) file. |
| 113 | * |
| 114 | * This has been 'unfolded', so only 1 line will be passed. Unescaping is |
| 115 | * not yet done, but parameters are not included. |
| 116 | * |
| 117 | * @param string $val |
| 118 | */ |
| 119 | public function setRawMimeDirValue($val) |
| 120 | { |
| 121 | $this->setValue($val); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Returns a raw mime-dir representation of the value. |
| 126 | * |
| 127 | * @return string |
| 128 | */ |
| 129 | public function getRawMimeDirValue() |
| 130 | { |
| 131 | return $this->getValue(); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Returns the type of value. |
| 136 | * |
| 137 | * This corresponds to the VALUE= parameter. Every property also has a |
| 138 | * 'default' valueType. |
| 139 | * |
| 140 | * @return string |
| 141 | */ |
| 142 | public function getValueType() |
| 143 | { |
| 144 | return 'RECUR'; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Returns the value, in the format it should be encoded for json. |
| 149 | * |
| 150 | * This method must always return an array. |
| 151 | * |
| 152 | * @return array |
| 153 | */ |
| 154 | public function getJsonValue() |
| 155 | { |
| 156 | $values = []; |
| 157 | foreach ($this->getParts() as $k => $v) { |
| 158 | if (0 === strcmp($k, 'UNTIL')) { |
| 159 | $date = new DateTime($this->root, null, $v); |
| 160 | $values[strtolower($k)] = $date->getJsonValue()[0]; |
| 161 | } elseif (0 === strcmp($k, 'COUNT')) { |
| 162 | $values[strtolower($k)] = intval($v); |
| 163 | } else { |
| 164 | $values[strtolower($k)] = $v; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | return [$values]; |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * This method serializes only the value of a property. This is used to |
| 173 | * create xCard or xCal documents. |
| 174 | * |
| 175 | * @param Xml\Writer $writer XML writer |
| 176 | */ |
| 177 | protected function xmlSerializeValue(Xml\Writer $writer) |
| 178 | { |
| 179 | $valueType = strtolower($this->getValueType()); |
| 180 | |
| 181 | foreach ($this->getJsonValue() as $value) { |
| 182 | $writer->writeElement($valueType, $value); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Parses an RRULE value string, and turns it into a struct-ish array. |
| 188 | * |
| 189 | * @param string $value |
| 190 | * |
| 191 | * @return array |
| 192 | */ |
| 193 | public static function stringToArray($value) |
| 194 | { |
| 195 | $value = strtoupper($value); |
| 196 | $newValue = []; |
| 197 | foreach (explode(';', $value) as $part) { |
| 198 | // Skipping empty parts. |
| 199 | if (empty($part)) { |
| 200 | continue; |
| 201 | } |
| 202 | |
| 203 | $parts = explode('=', $part); |
| 204 | |
| 205 | if (2 !== count($parts)) { |
| 206 | throw new InvalidDataException('The supplied iCalendar RRULE part is incorrect: '.$part); |
| 207 | } |
| 208 | |
| 209 | list($partName, $partValue) = $parts; |
| 210 | |
| 211 | // The value itself had multiple values.. |
| 212 | if (false !== strpos($partValue, ',')) { |
| 213 | $partValue = explode(',', $partValue); |
| 214 | } |
| 215 | $newValue[$partName] = $partValue; |
| 216 | } |
| 217 | |
| 218 | return $newValue; |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Validates the node for correctness. |
| 223 | * |
| 224 | * The following options are supported: |
| 225 | * Node::REPAIR - May attempt to automatically repair the problem. |
| 226 | * |
| 227 | * This method returns an array with detected problems. |
| 228 | * Every element has the following properties: |
| 229 | * |
| 230 | * * level - problem level. |
| 231 | * * message - A human-readable string describing the issue. |
| 232 | * * node - A reference to the problematic node. |
| 233 | * |
| 234 | * The level means: |
| 235 | * 1 - The issue was repaired (only happens if REPAIR was turned on) |
| 236 | * 2 - An inconsequential issue |
| 237 | * 3 - A severe issue. |
| 238 | * |
| 239 | * @param int $options |
| 240 | * |
| 241 | * @return array |
| 242 | */ |
| 243 | public function validate($options = 0) |
| 244 | { |
| 245 | $repair = ($options & self::REPAIR); |
| 246 | |
| 247 | $warnings = parent::validate($options); |
| 248 | $values = $this->getParts(); |
| 249 | |
| 250 | foreach ($values as $key => $value) { |
| 251 | if ('' === $value) { |
| 252 | $warnings[] = [ |
| 253 | 'level' => $repair ? 1 : 3, |
| 254 | 'message' => 'Invalid value for '.$key.' in '.$this->name, |
| 255 | 'node' => $this, |
| 256 | ]; |
| 257 | if ($repair) { |
| 258 | unset($values[$key]); |
| 259 | } |
| 260 | } elseif ('BYMONTH' == $key) { |
| 261 | $byMonth = (array) $value; |
| 262 | foreach ($byMonth as $i => $v) { |
| 263 | if (!is_numeric($v) || (int) $v < 1 || (int) $v > 12) { |
| 264 | $warnings[] = [ |
| 265 | 'level' => $repair ? 1 : 3, |
| 266 | 'message' => 'BYMONTH in RRULE must have value(s) between 1 and 12!', |
| 267 | 'node' => $this, |
| 268 | ]; |
| 269 | if ($repair) { |
| 270 | if (is_array($value)) { |
| 271 | unset($values[$key][$i]); |
| 272 | } else { |
| 273 | unset($values[$key]); |
| 274 | } |
| 275 | } |
| 276 | } |
| 277 | } |
| 278 | // if there is no valid entry left, remove the whole value |
| 279 | if (is_array($value) && empty($values[$key])) { |
| 280 | unset($values[$key]); |
| 281 | } |
| 282 | } elseif ('BYWEEKNO' == $key) { |
| 283 | $byWeekNo = (array) $value; |
| 284 | foreach ($byWeekNo as $i => $v) { |
| 285 | if (!is_numeric($v) || (int) $v < -53 || 0 == (int) $v || (int) $v > 53) { |
| 286 | $warnings[] = [ |
| 287 | 'level' => $repair ? 1 : 3, |
| 288 | 'message' => 'BYWEEKNO in RRULE must have value(s) from -53 to -1, or 1 to 53!', |
| 289 | 'node' => $this, |
| 290 | ]; |
| 291 | if ($repair) { |
| 292 | if (is_array($value)) { |
| 293 | unset($values[$key][$i]); |
| 294 | } else { |
| 295 | unset($values[$key]); |
| 296 | } |
| 297 | } |
| 298 | } |
| 299 | } |
| 300 | // if there is no valid entry left, remove the whole value |
| 301 | if (is_array($value) && empty($values[$key])) { |
| 302 | unset($values[$key]); |
| 303 | } |
| 304 | } elseif ('BYYEARDAY' == $key) { |
| 305 | $byYearDay = (array) $value; |
| 306 | foreach ($byYearDay as $i => $v) { |
| 307 | if (!is_numeric($v) || (int) $v < -366 || 0 == (int) $v || (int) $v > 366) { |
| 308 | $warnings[] = [ |
| 309 | 'level' => $repair ? 1 : 3, |
| 310 | 'message' => 'BYYEARDAY in RRULE must have value(s) from -366 to -1, or 1 to 366!', |
| 311 | 'node' => $this, |
| 312 | ]; |
| 313 | if ($repair) { |
| 314 | if (is_array($value)) { |
| 315 | unset($values[$key][$i]); |
| 316 | } else { |
| 317 | unset($values[$key]); |
| 318 | } |
| 319 | } |
| 320 | } |
| 321 | } |
| 322 | // if there is no valid entry left, remove the whole value |
| 323 | if (is_array($value) && empty($values[$key])) { |
| 324 | unset($values[$key]); |
| 325 | } |
| 326 | } |
| 327 | } |
| 328 | if (!isset($values['FREQ'])) { |
| 329 | $warnings[] = [ |
| 330 | 'level' => $repair ? 1 : 3, |
| 331 | 'message' => 'FREQ is required in '.$this->name, |
| 332 | 'node' => $this, |
| 333 | ]; |
| 334 | if ($repair) { |
| 335 | $this->parent->remove($this); |
| 336 | } |
| 337 | } |
| 338 | if ($repair) { |
| 339 | $this->setValue($values); |
| 340 | } |
| 341 | |
| 342 | return $warnings; |
| 343 | } |
| 344 | } |
| 345 |