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