1<?php
2
3namespace Sabre\DAV\Browser;
4
5use Sabre\DAV\PropFind;
6
7/**
8 * This class is used by the browser plugin to trick the system in returning
9 * every defined property.
10 *
11 * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
12 * @author Evert Pot (http://evertpot.com/)
13 * @license http://sabre.io/license/ Modified BSD License
14 */
15class PropFindAll extends PropFind {
16
17    /**
18     * Creates the PROPFIND object
19     *
20     * @param string $path
21     */
22    function __construct($path) {
23
24        parent::__construct($path, []);
25
26    }
27
28    /**
29     * Handles a specific property.
30     *
31     * This method checks whether the specified property was requested in this
32     * PROPFIND request, and if so, it will call the callback and use the
33     * return value for it's value.
34     *
35     * Example:
36     *
37     * $propFind->handle('{DAV:}displayname', function() {
38     *      return 'hello';
39     * });
40     *
41     * Note that handle will only work the first time. If null is returned, the
42     * value is ignored.
43     *
44     * It's also possible to not pass a callback, but immediately pass a value
45     *
46     * @param string $propertyName
47     * @param mixed $valueOrCallBack
48     * @return void
49     */
50    function handle($propertyName, $valueOrCallBack) {
51
52        if (is_callable($valueOrCallBack)) {
53            $value = $valueOrCallBack();
54        } else {
55            $value = $valueOrCallBack;
56        }
57        if (!is_null($value)) {
58            $this->result[$propertyName] = [200, $value];
59        }
60
61    }
62
63    /**
64     * Sets the value of the property
65     *
66     * If status is not supplied, the status will default to 200 for non-null
67     * properties, and 404 for null properties.
68     *
69     * @param string $propertyName
70     * @param mixed $value
71     * @param int $status
72     * @return void
73     */
74    function set($propertyName, $value, $status = null) {
75
76        if (is_null($status)) {
77            $status = is_null($value) ? 404 : 200;
78        }
79        $this->result[$propertyName] = [$status, $value];
80
81    }
82
83    /**
84     * Returns the current value for a property.
85     *
86     * @param string $propertyName
87     * @return mixed
88     */
89    function get($propertyName) {
90
91        return isset($this->result[$propertyName]) ? $this->result[$propertyName][1] : null;
92
93    }
94
95    /**
96     * Returns the current status code for a property name.
97     *
98     * If the property does not appear in the list of requested properties,
99     * null will be returned.
100     *
101     * @param string $propertyName
102     * @return int|null
103     */
104    function getStatus($propertyName) {
105
106        return isset($this->result[$propertyName]) ? $this->result[$propertyName][0] : 404;
107
108    }
109
110    /**
111     * Returns all propertynames that have a 404 status, and thus don't have a
112     * value yet.
113     *
114     * @return array
115     */
116    function get404Properties() {
117
118        $result = [];
119        foreach ($this->result as $propertyName => $stuff) {
120            if ($stuff[0] === 404) {
121                $result[] = $propertyName;
122            }
123        }
124        // If there's nothing in this list, we're adding one fictional item.
125        if (!$result) {
126            $result[] = '{http://sabredav.org/ns}idk';
127        }
128        return $result;
129
130    }
131
132}
133