9 * @author Christian Weiske <cweiske@php.net>
10 * @license http://www.gnu.org/copyleft/lesser.html LGPL
11 * @link http://pear.php.net/package/XML_XRD
14 require_once 'XML/XRD/PropertyAccess.php';
15 require_once 'XML/XRD/Element/Link.php';
16 require_once 'XML/XRD/Loader.php';
17 require_once 'XML/XRD/Serializer.php';
20 * Main class used to load XRD documents from string or file.
22 * After loading the file, access to links is possible with get() and getAll(),
23 * as well as foreach-iterating over the XML_XRD object.
25 * Property access is possible with getProperties() and array access (foreach)
26 * on the XML_XRD object.
28 * Verification that the subject/aliases match the requested URL can be done with
33 * @author Christian Weiske <cweiske@php.net>
34 * @license http://www.gnu.org/copyleft/lesser.html LGPL
35 * @version Release: @package_version@
36 * @link http://pear.php.net/package/XML_XRD
38 class XML_XRD extends XML_XRD_PropertyAccess implements IteratorAggregate
41 * XRD file/string loading dispatcher
48 * XRD serializing dispatcher
50 * @var XML_XRD_Serializer
62 * Array of subject alias strings
66 public $aliases = array();
69 * Array of link objects
73 public $links = array();
76 * Unix timestamp when the document expires.
77 * NULL when no expiry date set.
84 * xml:id of the XRD document
93 * Loads the contents of the given file.
95 * Note: Only use file type auto-detection for local files.
96 * Do not use it on remote files as the file gets requested several times.
98 * @param string $file Path to an XRD file
99 * @param string $type File type: xml or json, NULL for auto-detection
103 * @throws XML_XRD_Loader_Exception When the file is invalid or cannot be
106 public function loadFile($file, $type = null)
108 if (!isset($this->loader)) {
109 $this->loader = new XML_XRD_Loader($this);
111 return $this->loader->loadFile($file, $type);
115 * Loads the contents of the given string
117 * @param string $str XRD string
118 * @param string $type File type: xml or json, NULL for auto-detection
122 * @throws XML_XRD_Loader_Exception When the string is invalid or cannot be
125 public function loadString($str, $type = null)
127 if (!isset($this->loader)) {
128 $this->loader = new XML_XRD_Loader($this);
130 return $this->loader->loadString($str, $type);
134 * Checks if the XRD document describes the given URI.
136 * This should always be used to make sure the XRD file
137 * is the correct one for e.g. the given host, and not a copycat.
139 * Checks against the subject and aliases
141 * @param string $uri An URI that the document is expected to describe
143 * @return boolean True or false
145 public function describes($uri)
147 if ($this->subject == $uri) {
150 foreach ($this->aliases as $alias) {
151 if ($alias == $uri) {
160 * Get the link with highest priority for the given relation and type.
162 * @param string $rel Relation name
163 * @param string $type MIME Type
164 * @param boolean $typeFallback When true and no link with the given type
165 * could be found, the best link without a
166 * type will be returned
168 * @return XML_XRD_Element_Link Link object or NULL if none found
170 public function get($rel, $type = null, $typeFallback = true)
172 $links = $this->getAll($rel, $type, $typeFallback);
173 if (count($links) == 0) {
182 * Get all links with the given relation and type, highest priority first.
184 * @param string $rel Relation name
185 * @param string $type MIME Type
186 * @param boolean $typeFallback When true and no link with the given type
187 * could be found, the best link without a
188 * type will be returned
190 * @return array Array of XML_XRD_Element_Link objects
192 public function getAll($rel, $type = null, $typeFallback = true)
196 foreach ($this->links as $link) {
197 if ($link->rel == $rel
198 && ($type === null || $link->type == $type
199 || $typeFallback && $link->type === null)
202 $exactType |= $typeFallback && $type !== null
203 && $link->type == $type;
207 //remove all links without type
208 $exactlinks = array();
209 foreach ($links as $link) {
210 if ($link->type !== null) {
211 $exactlinks[] = $link;
214 $links = $exactlinks;
220 * Return the iterator object to loop over the links
222 * Part of the IteratorAggregate interface
224 * @return Traversable Iterator for the links
226 public function getIterator()
228 return new ArrayIterator($this->links);
232 * Converts this XRD object to XML or JSON.
234 * @param string $type Serialization type: xml or json
236 * @return string Generated content
238 public function to($type)
240 if (!isset($this->serializer)) {
241 $this->serializer = new XML_XRD_Serializer($this);
243 return $this->serializer->to($type);
247 * Converts this XRD object to XML.
249 * @return string Generated XML
251 * @deprecated use to('xml')
253 public function toXML()
255 return $this->to('xml');