1<?php
2
3namespace Sabre\Xml\Element;
4
5use Sabre\Xml;
6
7/**
8 * Uri element.
9 *
10 * This represents a single uri. An example of how this may be encoded:
11 *
12 *    <link>/foo/bar</link>
13 *    <d:href xmlns:d="DAV:">http://example.org/hi</d:href>
14 *
15 * If the uri is relative, it will be automatically expanded to an absolute
16 * url during writing and reading, if the contextUri property is set on the
17 * reader and/or writer.
18 *
19 * @copyright Copyright (C) 2009-2015 fruux GmbH (https://fruux.com/).
20 * @author Evert Pot (http://evertpot.com/)
21 * @license http://sabre.io/license/ Modified BSD License
22 */
23class Uri implements Xml\Element {
24
25    /**
26     * Uri element value.
27     *
28     * @var string
29     */
30    protected $value;
31
32    /**
33     * Constructor
34     *
35     * @param string $value
36     */
37    function __construct($value)
38    {
39        $this->value = $value;
40    }
41
42    /**
43     * The xmlSerialize metod is called during xml writing.
44     *
45     * Use the $writer argument to write its own xml serialization.
46     *
47     * An important note: do _not_ create a parent element. Any element
48     * implementing XmlSerializble should only ever write what's considered
49     * its 'inner xml'.
50     *
51     * The parent of the current element is responsible for writing a
52     * containing element.
53     *
54     * This allows serializers to be re-used for different element names.
55     *
56     * If you are opening new elements, you must also close them again.
57     *
58     * @param Writer $writer
59     * @return void
60     */
61    function xmlSerialize(Xml\Writer $writer) {
62
63        $writer->text(
64            \Sabre\Uri\resolve(
65                $writer->contextUri,
66                $this->value
67            )
68        );
69
70    }
71
72    /**
73     * This method is called during xml parsing.
74     *
75     * This method is called statically, this is because in theory this method
76     * may be used as a type of constructor, or factory method.
77     *
78     * Often you want to return an instance of the current class, but you are
79     * free to return other data as well.
80     *
81     * Important note 2: You are responsible for advancing the reader to the
82     * next element. Not doing anything will result in a never-ending loop.
83     *
84     * If you just want to skip parsing for this element altogether, you can
85     * just call $reader->next();
86     *
87     * $reader->parseSubTree() will parse the entire sub-tree, and advance to
88     * the next element.
89     *
90     * @param Xml\Reader $reader
91     * @return mixed
92     */
93    static function xmlDeserialize(Xml\Reader $reader) {
94
95        return new self(
96            \Sabre\Uri\resolve(
97                $reader->contextUri,
98                $reader->readText()
99            )
100        );
101
102    }
103
104}
105