1<?php
2
3namespace Sabre\CalDAV\Xml\Property;
4
5use Sabre\Xml\Element;
6use Sabre\Xml\Reader;
7use Sabre\Xml\Writer;
8use Sabre\Xml\Element\Elements;
9use Sabre\CalDAV\Plugin;
10
11/**
12 * schedule-calendar-transp property.
13 *
14 * This property is a representation of the schedule-calendar-transp property.
15 * This property is defined in:
16 *
17 * http://tools.ietf.org/html/rfc6638#section-9.1
18 *
19 * Its values are either 'transparent' or 'opaque'. If it's transparent, it
20 * means that this calendar will not be taken into consideration when a
21 * different user queries for free-busy information. If it's 'opaque', it will.
22 *
23 * @copyright Copyright (C) 2007-2015 fruux GmbH (https://fruux.com/).
24 * @author Evert Pot (http://www.rooftopsolutions.nl/)
25 * @license http://sabre.io/license/ Modified BSD License
26 */
27class ScheduleCalendarTransp implements Element {
28
29    const TRANSPARENT = 'transparent';
30    const OPAQUE = 'opaque';
31
32    /**
33     * value
34     *
35     * @var string
36     */
37    protected $value;
38
39    /**
40     * Creates the property
41     *
42     * @param string $value
43     */
44    function __construct($value) {
45
46        if ($value !== self::TRANSPARENT && $value !== self::OPAQUE) {
47            throw new \InvalidArgumentException('The value must either be specified as "transparent" or "opaque"');
48        }
49        $this->value = $value;
50
51    }
52
53    /**
54     * Returns the current value
55     *
56     * @return string
57     */
58    function getValue() {
59
60        return $this->value;
61
62    }
63
64    /**
65     * The xmlSerialize metod is called during xml writing.
66     *
67     * Use the $writer argument to write its own xml serialization.
68     *
69     * An important note: do _not_ create a parent element. Any element
70     * implementing XmlSerializble should only ever write what's considered
71     * its 'inner xml'.
72     *
73     * The parent of the current element is responsible for writing a
74     * containing element.
75     *
76     * This allows serializers to be re-used for different element names.
77     *
78     * If you are opening new elements, you must also close them again.
79     *
80     * @param Writer $writer
81     * @return void
82     */
83    function xmlSerialize(Writer $writer) {
84
85        switch ($this->value) {
86            case self::TRANSPARENT :
87                $writer->writeElement('{' . Plugin::NS_CALDAV . '}transparent');
88                break;
89            case self::OPAQUE :
90                $writer->writeElement('{' . Plugin::NS_CALDAV . '}opaque');
91                break;
92        }
93
94    }
95
96    /**
97     * The deserialize method is called during xml parsing.
98     *
99     * This method is called statictly, this is because in theory this method
100     * may be used as a type of constructor, or factory method.
101     *
102     * Often you want to return an instance of the current class, but you are
103     * free to return other data as well.
104     *
105     * You are responsible for advancing the reader to the next element. Not
106     * doing anything will result in a never-ending loop.
107     *
108     * If you just want to skip parsing for this element altogether, you can
109     * just call $reader->next();
110     *
111     * $reader->parseInnerTree() will parse the entire sub-tree, and advance to
112     * the next element.
113     *
114     * @param Reader $reader
115     * @return mixed
116     */
117    static function xmlDeserialize(Reader $reader) {
118
119        $elems = Elements::xmlDeserialize($reader);
120
121        $value = null;
122
123        foreach ($elems as $elem) {
124            switch ($elem) {
125                case '{' . Plugin::NS_CALDAV . '}opaque' :
126                    $value = self::OPAQUE;
127                    break;
128                case '{' . Plugin::NS_CALDAV . '}transparent' :
129                    $value = self::TRANSPARENT;
130                    break;
131            }
132        }
133        if (is_null($value))
134           return null;
135
136        return new self($value);
137
138    }
139
140}
141