1<?php
2
3namespace Sabre\DAV\Xml\Property;
4
5use Sabre\DAV\Exception\BadRequest;
6use Sabre\DAV\Sharing\Plugin as SharingPlugin;
7use Sabre\Xml\Element;
8use Sabre\Xml\Reader;
9use Sabre\Xml\Writer;
10
11/**
12 * This class represents the {DAV:}share-access property.
13 *
14 * This property is defined here:
15 * https://tools.ietf.org/html/draft-pot-webdav-resource-sharing-03#section-4.4.1
16 *
17 * This property is used to indicate if a resource is a shared resource, and
18 * whether the instance of the shared resource is the original instance, or
19 * an instance belonging to a sharee.
20 *
21 * @copyright Copyright (C) fruux GmbH (https://fruux.com/).
22 * @author Evert Pot (http://evertpot.com/)
23 * @license http://sabre.io/license/ Modified BSD License
24 */
25class ShareAccess implements Element {
26
27    /**
28     * Either SHARED or SHAREDOWNER
29     *
30     * @var int
31     */
32    protected $value;
33
34    /**
35     * Creates the property.
36     *
37     * The constructor value must be one of the
38     * \Sabre\DAV\Sharing\Plugin::ACCESS_ constants.
39     *
40     * @param int $shareAccess
41     */
42    function __construct($shareAccess) {
43
44        $this->value = $shareAccess;
45
46    }
47
48    /**
49     * Returns the current value.
50     *
51     * @return int
52     */
53    function getValue() {
54
55        return $this->value;
56
57    }
58
59    /**
60     * The xmlSerialize method is called during xml writing.
61     *
62     * Use the $writer argument to write its own xml serialization.
63     *
64     * An important note: do _not_ create a parent element. Any element
65     * implementing XmlSerializable should only ever write what's considered
66     * its 'inner xml'.
67     *
68     * The parent of the current element is responsible for writing a
69     * containing element.
70     *
71     * This allows serializers to be re-used for different element names.
72     *
73     * If you are opening new elements, you must also close them again.
74     *
75     * @param Writer $writer
76     * @return void
77     */
78    function xmlSerialize(Writer $writer) {
79
80        switch ($this->value) {
81
82            case SharingPlugin::ACCESS_NOTSHARED :
83                $writer->writeElement('{DAV:}not-shared');
84                break;
85            case SharingPlugin::ACCESS_SHAREDOWNER :
86                $writer->writeElement('{DAV:}shared-owner');
87                break;
88            case SharingPlugin::ACCESS_READ :
89                $writer->writeElement('{DAV:}read');
90                break;
91            case SharingPlugin::ACCESS_READWRITE :
92                $writer->writeElement('{DAV:}read-write');
93                break;
94            case SharingPlugin::ACCESS_NOACCESS :
95                $writer->writeElement('{DAV:}no-access');
96                break;
97
98        }
99
100    }
101
102    /**
103     * The deserialize method is called during xml parsing.
104     *
105     * This method is called statically, this is because in theory this method
106     * may be used as a type of constructor, or factory method.
107     *
108     * Often you want to return an instance of the current class, but you are
109     * free to return other data as well.
110     *
111     * You are responsible for advancing the reader to the next element. Not
112     * doing anything will result in a never-ending loop.
113     *
114     * If you just want to skip parsing for this element altogether, you can
115     * just call $reader->next();
116     *
117     * $reader->parseInnerTree() will parse the entire sub-tree, and advance to
118     * the next element.
119     *
120     * @param Reader $reader
121     * @return mixed
122     */
123    static function xmlDeserialize(Reader $reader) {
124
125        $elems = $reader->parseInnerTree();
126        foreach ($elems as $elem) {
127            switch ($elem['name']) {
128                case '{DAV:}not-shared' :
129                    return new self(SharingPlugin::ACCESS_NOTSHARED);
130                case '{DAV:}shared-owner' :
131                    return new self(SharingPlugin::ACCESS_SHAREDOWNER);
132                case '{DAV:}read' :
133                    return new self(SharingPlugin::ACCESS_READ);
134                case '{DAV:}read-write' :
135                    return new self(SharingPlugin::ACCESS_READWRITE);
136                case '{DAV:}no-access' :
137                    return new self(SharingPlugin::ACCESS_NOACCESS);
138            }
139        }
140        throw new BadRequest('Invalid value for {DAV:}share-access element');
141
142    }
143}
144