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