SimpleCollection.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. * SimpleCollection
  4. *
  5. * The SimpleCollection is used to quickly setup static directory structures.
  6. * Just create the object with a proper name, and add children to use it.
  7. *
  8. * @package Sabre
  9. * @subpackage DAV
  10. * @copyright Copyright (C) 2007-2012 Rooftop Solutions. All rights reserved.
  11. * @author Evert Pot (http://www.rooftopsolutions.nl/)
  12. * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
  13. */
  14. class Sabre_DAV_SimpleCollection extends Sabre_DAV_Collection {
  15. /**
  16. * List of childnodes
  17. *
  18. * @var array
  19. */
  20. protected $children = array();
  21. /**
  22. * Name of this resource
  23. *
  24. * @var string
  25. */
  26. protected $name;
  27. /**
  28. * Creates this node
  29. *
  30. * The name of the node must be passed, child nodes can also be bassed.
  31. * This nodes must be instances of Sabre_DAV_INode
  32. *
  33. * @param string $name
  34. * @param array $children
  35. */
  36. public function __construct($name,array $children = array()) {
  37. $this->name = $name;
  38. foreach($children as $child) {
  39. if (!($child instanceof Sabre_DAV_INode)) throw new Sabre_DAV_Exception('Only instances of Sabre_DAV_INode are allowed to be passed in the children argument');
  40. $this->addChild($child);
  41. }
  42. }
  43. /**
  44. * Adds a new childnode to this collection
  45. *
  46. * @param Sabre_DAV_INode $child
  47. * @return void
  48. */
  49. public function addChild(Sabre_DAV_INode $child) {
  50. $this->children[$child->getName()] = $child;
  51. }
  52. /**
  53. * Returns the name of the collection
  54. *
  55. * @return string
  56. */
  57. public function getName() {
  58. return $this->name;
  59. }
  60. /**
  61. * Returns a child object, by its name.
  62. *
  63. * This method makes use of the getChildren method to grab all the child nodes, and compares the name.
  64. * Generally its wise to override this, as this can usually be optimized
  65. *
  66. * @param string $name
  67. * @throws Sabre_DAV_Exception_NotFound
  68. * @return Sabre_DAV_INode
  69. */
  70. public function getChild($name) {
  71. if (isset($this->children[$name])) return $this->children[$name];
  72. throw new Sabre_DAV_Exception_NotFound('File not found: ' . $name . ' in \'' . $this->getName() . '\'');
  73. }
  74. /**
  75. * Returns a list of children for this collection
  76. *
  77. * @return array
  78. */
  79. public function getChildren() {
  80. return array_values($this->children);
  81. }
  82. }