1<?php
2
3namespace Sabre\DAV\Mock;
4
5use Sabre\DAV;
6
7/**
8 * Mock Collection.
9 *
10 * This collection quickly allows you to create trees of nodes.
11 * Children are specified as an array.
12 *
13 * Every key a filename, every array value is either:
14 *   * an array, for a sub-collection
15 *   * a string, for a file
16 *   * An instance of \Sabre\DAV\INode.
17 *
18 * @copyright Copyright (C) 2007-2015 fruux GmbH (https://fruux.com/).
19 * @author Evert Pot (http://evertpot.com/)
20 * @license http://sabre.io/license/ Modified BSD License
21 */
22class Collection extends DAV\Collection {
23
24    protected $name;
25    protected $children;
26    protected $parent;
27
28    /**
29     * Creates the object
30     *
31     * @param string $name
32     * @param array $children
33     * @return void
34     */
35    function __construct($name, array $children = array(), Collection $parent = null) {
36
37        $this->name = $name;
38        foreach($children as $key=>$value) {
39            if (is_string($value)) {
40                $this->children[] = new File($key, $value, $this);
41            } elseif (is_array($value)) {
42                $this->children[] = new Collection($key, $value, $this);
43            } elseif ($value instanceof \Sabre\DAV\INode) {
44                $this->children[] = $value;
45            } else {
46                throw new \InvalidArgumentException('Unknown value passed in $children');
47            }
48        }
49        $this->parent = $parent;
50
51    }
52
53    /**
54     * Returns the name of the node.
55     *
56     * This is used to generate the url.
57     *
58     * @return string
59     */
60    function getName() {
61
62        return $this->name;
63
64    }
65
66    /**
67     * Creates a new file in the directory
68     *
69     * Data will either be supplied as a stream resource, or in certain cases
70     * as a string. Keep in mind that you may have to support either.
71     *
72     * After successful creation of the file, you may choose to return the ETag
73     * of the new file here.
74     *
75     * The returned ETag must be surrounded by double-quotes (The quotes should
76     * be part of the actual string).
77     *
78     * If you cannot accurately determine the ETag, you should not return it.
79     * If you don't store the file exactly as-is (you're transforming it
80     * somehow) you should also not return an ETag.
81     *
82     * This means that if a subsequent GET to this new file does not exactly
83     * return the same contents of what was submitted here, you are strongly
84     * recommended to omit the ETag.
85     *
86     * @param string $name Name of the file
87     * @param resource|string $data Initial payload
88     * @return null|string
89     */
90    function createFile($name, $data = null) {
91
92        if (is_resource($data)) {
93            $data = stream_get_contents($data);
94        }
95        $this->children[] = new File($name, $data, $this);
96        return '"' . md5($data) . '"';
97
98    }
99
100    /**
101     * Creates a new subdirectory
102     *
103     * @param string $name
104     * @return void
105     */
106    function createDirectory($name) {
107
108        $this->children[] = new Collection($name);
109
110    }
111
112    /**
113     * Returns an array with all the child nodes
114     *
115     * @return \Sabre\DAV\INode[]
116     */
117    function getChildren() {
118
119        return $this->children;
120
121    }
122
123    /**
124     * Removes a childnode from this node.
125     *
126     * @param string $name
127     * @return void
128     */
129    function deleteChild($name) {
130
131        foreach($this->children as $key=>$value) {
132
133            if ($value->getName() == $name) {
134                unset($this->children[$key]);
135                return;
136            }
137
138        }
139
140    }
141
142    /**
143     * Deletes this collection and all its children,.
144     *
145     * @return void
146     */
147    function delete() {
148
149        foreach($this->getChildren() as $child) {
150            $this->deleteChild($child->getName());
151        }
152        $this->parent->deleteChild($this->getName());
153
154    }
155
156}
157