Href.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. declare(strict_types=1);
  3. namespace Sabre\DAV\Xml\Property;
  4. use Sabre\DAV\Browser\HtmlOutput;
  5. use Sabre\DAV\Browser\HtmlOutputHelper;
  6. use Sabre\Uri;
  7. use Sabre\Xml\Element;
  8. use Sabre\Xml\Reader;
  9. use Sabre\Xml\Writer;
  10. /**
  11. * Href property.
  12. *
  13. * This class represents any WebDAV property that contains a {DAV:}href
  14. * element, and there are many.
  15. *
  16. * It can support either 1 or more hrefs. If while unserializing no valid
  17. * {DAV:}href elements were found, this property will unserialize itself as
  18. * null.
  19. *
  20. * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
  21. * @author Evert Pot (http://www.rooftopsolutions.nl/)
  22. * @license http://sabre.io/license/ Modified BSD License
  23. */
  24. class Href implements Element, HtmlOutput
  25. {
  26. /**
  27. * List of uris.
  28. *
  29. * @var array
  30. */
  31. protected $hrefs;
  32. /**
  33. * Automatically prefix the url with the server base directory.
  34. * Note: use of this property in code was removed in PR:
  35. * https://github.com/sabre-io/dav/pull/801
  36. * But the property is left here because old data may still exist
  37. * that has this property saved.
  38. * See discussion in issue:
  39. * https://github.com/sabre-io/Baikal/issues/1154.
  40. *
  41. * @var bool
  42. */
  43. protected $autoPrefix = true;
  44. /**
  45. * Constructor.
  46. *
  47. * You must either pass a string for a single href, or an array of hrefs.
  48. *
  49. * @param string|string[] $hrefs
  50. */
  51. public function __construct($hrefs)
  52. {
  53. if (is_string($hrefs)) {
  54. $hrefs = [$hrefs];
  55. }
  56. $this->hrefs = $hrefs;
  57. }
  58. /**
  59. * Returns the first Href.
  60. *
  61. * @return string|null
  62. */
  63. public function getHref()
  64. {
  65. return $this->hrefs[0] ?? null;
  66. }
  67. /**
  68. * Returns the hrefs as an array.
  69. *
  70. * @return array
  71. */
  72. public function getHrefs()
  73. {
  74. return $this->hrefs;
  75. }
  76. /**
  77. * The xmlSerialize method is called during xml writing.
  78. *
  79. * Use the $writer argument to write its own xml serialization.
  80. *
  81. * An important note: do _not_ create a parent element. Any element
  82. * implementing XmlSerializable should only ever write what's considered
  83. * its 'inner xml'.
  84. *
  85. * The parent of the current element is responsible for writing a
  86. * containing element.
  87. *
  88. * This allows serializers to be re-used for different element names.
  89. *
  90. * If you are opening new elements, you must also close them again.
  91. */
  92. public function xmlSerialize(Writer $writer)
  93. {
  94. foreach ($this->getHrefs() as $href) {
  95. $href = Uri\resolve($writer->contextUri, $href);
  96. $writer->writeElement('{DAV:}href', $href);
  97. }
  98. }
  99. /**
  100. * Generate html representation for this value.
  101. *
  102. * The html output is 100% trusted, and no effort is being made to sanitize
  103. * it. It's up to the implementor to sanitize user provided values.
  104. *
  105. * The output must be in UTF-8.
  106. *
  107. * The baseUri parameter is a url to the root of the application, and can
  108. * be used to construct local links.
  109. *
  110. * @return string
  111. */
  112. public function toHtml(HtmlOutputHelper $html)
  113. {
  114. $links = [];
  115. foreach ($this->getHrefs() as $href) {
  116. $links[] = $html->link($href);
  117. }
  118. return implode('<br />', $links);
  119. }
  120. /**
  121. * The deserialize method is called during xml parsing.
  122. *
  123. * This method is called statically, this is because in theory this method
  124. * may be used as a type of constructor, or factory method.
  125. *
  126. * Often you want to return an instance of the current class, but you are
  127. * free to return other data as well.
  128. *
  129. * You are responsible for advancing the reader to the next element. Not
  130. * doing anything will result in a never-ending loop.
  131. *
  132. * If you just want to skip parsing for this element altogether, you can
  133. * just call $reader->next();
  134. *
  135. * $reader->parseInnerTree() will parse the entire sub-tree, and advance to
  136. * the next element.
  137. *
  138. * @return mixed
  139. */
  140. public static function xmlDeserialize(Reader $reader)
  141. {
  142. $hrefs = [];
  143. foreach ((array) $reader->parseInnerTree() as $elem) {
  144. if ('{DAV:}href' !== $elem['name']) {
  145. continue;
  146. }
  147. $hrefs[] = $elem['value'];
  148. }
  149. if ($hrefs) {
  150. return new self($hrefs);
  151. }
  152. }
  153. }