1<?php 2 3namespace Sabre\VObject\Property\ICalendar; 4 5use Sabre\VObject\DateTimeParser; 6use Sabre\VObject\Property; 7use Sabre\Xml; 8 9/** 10 * Period property. 11 * 12 * This object represents PERIOD values, as defined here: 13 * 14 * http://tools.ietf.org/html/rfc5545#section-3.8.2.6 15 * 16 * @copyright Copyright (C) fruux GmbH (https://fruux.com/) 17 * @author Evert Pot (http://evertpot.com/) 18 * @license http://sabre.io/license/ Modified BSD License 19 */ 20class Period extends Property { 21 22 /** 23 * In case this is a multi-value property. This string will be used as a 24 * delimiter. 25 * 26 * @var string|null 27 */ 28 public $delimiter = ','; 29 30 /** 31 * Sets a raw value coming from a mimedir (iCalendar/vCard) file. 32 * 33 * This has been 'unfolded', so only 1 line will be passed. Unescaping is 34 * not yet done, but parameters are not included. 35 * 36 * @param string $val 37 * 38 * @return void 39 */ 40 function setRawMimeDirValue($val) { 41 42 $this->setValue(explode($this->delimiter, $val)); 43 44 } 45 46 /** 47 * Returns a raw mime-dir representation of the value. 48 * 49 * @return string 50 */ 51 function getRawMimeDirValue() { 52 53 return implode($this->delimiter, $this->getParts()); 54 55 } 56 57 /** 58 * Returns the type of value. 59 * 60 * This corresponds to the VALUE= parameter. Every property also has a 61 * 'default' valueType. 62 * 63 * @return string 64 */ 65 function getValueType() { 66 67 return 'PERIOD'; 68 69 } 70 71 /** 72 * Sets the json value, as it would appear in a jCard or jCal object. 73 * 74 * The value must always be an array. 75 * 76 * @param array $value 77 * 78 * @return void 79 */ 80 function setJsonValue(array $value) { 81 82 $value = array_map( 83 function($item) { 84 85 return strtr(implode('/', $item), [':' => '', '-' => '']); 86 87 }, 88 $value 89 ); 90 parent::setJsonValue($value); 91 92 } 93 94 /** 95 * Returns the value, in the format it should be encoded for json. 96 * 97 * This method must always return an array. 98 * 99 * @return array 100 */ 101 function getJsonValue() { 102 103 $return = []; 104 foreach ($this->getParts() as $item) { 105 106 list($start, $end) = explode('/', $item, 2); 107 108 $start = DateTimeParser::parseDateTime($start); 109 110 // This is a duration value. 111 if ($end[0] === 'P') { 112 $return[] = [ 113 $start->format('Y-m-d\\TH:i:s'), 114 $end 115 ]; 116 } else { 117 $end = DateTimeParser::parseDateTime($end); 118 $return[] = [ 119 $start->format('Y-m-d\\TH:i:s'), 120 $end->format('Y-m-d\\TH:i:s'), 121 ]; 122 } 123 124 } 125 126 return $return; 127 128 } 129 130 /** 131 * This method serializes only the value of a property. This is used to 132 * create xCard or xCal documents. 133 * 134 * @param Xml\Writer $writer XML writer. 135 * 136 * @return void 137 */ 138 protected function xmlSerializeValue(Xml\Writer $writer) { 139 140 $writer->startElement(strtolower($this->getValueType())); 141 $value = $this->getJsonValue(); 142 $writer->writeElement('start', $value[0][0]); 143 144 if ($value[0][1][0] === 'P') { 145 $writer->writeElement('duration', $value[0][1]); 146 } 147 else { 148 $writer->writeElement('end', $value[0][1]); 149 } 150 151 $writer->endElement(); 152 153 } 154 155} 156