Elements.php 2.6 KB

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