Collection.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. declare(strict_types=1);
  3. namespace Sabre\DAV;
  4. /**
  5. * Collection class.
  6. *
  7. * This is a helper class, that should aid in getting collections classes setup.
  8. * Most of its methods are implemented, and throw permission denied exceptions
  9. *
  10. * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
  11. * @author Evert Pot (http://evertpot.com/)
  12. * @license http://sabre.io/license/ Modified BSD License
  13. */
  14. abstract class Collection extends Node implements ICollection
  15. {
  16. /**
  17. * Returns a child object, by its name.
  18. *
  19. * This method makes use of the getChildren method to grab all the child
  20. * nodes, and compares the name.
  21. * Generally its wise to override this, as this can usually be optimized
  22. *
  23. * This method must throw Sabre\DAV\Exception\NotFound if the node does not
  24. * exist.
  25. *
  26. * @param string $name
  27. *
  28. * @throws Exception\NotFound
  29. *
  30. * @return INode
  31. */
  32. public function getChild($name)
  33. {
  34. foreach ($this->getChildren() as $child) {
  35. if ($child->getName() === $name) {
  36. return $child;
  37. }
  38. }
  39. throw new Exception\NotFound('File not found: '.$name);
  40. }
  41. /**
  42. * Checks is a child-node exists.
  43. *
  44. * It is generally a good idea to try and override this. Usually it can be optimized.
  45. *
  46. * @param string $name
  47. *
  48. * @return bool
  49. */
  50. public function childExists($name)
  51. {
  52. try {
  53. $this->getChild($name);
  54. return true;
  55. } catch (Exception\NotFound $e) {
  56. return false;
  57. }
  58. }
  59. /**
  60. * Creates a new file in the directory.
  61. *
  62. * Data will either be supplied as a stream resource, or in certain cases
  63. * as a string. Keep in mind that you may have to support either.
  64. *
  65. * After successful creation of the file, you may choose to return the ETag
  66. * of the new file here.
  67. *
  68. * The returned ETag must be surrounded by double-quotes (The quotes should
  69. * be part of the actual string).
  70. *
  71. * If you cannot accurately determine the ETag, you should not return it.
  72. * If you don't store the file exactly as-is (you're transforming it
  73. * somehow) you should also not return an ETag.
  74. *
  75. * This means that if a subsequent GET to this new file does not exactly
  76. * return the same contents of what was submitted here, you are strongly
  77. * recommended to omit the ETag.
  78. *
  79. * @param string $name Name of the file
  80. * @param resource|string $data Initial payload
  81. *
  82. * @return string|null
  83. */
  84. public function createFile($name, $data = null)
  85. {
  86. throw new Exception\Forbidden('Permission denied to create file (filename '.$name.')');
  87. }
  88. /**
  89. * Creates a new subdirectory.
  90. *
  91. * @param string $name
  92. *
  93. * @throws Exception\Forbidden
  94. */
  95. public function createDirectory($name)
  96. {
  97. throw new Exception\Forbidden('Permission denied to create directory');
  98. }
  99. }