1<?php
2
3namespace Sabre\DAV;
4
5/**
6 * SimpleCollection
7 *
8 * The SimpleCollection is used to quickly setup static directory structures.
9 * Just create the object with a proper name, and add children to use it.
10 *
11 * @copyright Copyright (C) 2007-2015 fruux GmbH (https://fruux.com/).
12 * @author Evert Pot (http://evertpot.com/)
13 * @license http://sabre.io/license/ Modified BSD License
14 */
15class SimpleCollection extends Collection {
16
17    /**
18     * List of childnodes
19     *
20     * @var INode[]
21     */
22    protected $children = [];
23
24    /**
25     * Name of this resource
26     *
27     * @var string
28     */
29    protected $name;
30
31    /**
32     * Creates this node
33     *
34     * The name of the node must be passed, child nodes can also be passed.
35     * This nodes must be instances of INode
36     *
37     * @param string $name
38     * @param INode[] $children
39     */
40    function __construct($name, array $children = []) {
41
42        $this->name = $name;
43        foreach ($children as $child) {
44
45            if (!($child instanceof INode)) throw new Exception('Only instances of Sabre\DAV\INode are allowed to be passed in the children argument');
46            $this->addChild($child);
47
48        }
49
50    }
51
52    /**
53     * Adds a new childnode to this collection
54     *
55     * @param INode $child
56     * @return void
57     */
58    function addChild(INode $child) {
59
60        $this->children[$child->getName()] = $child;
61
62    }
63
64    /**
65     * Returns the name of the collection
66     *
67     * @return string
68     */
69    function getName() {
70
71        return $this->name;
72
73    }
74
75    /**
76     * Returns a child object, by its name.
77     *
78     * This method makes use of the getChildren method to grab all the child nodes, and compares the name.
79     * Generally its wise to override this, as this can usually be optimized
80     *
81     * This method must throw Sabre\DAV\Exception\NotFound if the node does not
82     * exist.
83     *
84     * @param string $name
85     * @throws Exception\NotFound
86     * @return INode
87     */
88    function getChild($name) {
89
90        if (isset($this->children[$name])) return $this->children[$name];
91        throw new Exception\NotFound('File not found: ' . $name . ' in \'' . $this->getName() . '\'');
92
93    }
94
95    /**
96     * Returns a list of children for this collection
97     *
98     * @return INode[]
99     */
100    function getChildren() {
101
102        return array_values($this->children);
103
104    }
105
106
107}
108