1<?php
2
3namespace Sabre\DAV\PropertyStorage\Backend;
4
5use Sabre\DAV\PropFind;
6use Sabre\DAV\PropPatch;
7
8class Mock implements BackendInterface {
9
10    public $data = [];
11
12    /**
13     * Fetches properties for a path.
14     *
15     * This method received a PropFind object, which contains all the
16     * information about the properties that need to be fetched.
17     *
18     * Ususually you would just want to call 'get404Properties' on this object,
19     * as this will give you the _exact_ list of properties that need to be
20     * fetched, and haven't yet.
21     *
22     * @param string $path
23     * @param PropFind $propFind
24     * @return void
25     */
26    public function propFind($path, PropFind $propFind) {
27
28        if (!isset($this->data[$path])) {
29            return;
30        }
31
32        foreach($this->data[$path] as $name=>$value) {
33            $propFind->set($name, $value);
34        }
35
36    }
37
38    /**
39     * Updates properties for a path
40     *
41     * This method received a PropPatch object, which contains all the
42     * information about the update.
43     *
44     * Usually you would want to call 'handleRemaining' on this object, to get;
45     * a list of all properties that need to be stored.
46     *
47     * @param string $path
48     * @param PropPatch $propPatch
49     * @return void
50     */
51    public function propPatch($path, PropPatch $propPatch) {
52
53        if (!isset($this->data[$path])) {
54            $this->data[$path] = [];
55        }
56        $propPatch->handleRemaining(function($properties) use ($path) {
57
58            foreach($properties as $propName=>$propValue) {
59
60                if (is_null($propValue)) {
61                    unset($this->data[$path][$propName]);
62                } else {
63                    $this->data[$path][$propName] = $propValue;
64                }
65                return true;
66
67            }
68
69        });
70
71    }
72
73    /**
74     * This method is called after a node is deleted.
75     *
76     * This allows a backend to clean up all associated properties.
77     */
78    public function delete($path) {
79
80        unset($this->data[$path]);
81
82    }
83
84    /**
85     * This method is called after a successful MOVE
86     *
87     * This should be used to migrate all properties from one path to another.
88     * Note that entire collections may be moved, so ensure that all properties
89     * for children are also moved along.
90     *
91     * @param string $source
92     * @param string $destination
93     * @return void
94     */
95    public function move($source, $destination) {
96
97        foreach($this->data as $path => $props) {
98
99            if ($path === $source) {
100                $this->data[$destination] = $props;
101                unset($this->data[$path]);
102                continue;
103            }
104
105            if (strpos($path, $source . '/')===0) {
106                $this->data[$destination . substr($path, strlen($source)+1)] = $props;
107                unset($this->data[$path]);
108            }
109
110        }
111
112    }
113
114}
115