1<?php
2
3namespace Sabre\DAV\Mock;
4
5use
6    Sabre\DAV\IProperties,
7    Sabre\DAV\PropPatch;
8
9
10/**
11 * A node specifically for testing property-related operations
12 *
13 * @copyright Copyright (C) 2007-2015 fruux GmbH (https://fruux.com/).
14 * @author Evert Pot (http://evertpot.com/)
15 * @license http://sabre.io/license/ Modified BSD License
16 */
17class PropertiesCollection extends Collection implements IProperties {
18
19    public $failMode = false;
20
21    public $properties;
22
23    /**
24     * Creates the object
25     *
26     * @param string $name
27     * @param array $children
28     * @param array $properties
29     * @return void
30     */
31    public function __construct($name, array $children, array $properties = []) {
32
33        parent::__construct($name, $children, null);
34        $this->properties = $properties;
35
36    }
37
38    /**
39     * Updates properties on this node.
40     *
41     * This method received a PropPatch object, which contains all the
42     * information about the update.
43     *
44     * To update specific properties, call the 'handle' method on this object.
45     * Read the PropPatch documentation for more information.
46     *
47     * @param array $mutations
48     * @return bool|array
49     */
50    public function propPatch(PropPatch $proppatch) {
51
52        $proppatch->handleRemaining(function($updateProperties) {
53
54            switch($this->failMode) {
55                case 'updatepropsfalse' : return false;
56                case 'updatepropsarray' :
57                    $r = [];
58                    foreach($updateProperties as $k=>$v) $r[$k] = 402;
59                    return $r;
60                case 'updatepropsobj' :
61                    return new \STDClass();
62            }
63
64        });
65
66    }
67
68    /**
69     * Returns a list of properties for this nodes.
70     *
71     * The properties list is a list of propertynames the client requested,
72     * encoded in clark-notation {xmlnamespace}tagname
73     *
74     * If the array is empty, it means 'all properties' were requested.
75     *
76     * Note that it's fine to liberally give properties back, instead of
77     * conforming to the list of requested properties.
78     * The Server class will filter out the extra.
79     *
80     * @param array $properties
81     * @return array
82     */
83    public function getProperties($requestedProperties) {
84
85        $returnedProperties = array();
86        foreach($requestedProperties as $requestedProperty) {
87            if (isset($this->properties[$requestedProperty])) {
88                $returnedProperties[$requestedProperty] =
89                    $this->properties[$requestedProperty];
90            }
91        }
92        return $returnedProperties;
93
94    }
95
96}
97