1<?php
2
3namespace Sabre\VObject\Property\ICalendar;
4
5use
6    Sabre\VObject\Property,
7    Sabre\VObject\Parser\MimeDir,
8    Sabre\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) 2011-2015 fruux GmbH (https://fruux.com/).
18 * @author Evert Pot (http://evertpot.com/)
19 * @license http://sabre.io/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(
82            function($item) {
83
84                return strtr(implode('/', $item), array(':' => '', '-' => ''));
85
86            },
87            $value
88        );
89        parent::setJsonValue($value);
90
91    }
92
93    /**
94     * Returns the value, in the format it should be encoded for json.
95     *
96     * This method must always return an array.
97     *
98     * @return array
99     */
100    public function getJsonValue() {
101
102        $return = array();
103        foreach($this->getParts() as $item) {
104
105            list($start, $end) = explode('/', $item, 2);
106
107            $start = DateTimeParser::parseDateTime($start);
108
109            // This is a duration value.
110            if ($end[0]==='P') {
111                $return[] = array(
112                    $start->format('Y-m-d\\TH:i:s'),
113                    $end
114                );
115            } else {
116                $end = DateTimeParser::parseDateTime($end);
117                $return[] = array(
118                    $start->format('Y-m-d\\TH:i:s'),
119                    $end->format('Y-m-d\\TH:i:s'),
120                );
121            }
122
123        }
124
125        return $return;
126
127    }
128
129}
130