1*a1a3b679SAndreas Boehler<?php 2*a1a3b679SAndreas Boehler 3*a1a3b679SAndreas Boehlernamespace Sabre\VObject\Property\ICalendar; 4*a1a3b679SAndreas Boehler 5*a1a3b679SAndreas Boehleruse 6*a1a3b679SAndreas Boehler Sabre\VObject\Property, 7*a1a3b679SAndreas Boehler Sabre\VObject\Parser\MimeDir; 8*a1a3b679SAndreas Boehler 9*a1a3b679SAndreas Boehler/** 10*a1a3b679SAndreas Boehler * Recur property 11*a1a3b679SAndreas Boehler * 12*a1a3b679SAndreas Boehler * This object represents RECUR properties. 13*a1a3b679SAndreas Boehler * These values are just used for RRULE and the now deprecated EXRULE. 14*a1a3b679SAndreas Boehler * 15*a1a3b679SAndreas Boehler * The RRULE property may look something like this: 16*a1a3b679SAndreas Boehler * 17*a1a3b679SAndreas Boehler * RRULE:FREQ=MONTHLY;BYDAY=1,2,3;BYHOUR=5. 18*a1a3b679SAndreas Boehler * 19*a1a3b679SAndreas Boehler * This property exposes this as a key=>value array that is accessible using 20*a1a3b679SAndreas Boehler * getParts, and may be set using setParts. 21*a1a3b679SAndreas Boehler * 22*a1a3b679SAndreas Boehler * @copyright Copyright (C) 2011-2015 fruux GmbH (https://fruux.com/). 23*a1a3b679SAndreas Boehler * @author Evert Pot (http://evertpot.com/) 24*a1a3b679SAndreas Boehler * @license http://sabre.io/license/ Modified BSD License 25*a1a3b679SAndreas Boehler */ 26*a1a3b679SAndreas Boehlerclass Recur extends Property { 27*a1a3b679SAndreas Boehler 28*a1a3b679SAndreas Boehler /** 29*a1a3b679SAndreas Boehler * Updates the current value. 30*a1a3b679SAndreas Boehler * 31*a1a3b679SAndreas Boehler * This may be either a single, or multiple strings in an array. 32*a1a3b679SAndreas Boehler * 33*a1a3b679SAndreas Boehler * @param string|array $value 34*a1a3b679SAndreas Boehler * @return void 35*a1a3b679SAndreas Boehler */ 36*a1a3b679SAndreas Boehler public function setValue($value) { 37*a1a3b679SAndreas Boehler 38*a1a3b679SAndreas Boehler // If we're getting the data from json, we'll be receiving an object 39*a1a3b679SAndreas Boehler if ($value instanceof \StdClass) { 40*a1a3b679SAndreas Boehler $value = (array)$value; 41*a1a3b679SAndreas Boehler } 42*a1a3b679SAndreas Boehler 43*a1a3b679SAndreas Boehler if (is_array($value)) { 44*a1a3b679SAndreas Boehler $newVal = array(); 45*a1a3b679SAndreas Boehler foreach($value as $k=>$v) { 46*a1a3b679SAndreas Boehler 47*a1a3b679SAndreas Boehler if (is_string($v)) { 48*a1a3b679SAndreas Boehler $v = strtoupper($v); 49*a1a3b679SAndreas Boehler 50*a1a3b679SAndreas Boehler // The value had multiple sub-values 51*a1a3b679SAndreas Boehler if (strpos($v,',')!==false) { 52*a1a3b679SAndreas Boehler $v = explode(',', $v); 53*a1a3b679SAndreas Boehler } 54*a1a3b679SAndreas Boehler } else { 55*a1a3b679SAndreas Boehler $v = array_map('strtoupper', $v); 56*a1a3b679SAndreas Boehler } 57*a1a3b679SAndreas Boehler 58*a1a3b679SAndreas Boehler $newVal[strtoupper($k)] = $v; 59*a1a3b679SAndreas Boehler } 60*a1a3b679SAndreas Boehler $this->value = $newVal; 61*a1a3b679SAndreas Boehler } elseif (is_string($value)) { 62*a1a3b679SAndreas Boehler $this->value = self::stringToArray($value); 63*a1a3b679SAndreas Boehler } else { 64*a1a3b679SAndreas Boehler throw new \InvalidArgumentException('You must either pass a string, or a key=>value array'); 65*a1a3b679SAndreas Boehler } 66*a1a3b679SAndreas Boehler 67*a1a3b679SAndreas Boehler } 68*a1a3b679SAndreas Boehler 69*a1a3b679SAndreas Boehler /** 70*a1a3b679SAndreas Boehler * Returns the current value. 71*a1a3b679SAndreas Boehler * 72*a1a3b679SAndreas Boehler * This method will always return a singular value. If this was a 73*a1a3b679SAndreas Boehler * multi-value object, some decision will be made first on how to represent 74*a1a3b679SAndreas Boehler * it as a string. 75*a1a3b679SAndreas Boehler * 76*a1a3b679SAndreas Boehler * To get the correct multi-value version, use getParts. 77*a1a3b679SAndreas Boehler * 78*a1a3b679SAndreas Boehler * @return string 79*a1a3b679SAndreas Boehler */ 80*a1a3b679SAndreas Boehler public function getValue() { 81*a1a3b679SAndreas Boehler 82*a1a3b679SAndreas Boehler $out = array(); 83*a1a3b679SAndreas Boehler foreach($this->value as $key=>$value) { 84*a1a3b679SAndreas Boehler $out[] = $key . '=' . (is_array($value)?implode(',', $value):$value); 85*a1a3b679SAndreas Boehler } 86*a1a3b679SAndreas Boehler return strtoupper(implode(';',$out)); 87*a1a3b679SAndreas Boehler 88*a1a3b679SAndreas Boehler } 89*a1a3b679SAndreas Boehler 90*a1a3b679SAndreas Boehler /** 91*a1a3b679SAndreas Boehler * Sets a multi-valued property. 92*a1a3b679SAndreas Boehler * 93*a1a3b679SAndreas Boehler * @param array $parts 94*a1a3b679SAndreas Boehler * @return void 95*a1a3b679SAndreas Boehler */ 96*a1a3b679SAndreas Boehler public function setParts(array $parts) { 97*a1a3b679SAndreas Boehler 98*a1a3b679SAndreas Boehler $this->setValue($parts); 99*a1a3b679SAndreas Boehler 100*a1a3b679SAndreas Boehler } 101*a1a3b679SAndreas Boehler 102*a1a3b679SAndreas Boehler /** 103*a1a3b679SAndreas Boehler * Returns a multi-valued property. 104*a1a3b679SAndreas Boehler * 105*a1a3b679SAndreas Boehler * This method always returns an array, if there was only a single value, 106*a1a3b679SAndreas Boehler * it will still be wrapped in an array. 107*a1a3b679SAndreas Boehler * 108*a1a3b679SAndreas Boehler * @return array 109*a1a3b679SAndreas Boehler */ 110*a1a3b679SAndreas Boehler public function getParts() { 111*a1a3b679SAndreas Boehler 112*a1a3b679SAndreas Boehler return $this->value; 113*a1a3b679SAndreas Boehler 114*a1a3b679SAndreas Boehler } 115*a1a3b679SAndreas Boehler 116*a1a3b679SAndreas Boehler /** 117*a1a3b679SAndreas Boehler * Sets a raw value coming from a mimedir (iCalendar/vCard) file. 118*a1a3b679SAndreas Boehler * 119*a1a3b679SAndreas Boehler * This has been 'unfolded', so only 1 line will be passed. Unescaping is 120*a1a3b679SAndreas Boehler * not yet done, but parameters are not included. 121*a1a3b679SAndreas Boehler * 122*a1a3b679SAndreas Boehler * @param string $val 123*a1a3b679SAndreas Boehler * @return void 124*a1a3b679SAndreas Boehler */ 125*a1a3b679SAndreas Boehler public function setRawMimeDirValue($val) { 126*a1a3b679SAndreas Boehler 127*a1a3b679SAndreas Boehler $this->setValue($val); 128*a1a3b679SAndreas Boehler 129*a1a3b679SAndreas Boehler } 130*a1a3b679SAndreas Boehler 131*a1a3b679SAndreas Boehler /** 132*a1a3b679SAndreas Boehler * Returns a raw mime-dir representation of the value. 133*a1a3b679SAndreas Boehler * 134*a1a3b679SAndreas Boehler * @return string 135*a1a3b679SAndreas Boehler */ 136*a1a3b679SAndreas Boehler public function getRawMimeDirValue() { 137*a1a3b679SAndreas Boehler 138*a1a3b679SAndreas Boehler return $this->getValue(); 139*a1a3b679SAndreas Boehler 140*a1a3b679SAndreas Boehler } 141*a1a3b679SAndreas Boehler 142*a1a3b679SAndreas Boehler /** 143*a1a3b679SAndreas Boehler * Returns the type of value. 144*a1a3b679SAndreas Boehler * 145*a1a3b679SAndreas Boehler * This corresponds to the VALUE= parameter. Every property also has a 146*a1a3b679SAndreas Boehler * 'default' valueType. 147*a1a3b679SAndreas Boehler * 148*a1a3b679SAndreas Boehler * @return string 149*a1a3b679SAndreas Boehler */ 150*a1a3b679SAndreas Boehler public function getValueType() { 151*a1a3b679SAndreas Boehler 152*a1a3b679SAndreas Boehler return "RECUR"; 153*a1a3b679SAndreas Boehler 154*a1a3b679SAndreas Boehler } 155*a1a3b679SAndreas Boehler 156*a1a3b679SAndreas Boehler /** 157*a1a3b679SAndreas Boehler * Returns the value, in the format it should be encoded for json. 158*a1a3b679SAndreas Boehler * 159*a1a3b679SAndreas Boehler * This method must always return an array. 160*a1a3b679SAndreas Boehler * 161*a1a3b679SAndreas Boehler * @return array 162*a1a3b679SAndreas Boehler */ 163*a1a3b679SAndreas Boehler public function getJsonValue() { 164*a1a3b679SAndreas Boehler 165*a1a3b679SAndreas Boehler $values = array(); 166*a1a3b679SAndreas Boehler foreach($this->getParts() as $k=>$v) { 167*a1a3b679SAndreas Boehler $values[strtolower($k)] = $v; 168*a1a3b679SAndreas Boehler } 169*a1a3b679SAndreas Boehler return array($values); 170*a1a3b679SAndreas Boehler 171*a1a3b679SAndreas Boehler } 172*a1a3b679SAndreas Boehler 173*a1a3b679SAndreas Boehler /** 174*a1a3b679SAndreas Boehler * Parses an RRULE value string, and turns it into a struct-ish array. 175*a1a3b679SAndreas Boehler * 176*a1a3b679SAndreas Boehler * @param string $value 177*a1a3b679SAndreas Boehler * @return array 178*a1a3b679SAndreas Boehler */ 179*a1a3b679SAndreas Boehler static function stringToArray($value) { 180*a1a3b679SAndreas Boehler 181*a1a3b679SAndreas Boehler $value = strtoupper($value); 182*a1a3b679SAndreas Boehler $newValue = array(); 183*a1a3b679SAndreas Boehler foreach(explode(';', $value) as $part) { 184*a1a3b679SAndreas Boehler 185*a1a3b679SAndreas Boehler // Skipping empty parts. 186*a1a3b679SAndreas Boehler if (empty($part)) { 187*a1a3b679SAndreas Boehler continue; 188*a1a3b679SAndreas Boehler } 189*a1a3b679SAndreas Boehler list($partName, $partValue) = explode('=', $part); 190*a1a3b679SAndreas Boehler 191*a1a3b679SAndreas Boehler // The value itself had multiple values.. 192*a1a3b679SAndreas Boehler if (strpos($partValue,',')!==false) { 193*a1a3b679SAndreas Boehler $partValue=explode(',', $partValue); 194*a1a3b679SAndreas Boehler } 195*a1a3b679SAndreas Boehler $newValue[$partName] = $partValue; 196*a1a3b679SAndreas Boehler 197*a1a3b679SAndreas Boehler } 198*a1a3b679SAndreas Boehler 199*a1a3b679SAndreas Boehler return $newValue; 200*a1a3b679SAndreas Boehler 201*a1a3b679SAndreas Boehler } 202*a1a3b679SAndreas Boehler 203*a1a3b679SAndreas Boehler} 204