1<?php
2
3namespace Sabre\DAV\Xml\Request;
4
5use Sabre\DAV\Locks\LockInfo;
6use Sabre\Xml\Element\KeyValue;
7use Sabre\Xml\Reader;
8use Sabre\Xml\XmlDeserializable;
9
10/**
11 * WebDAV LOCK request parser.
12 *
13 * This class parses the {DAV:}lockinfo request, as defined in:
14 *
15 * http://tools.ietf.org/html/rfc4918#section-9.10
16 *
17 * @copyright Copyright (C) 2007-2015 fruux GmbH (https://fruux.com/).
18 * @author Evert Pot (http://evertpot.com/)
19 * @license http://sabre.io/license/ Modified BSD License
20 */
21class Lock implements XmlDeserializable {
22
23    /**
24     * Owner of the lock
25     *
26     * @var string
27     */
28    public $owner;
29
30    /**
31     * Scope of the lock.
32     *
33     * Either LockInfo::SHARED or LockInfo::EXCLUSIVE
34     * @var int
35     */
36    public $scope;
37
38    /**
39     * The deserialize method is called during xml parsing.
40     *
41     * This method is called statictly, this is because in theory this method
42     * may be used as a type of constructor, or factory method.
43     *
44     * Often you want to return an instance of the current class, but you are
45     * free to return other data as well.
46     *
47     * You are responsible for advancing the reader to the next element. Not
48     * doing anything will result in a never-ending loop.
49     *
50     * If you just want to skip parsing for this element altogether, you can
51     * just call $reader->next();
52     *
53     * $reader->parseInnerTree() will parse the entire sub-tree, and advance to
54     * the next element.
55     *
56     * @param Reader $reader
57     * @return mixed
58     */
59    static function xmlDeserialize(Reader $reader) {
60
61        $reader->pushContext();
62        $reader->elementMap['{DAV:}owner'] = 'Sabre\\Xml\\Element\\XmlFragment';
63
64        $values = KeyValue::xmlDeserialize($reader);
65
66        $reader->popContext();
67
68        $new = new self();
69        $new->owner = !empty($values['{DAV:}owner']) ? $values['{DAV:}owner']->getXml() : null;
70        $new->scope = LockInfo::SHARED;
71
72        if (isset($values['{DAV:}lockscope'])) {
73            foreach ($values['{DAV:}lockscope'] as $elem) {
74                if ($elem['name'] === '{DAV:}exclusive') $new->scope = LockInfo::EXCLUSIVE;
75            }
76        }
77        return $new;
78
79    }
80
81}
82