Uri.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. declare(strict_types=1);
  3. namespace Sabre\Xml\Element;
  4. use Sabre\Xml;
  5. /**
  6. * Uri element.
  7. *
  8. * This represents a single uri. An example of how this may be encoded:
  9. *
  10. * <link>/foo/bar</link>
  11. * <d:href xmlns:d="DAV:">http://example.org/hi</d:href>
  12. *
  13. * If the uri is relative, it will be automatically expanded to an absolute
  14. * url during writing and reading, if the contextUri property is set on the
  15. * reader and/or writer.
  16. *
  17. * @copyright Copyright (C) 2009-2015 fruux GmbH (https://fruux.com/).
  18. * @author Evert Pot (http://evertpot.com/)
  19. * @license http://sabre.io/license/ Modified BSD License
  20. */
  21. class Uri implements Xml\Element
  22. {
  23. /**
  24. * Uri element value.
  25. *
  26. * @var string
  27. */
  28. protected $value;
  29. /**
  30. * Constructor.
  31. *
  32. * @param string $value
  33. */
  34. public function __construct($value)
  35. {
  36. $this->value = $value;
  37. }
  38. /**
  39. * The xmlSerialize method is called during xml writing.
  40. *
  41. * Use the $writer argument to write its own xml serialization.
  42. *
  43. * An important note: do _not_ create a parent element. Any element
  44. * implementing XmlSerializable should only ever write what's considered
  45. * its 'inner xml'.
  46. *
  47. * The parent of the current element is responsible for writing a
  48. * containing element.
  49. *
  50. * This allows serializers to be re-used for different element names.
  51. *
  52. * If you are opening new elements, you must also close them again.
  53. */
  54. public function xmlSerialize(Xml\Writer $writer)
  55. {
  56. $writer->text(
  57. \Sabre\Uri\resolve(
  58. $writer->contextUri,
  59. $this->value
  60. )
  61. );
  62. }
  63. /**
  64. * This method is called during xml parsing.
  65. *
  66. * This method is called statically, this is because in theory this method
  67. * may be used as a type of constructor, or factory method.
  68. *
  69. * Often you want to return an instance of the current class, but you are
  70. * free to return other data as well.
  71. *
  72. * Important note 2: You are responsible for advancing the reader to the
  73. * next element. Not doing anything will result in a never-ending loop.
  74. *
  75. * If you just want to skip parsing for this element altogether, you can
  76. * just call $reader->next();
  77. *
  78. * $reader->parseSubTree() will parse the entire sub-tree, and advance to
  79. * the next element.
  80. */
  81. public static function xmlDeserialize(Xml\Reader $reader)
  82. {
  83. return new self(
  84. \Sabre\Uri\resolve(
  85. (string) $reader->contextUri,
  86. $reader->readText()
  87. )
  88. );
  89. }
  90. }