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