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