Base.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. declare(strict_types=1);
  3. namespace Sabre\Xml\Element;
  4. use Sabre\Xml;
  5. /**
  6. * The Base XML element is the standard parser & generator that's used by the
  7. * XML reader and writer.
  8. *
  9. * It spits out a simple PHP array structure during deserialization, that can
  10. * also be directly injected back into Writer::write.
  11. *
  12. * @copyright Copyright (C) 2009-2015 fruux GmbH (https://fruux.com/).
  13. * @author Evert Pot (http://evertpot.com/)
  14. * @license http://sabre.io/license/ Modified BSD License
  15. */
  16. class Base implements Xml\Element
  17. {
  18. /**
  19. * PHP value to serialize.
  20. */
  21. protected $value;
  22. /**
  23. * Constructor.
  24. */
  25. public function __construct($value = null)
  26. {
  27. $this->value = $value;
  28. }
  29. /**
  30. * The xmlSerialize method is called during xml writing.
  31. *
  32. * Use the $writer argument to write its own xml serialization.
  33. *
  34. * An important note: do _not_ create a parent element. Any element
  35. * implementing XmlSerializable should only ever write what's considered
  36. * its 'inner xml'.
  37. *
  38. * The parent of the current element is responsible for writing a
  39. * containing element.
  40. *
  41. * This allows serializers to be re-used for different element names.
  42. *
  43. * If you are opening new elements, you must also close them again.
  44. */
  45. public function xmlSerialize(Xml\Writer $writer)
  46. {
  47. $writer->write($this->value);
  48. }
  49. /**
  50. * The deserialize method is called during xml parsing.
  51. *
  52. * This method is called statically, this is because in theory this method
  53. * may be used as a type of constructor, or factory method.
  54. *
  55. * Often you want to return an instance of the current class, but you are
  56. * free to return other data as well.
  57. *
  58. * Important note 2: You are responsible for advancing the reader to the
  59. * next element. Not doing anything will result in a never-ending loop.
  60. *
  61. * If you just want to skip parsing for this element altogether, you can
  62. * just call $reader->next();
  63. *
  64. * $reader->parseInnerTree() will parse the entire sub-tree, and advance to
  65. * the next element.
  66. */
  67. public static function xmlDeserialize(Xml\Reader $reader)
  68. {
  69. $subTree = $reader->parseInnerTree();
  70. return $subTree;
  71. }
  72. }