1<?php
2
3namespace SabreForRainLoop\VObject\Property\ICalendar;
4
5use
6    SabreForRainLoop\VObject\Property,
7    SabreForRainLoop\VObject\Parser\MimeDir,
8    SabreForRainLoop\VObject\DateTimeParser;
9
10/**
11 * Period property
12 *
13 * This object represents PERIOD values, as defined here:
14 *
15 * http://tools.ietf.org/html/rfc5545#section-3.8.2.6
16 *
17 * @copyright Copyright (C) 2007-2013 fruux GmbH. All rights reserved.
18 * @author Evert Pot (http://evertpot.com/)
19 * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
20 */
21class Period extends Property {
22
23    /**
24     * In case this is a multi-value property. This string will be used as a
25     * delimiter.
26     *
27     * @var string|null
28     */
29    public $delimiter = ',';
30
31    /**
32     * Sets a raw value coming from a mimedir (iCalendar/vCard) file.
33     *
34     * This has been 'unfolded', so only 1 line will be passed. Unescaping is
35     * not yet done, but parameters are not included.
36     *
37     * @param string $val
38     * @return void
39     */
40    public 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    public 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    public 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     * @return void
78     */
79    public function setJsonValue(array $value) {
80
81        $value = array_map(function($item) {
82
83            return strtr(implode('/', $item), array(':' => '', '-' => ''));
84
85        }, $value);
86        parent::setJsonValue($value);
87
88    }
89
90    /**
91     * Returns the value, in the format it should be encoded for json.
92     *
93     * This method must always return an array.
94     *
95     * @return array
96     */
97    public function getJsonValue() {
98
99        $return = array();
100        foreach($this->getParts() as $item) {
101
102            list($start, $end) = explode('/', $item, 2);
103
104            $start = DateTimeParser::parseDateTime($start);
105
106            // This is a duration value.
107            if ($end[0]==='P') {
108                $return[] = array(
109                    $start->format('Y-m-d\\TH:i:s'),
110                    $end
111                );
112            } else {
113                $end = DateTimeParser::parseDateTime($end);
114                $return[] = array(
115                    $start->format('Y-m-d\\TH:i:s'),
116                    $end->format('Y-m-d\\TH:i:s'),
117                );
118            }
119
120        }
121
122        return $return;
123
124    }
125
126}
127