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) 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     * @param Collection $parent
34     * @return void
35     */
36    function __construct($name, array $children = [], Collection $parent = null) {
37
38        $this->name = $name;
39        foreach ($children as $key => $value) {
40            if (is_string($value)) {
41                $this->children[] = new File($key, $value, $this);
42            } elseif (is_array($value)) {
43                $this->children[] = new self($key, $value, $this);
44            } elseif ($value instanceof \Sabre\DAV\INode) {
45                $this->children[] = $value;
46            } else {
47                throw new \InvalidArgumentException('Unknown value passed in $children');
48            }
49        }
50        $this->parent = $parent;
51
52    }
53
54    /**
55     * Returns the name of the node.
56     *
57     * This is used to generate the url.
58     *
59     * @return string
60     */
61    function getName() {
62
63        return $this->name;
64
65    }
66
67    /**
68     * Creates a new file in the directory
69     *
70     * Data will either be supplied as a stream resource, or in certain cases
71     * as a string. Keep in mind that you may have to support either.
72     *
73     * After successful creation of the file, you may choose to return the ETag
74     * of the new file here.
75     *
76     * The returned ETag must be surrounded by double-quotes (The quotes should
77     * be part of the actual string).
78     *
79     * If you cannot accurately determine the ETag, you should not return it.
80     * If you don't store the file exactly as-is (you're transforming it
81     * somehow) you should also not return an ETag.
82     *
83     * This means that if a subsequent GET to this new file does not exactly
84     * return the same contents of what was submitted here, you are strongly
85     * recommended to omit the ETag.
86     *
87     * @param string $name Name of the file
88     * @param resource|string $data Initial payload
89     * @return null|string
90     */
91    function createFile($name, $data = null) {
92
93        if (is_resource($data)) {
94            $data = stream_get_contents($data);
95        }
96        $this->children[] = new File($name, $data, $this);
97        return '"' . md5($data) . '"';
98
99    }
100
101    /**
102     * Creates a new subdirectory
103     *
104     * @param string $name
105     * @return void
106     */
107    function createDirectory($name) {
108
109        $this->children[] = new self($name);
110
111    }
112
113    /**
114     * Returns an array with all the child nodes
115     *
116     * @return \Sabre\DAV\INode[]
117     */
118    function getChildren() {
119
120        return $this->children;
121
122    }
123
124    /**
125     * Adds an already existing node to this collection.
126     *
127     * @param \Sabre\DAV\INode $node
128     */
129    function addNode(\Sabre\DAV\INode $node) {
130
131        $this->children[] = $node;
132
133    }
134
135    /**
136     * Removes a childnode from this node.
137     *
138     * @param string $name
139     * @return void
140     */
141    function deleteChild($name) {
142
143        foreach ($this->children as $key => $value) {
144
145            if ($value->getName() == $name) {
146                unset($this->children[$key]);
147                return;
148            }
149
150        }
151
152    }
153
154    /**
155     * Deletes this collection and all its children,.
156     *
157     * @return void
158     */
159    function delete() {
160
161        foreach ($this->getChildren() as $child) {
162            $this->deleteChild($child->getName());
163        }
164        $this->parent->deleteChild($this->getName());
165
166    }
167
168}
169