PropFindAll.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. declare(strict_types=1);
  3. namespace Sabre\DAV\Browser;
  4. use Sabre\DAV\PropFind;
  5. /**
  6. * This class is used by the browser plugin to trick the system in returning
  7. * every defined property.
  8. *
  9. * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
  10. * @author Evert Pot (http://evertpot.com/)
  11. * @license http://sabre.io/license/ Modified BSD License
  12. */
  13. class PropFindAll extends PropFind
  14. {
  15. /**
  16. * Creates the PROPFIND object.
  17. *
  18. * @param string $path
  19. */
  20. public function __construct($path)
  21. {
  22. parent::__construct($path, []);
  23. }
  24. /**
  25. * Handles a specific property.
  26. *
  27. * This method checks whether the specified property was requested in this
  28. * PROPFIND request, and if so, it will call the callback and use the
  29. * return value for it's value.
  30. *
  31. * Example:
  32. *
  33. * $propFind->handle('{DAV:}displayname', function() {
  34. * return 'hello';
  35. * });
  36. *
  37. * Note that handle will only work the first time. If null is returned, the
  38. * value is ignored.
  39. *
  40. * It's also possible to not pass a callback, but immediately pass a value
  41. *
  42. * @param string $propertyName
  43. * @param mixed $valueOrCallBack
  44. */
  45. public function handle($propertyName, $valueOrCallBack)
  46. {
  47. if (is_callable($valueOrCallBack)) {
  48. $value = $valueOrCallBack();
  49. } else {
  50. $value = $valueOrCallBack;
  51. }
  52. if (!is_null($value)) {
  53. $this->result[$propertyName] = [200, $value];
  54. }
  55. }
  56. /**
  57. * Sets the value of the property.
  58. *
  59. * If status is not supplied, the status will default to 200 for non-null
  60. * properties, and 404 for null properties.
  61. *
  62. * @param string $propertyName
  63. * @param mixed $value
  64. * @param int $status
  65. */
  66. public function set($propertyName, $value, $status = null)
  67. {
  68. if (is_null($status)) {
  69. $status = is_null($value) ? 404 : 200;
  70. }
  71. $this->result[$propertyName] = [$status, $value];
  72. }
  73. /**
  74. * Returns the current value for a property.
  75. *
  76. * @param string $propertyName
  77. *
  78. * @return mixed
  79. */
  80. public function get($propertyName)
  81. {
  82. return isset($this->result[$propertyName]) ? $this->result[$propertyName][1] : null;
  83. }
  84. /**
  85. * Returns the current status code for a property name.
  86. *
  87. * If the property does not appear in the list of requested properties,
  88. * null will be returned.
  89. *
  90. * @param string $propertyName
  91. *
  92. * @return int|null
  93. */
  94. public function getStatus($propertyName)
  95. {
  96. return isset($this->result[$propertyName]) ? $this->result[$propertyName][0] : 404;
  97. }
  98. /**
  99. * Returns all propertynames that have a 404 status, and thus don't have a
  100. * value yet.
  101. *
  102. * @return array
  103. */
  104. public function get404Properties()
  105. {
  106. $result = [];
  107. foreach ($this->result as $propertyName => $stuff) {
  108. if (404 === $stuff[0]) {
  109. $result[] = $propertyName;
  110. }
  111. }
  112. // If there's nothing in this list, we're adding one fictional item.
  113. if (!$result) {
  114. $result[] = '{http://sabredav.org/ns}idk';
  115. }
  116. return $result;
  117. }
  118. }