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     * Backups of previous contexts.
62     *
63     * @var array
64     */
65    protected $contextStack = [];
66
67    /**
68     * Create a new "context".
69     *
70     * This allows you to safely modify the elementMap, contextUri or
71     * namespaceMap. After you're done, you can restore the old data again
72     * with popContext.
73     *
74     * @return null
75     */
76    function pushContext() {
77
78        $this->contextStack[] = [
79            $this->elementMap,
80            $this->contextUri,
81            $this->namespaceMap
82        ];
83
84    }
85
86    /**
87     * Restore the previous "context".
88     *
89     * @return null
90     */
91    function popContext() {
92
93        list(
94            $this->elementMap,
95            $this->contextUri,
96            $this->namespaceMap
97        ) = array_pop($this->contextStack);
98
99    }
100
101}
102