1<?php
2
3namespace SabreForRainLoop\DAV\Property;
4
5use SabreForRainLoop\DAV;
6
7/**
8 * Href property
9 *
10 * The href property represents a url within a {DAV:}href element.
11 * This is used by many WebDAV extensions, but not really within the WebDAV core spec
12 *
13 * @copyright Copyright (C) 2007-2013 fruux GmbH (https://fruux.com/).
14 * @author Evert Pot (http://evertpot.com/)
15 * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
16 */
17class Href extends DAV\Property implements IHref {
18
19    /**
20     * href
21     *
22     * @var string
23     */
24    private $href;
25
26    /**
27     * Automatically prefix the url with the server base directory
28     *
29     * @var bool
30     */
31    private $autoPrefix = true;
32
33    /**
34     * __construct
35     *
36     * @param string $href
37     * @param bool $autoPrefix
38     */
39    public function __construct($href, $autoPrefix = true) {
40
41        $this->href = $href;
42        $this->autoPrefix = $autoPrefix;
43
44    }
45
46    /**
47     * Returns the uri
48     *
49     * @return string
50     */
51    public function getHref() {
52
53        return $this->href;
54
55    }
56
57    /**
58     * Serializes this property.
59     *
60     * It will additionally prepend the href property with the server's base uri.
61     *
62     * @param DAV\Server $server
63     * @param \DOMElement $dom
64     * @return void
65     */
66    public function serialize(DAV\Server $server, \DOMElement $dom) {
67
68        $prefix = $server->xmlNamespaces['DAV:'];
69        $elem = $dom->ownerDocument->createElement($prefix . ':href');
70
71        if ($this->autoPrefix) {
72            $value = $server->getBaseUri() . DAV\URLUtil::encodePath($this->href);
73        } else {
74            $value = $this->href;
75        }
76        $elem->appendChild($dom->ownerDocument->createTextNode($value));
77
78        $dom->appendChild($elem);
79
80    }
81
82    /**
83     * Unserializes this property from a DOM Element
84     *
85     * This method returns an instance of this class.
86     * It will only decode {DAV:}href values. For non-compatible elements null will be returned.
87     *
88     * @param \DOMElement $dom
89     * @return DAV\Property\Href
90     */
91    static function unserialize(\DOMElement $dom) {
92
93        if ($dom->firstChild && DAV\XMLUtil::toClarkNotation($dom->firstChild)==='{DAV:}href') {
94            return new self($dom->firstChild->textContent,false);
95        }
96
97    }
98
99}
100