SimpleCollection.php 2.6 KB

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