ContextStackTrait.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. declare(strict_types=1);
  3. namespace Sabre\Xml;
  4. /**
  5. * Context Stack.
  6. *
  7. * The Context maintains information about a document during either reading or
  8. * writing.
  9. *
  10. * During this process, it may be necessary to override this context
  11. * information.
  12. *
  13. * This trait allows easy access to the context, and allows the end-user to
  14. * override its settings for document fragments, and easily restore it again
  15. * later.
  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. trait ContextStackTrait
  22. {
  23. /**
  24. * This is the element map. It contains a list of XML elements (in clark
  25. * notation) as keys and PHP class names as values.
  26. *
  27. * The PHP class names must implement Sabre\Xml\Element.
  28. *
  29. * Values may also be a callable. In that case the function will be called
  30. * directly.
  31. *
  32. * @var array
  33. */
  34. public $elementMap = [];
  35. /**
  36. * A contextUri pointing to the document being parsed / written.
  37. * This uri may be used to resolve relative urls that may appear in the
  38. * document.
  39. *
  40. * The reader and writer don't use this property, but as it's an extremely
  41. * common use-case for parsing XML documents, it's added here as a
  42. * convenience.
  43. *
  44. * @var string|null
  45. */
  46. public $contextUri;
  47. /**
  48. * This is a list of namespaces that you want to give default prefixes.
  49. *
  50. * You must make sure you create this entire list before starting to write.
  51. * They should be registered on the root element.
  52. *
  53. * @var array
  54. */
  55. public $namespaceMap = [];
  56. /**
  57. * This is a list of custom serializers for specific classes.
  58. *
  59. * The writer may use this if you attempt to serialize an object with a
  60. * class that does not implement XmlSerializable.
  61. *
  62. * Instead it will look at this classmap to see if there is a custom
  63. * serializer here. This is useful if you don't want your value objects
  64. * to be responsible for serializing themselves.
  65. *
  66. * The keys in this classmap need to be fully qualified PHP class names,
  67. * the values must be callbacks. The callbacks take two arguments. The
  68. * writer class, and the value that must be written.
  69. *
  70. * function (Writer $writer, object $value)
  71. *
  72. * @var array
  73. */
  74. public $classMap = [];
  75. /**
  76. * Backups of previous contexts.
  77. *
  78. * @var array
  79. */
  80. protected $contextStack = [];
  81. /**
  82. * Create a new "context".
  83. *
  84. * This allows you to safely modify the elementMap, contextUri or
  85. * namespaceMap. After you're done, you can restore the old data again
  86. * with popContext.
  87. */
  88. public function pushContext()
  89. {
  90. $this->contextStack[] = [
  91. $this->elementMap,
  92. $this->contextUri,
  93. $this->namespaceMap,
  94. $this->classMap,
  95. ];
  96. }
  97. /**
  98. * Restore the previous "context".
  99. */
  100. public function popContext()
  101. {
  102. list(
  103. $this->elementMap,
  104. $this->contextUri,
  105. $this->namespaceMap,
  106. $this->classMap
  107. ) = array_pop($this->contextStack);
  108. }
  109. }