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