/tests/Sabre/DAV/Mock/Collection.php

https://github.com/fruux/sabre-dav · PHP · 168 lines · 56 code · 31 blank · 81 comment · 5 complexity · 1b23ae87d030a353d121d067c9ee742c MD5 · raw file

  1. <?php declare (strict_types=1);
  2. namespace Sabre\DAV\Mock;
  3. use Sabre\DAV;
  4. /**
  5. * Mock Collection.
  6. *
  7. * This collection quickly allows you to create trees of nodes.
  8. * Children are specified as an array.
  9. *
  10. * Every key a filename, every array value is either:
  11. * * an array, for a sub-collection
  12. * * a string, for a file
  13. * * An instance of \Sabre\DAV\INode.
  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. */
  19. class Collection extends DAV\Collection {
  20. protected $name;
  21. protected $children;
  22. protected $parent;
  23. /**
  24. * Creates the object
  25. *
  26. * @param string $name
  27. * @param array $children
  28. * @param Collection $parent
  29. * @return void
  30. */
  31. function __construct($name, array $children = [], Collection $parent = null) {
  32. $this->name = $name;
  33. foreach ($children as $key => $value) {
  34. if (is_string($value)) {
  35. $this->children[] = new File($key, $value, $this);
  36. } elseif (is_array($value)) {
  37. $this->children[] = new self($key, $value, $this);
  38. } elseif ($value instanceof \Sabre\DAV\INode) {
  39. $this->children[] = $value;
  40. } else {
  41. throw new \InvalidArgumentException('Unknown value passed in $children');
  42. }
  43. }
  44. $this->parent = $parent;
  45. }
  46. /**
  47. * Returns the name of the node.
  48. *
  49. * This is used to generate the url.
  50. *
  51. * @return string
  52. */
  53. function getName() {
  54. return $this->name;
  55. }
  56. /**
  57. * Creates a new file in the directory
  58. *
  59. * Data will either be supplied as a stream resource, or in certain cases
  60. * as a string. Keep in mind that you may have to support either.
  61. *
  62. * After successful creation of the file, you may choose to return the ETag
  63. * of the new file here.
  64. *
  65. * The returned ETag must be surrounded by double-quotes (The quotes should
  66. * be part of the actual string).
  67. *
  68. * If you cannot accurately determine the ETag, you should not return it.
  69. * If you don't store the file exactly as-is (you're transforming it
  70. * somehow) you should also not return an ETag.
  71. *
  72. * This means that if a subsequent GET to this new file does not exactly
  73. * return the same contents of what was submitted here, you are strongly
  74. * recommended to omit the ETag.
  75. *
  76. * @param string $name Name of the file
  77. * @param resource|string $data Initial payload
  78. * @return null|string
  79. */
  80. function createFile($name, $data = '') {
  81. if (is_resource($data)) {
  82. $data = stream_get_contents($data);
  83. }
  84. $this->children[] = new File($name, $data, $this);
  85. return '"' . md5($data) . '"';
  86. }
  87. /**
  88. * Creates a new subdirectory
  89. *
  90. * @param string $name
  91. * @return void
  92. */
  93. function createDirectory($name) {
  94. $this->children[] = new self($name);
  95. }
  96. /**
  97. * Returns an array with all the child nodes
  98. *
  99. * @return \Sabre\DAV\INode[]
  100. */
  101. function getChildren() {
  102. return $this->children;
  103. }
  104. /**
  105. * Adds an already existing node to this collection.
  106. *
  107. * @param \Sabre\DAV\INode $node
  108. */
  109. function addNode(\Sabre\DAV\INode $node) {
  110. $this->children[] = $node;
  111. }
  112. /**
  113. * Removes a childnode from this node.
  114. *
  115. * @param string $name
  116. * @return void
  117. */
  118. function deleteChild($name) {
  119. foreach ($this->children as $key => $value) {
  120. if ($value->getName() == $name) {
  121. unset($this->children[$key]);
  122. return;
  123. }
  124. }
  125. }
  126. /**
  127. * Deletes this collection and all its children,.
  128. *
  129. * @return void
  130. */
  131. function delete() {
  132. foreach ($this->getChildren() as $child) {
  133. $this->deleteChild($child->getName());
  134. }
  135. $this->parent->deleteChild($this->getName());
  136. }
  137. }