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
15 * Loads XRD data from a JSON file
19 * @author Christian Weiske <cweiske@php.net>
20 * @license http://www.gnu.org/copyleft/lesser.html LGPL
21 * @version Release: @package_version@
22 * @link http://pear.php.net/package/XML_XRD
24 class XML_XRD_Loader_JSON
27 * Data storage the XML data get loaded into
36 * Init object with xrd object
38 * @param XML_XRD $xrd Data storage the JSON data get loaded into
40 public function __construct(XML_XRD $xrd)
46 * Loads the contents of the given file
48 * @param string $file Path to an JRD file
52 * @throws XML_XRD_Loader_Exception When the JSON is invalid or cannot be
55 public function loadFile($file)
57 $json = file_get_contents($file);
58 if ($json === false) {
59 throw new XML_XRD_Loader_Exception(
60 'Error loading JRD file: ' . $file,
61 XML_XRD_Loader_Exception::LOAD
64 return $this->loadString($json);
68 * Loads the contents of the given string
70 * @param string $json JSON string
74 * @throws XML_XRD_Loader_Exception When the JSON is invalid or cannot be
77 public function loadString($json)
80 throw new XML_XRD_Loader_Exception(
81 'Error loading JRD: string empty',
82 XML_XRD_Loader_Exception::LOAD
86 $obj = json_decode($json);
88 return $this->load($obj);
91 $constants = get_defined_constants(true);
92 $json_errors = array();
93 foreach ($constants['json'] as $name => $value) {
94 if (!strncmp($name, 'JSON_ERROR_', 11)) {
95 $json_errors[$value] = $name;
98 throw new XML_XRD_Loader_Exception(
99 'Error loading JRD: ' . $json_errors[json_last_error()],
100 XML_XRD_Loader_Exception::LOAD
105 * Loads the JSON object into the classes' data structures
107 * @param object $j JSON object containing the whole JSON document
111 public function load(stdClass $j)
113 if (isset($j->subject)) {
114 $this->xrd->subject = (string)$j->subject;
116 if (isset($j->aliases)) {
117 foreach ($j->aliases as $jAlias) {
118 $this->xrd->aliases[] = (string)$jAlias;
122 if (isset($j->links)) {
123 foreach ($j->links as $jLink) {
124 $this->xrd->links[] = $this->loadLink($jLink);
128 $this->loadProperties($this->xrd, $j);
130 if (isset($j->expires)) {
131 $this->xrd->expires = strtotime($j->expires);
136 * Loads the Property elements from XML
138 * @param object $store Data store where the properties get stored
139 * @param object $j JSON element with "properties" variable
141 * @return boolean True when all went well
143 protected function loadProperties(
144 XML_XRD_PropertyAccess $store, stdClass $j
146 if (!isset($j->properties)) {
150 foreach ($j->properties as $type => $jProp) {
151 $store->properties[] = new XML_XRD_Element_Property(
152 $type, (string)$jProp
160 * Create a link element object from XML element
162 * @param object $j JSON link object
164 * @return XML_XRD_Element_Link Created link object
166 protected function loadLink(stdClass $j)
168 $link = new XML_XRD_Element_Link();
169 foreach (array('rel', 'type', 'href', 'template') as $var) {
170 if (isset($j->$var)) {
171 $link->$var = (string)$j->$var;
175 if (isset($j->titles)) {
176 foreach ($j->titles as $lang => $jTitle) {
177 if (!isset($link->titles[$lang])) {
178 $link->titles[$lang] = (string)$jTitle;
182 $this->loadProperties($link, $j);