KeyValue.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. declare(strict_types=1);
  3. namespace Sabre\Xml\Element;
  4. use Sabre\Xml;
  5. use Sabre\Xml\Deserializer;
  6. /**
  7. * 'KeyValue' parses out all child elements from a single node, and outputs a
  8. * key=>value struct.
  9. *
  10. * Attributes will be removed, and duplicate child elements are discarded.
  11. * Complex values within the elements will be parsed by the 'standard' parser.
  12. *
  13. * For example, KeyValue will parse:
  14. *
  15. * <?xml version="1.0"?>
  16. * <s:root xmlns:s="http://sabredav.org/ns">
  17. * <s:elem1>value1</s:elem1>
  18. * <s:elem2>value2</s:elem2>
  19. * <s:elem3 />
  20. * </s:root>
  21. *
  22. * Into:
  23. *
  24. * [
  25. * "{http://sabredav.org/ns}elem1" => "value1",
  26. * "{http://sabredav.org/ns}elem2" => "value2",
  27. * "{http://sabredav.org/ns}elem3" => null,
  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 KeyValue 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. $writer->write($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->parseInnerTree() 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\keyValue($reader);
  90. }
  91. }