JSON.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. /**
  3. * Part of XML_XRD
  4. *
  5. * PHP version 5
  6. *
  7. * @category XML
  8. * @package XML_XRD
  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
  12. */
  13. /**
  14. * Loads XRD data from a JSON file
  15. *
  16. * @category XML
  17. * @package XML_XRD
  18. * @author Christian Weiske <cweiske@php.net>
  19. * @license http://www.gnu.org/copyleft/lesser.html LGPL
  20. * @version Release: @package_version@
  21. * @link http://pear.php.net/package/XML_XRD
  22. */
  23. class XML_XRD_Loader_JSON
  24. {
  25. /**
  26. * Data storage the XML data get loaded into
  27. *
  28. * @var XML_XRD
  29. */
  30. protected $xrd;
  31. /**
  32. * Init object with xrd object
  33. *
  34. * @param XML_XRD $xrd Data storage the JSON data get loaded into
  35. */
  36. public function __construct(XML_XRD $xrd)
  37. {
  38. $this->xrd = $xrd;
  39. }
  40. /**
  41. * Loads the contents of the given file
  42. *
  43. * @param string $file Path to an JRD file
  44. *
  45. * @return void
  46. *
  47. * @throws XML_XRD_Loader_Exception When the JSON is invalid or cannot be
  48. * loaded
  49. */
  50. public function loadFile($file)
  51. {
  52. $json = file_get_contents($file);
  53. if ($json === false) {
  54. throw new XML_XRD_Loader_Exception(
  55. 'Error loading JRD file: ' . $file,
  56. XML_XRD_Loader_Exception::LOAD
  57. );
  58. }
  59. return $this->loadString($json);
  60. }
  61. /**
  62. * Loads the contents of the given string
  63. *
  64. * @param string $json JSON string
  65. *
  66. * @return void
  67. *
  68. * @throws XML_XRD_Loader_Exception When the JSON is invalid or cannot be
  69. * loaded
  70. */
  71. public function loadString($json)
  72. {
  73. if ($json == '') {
  74. throw new XML_XRD_Loader_Exception(
  75. 'Error loading JRD: string empty',
  76. XML_XRD_Loader_Exception::LOAD
  77. );
  78. }
  79. $obj = json_decode($json);
  80. if ($obj !== null) {
  81. return $this->load($obj);
  82. }
  83. $constants = get_defined_constants(true);
  84. $json_errors = array();
  85. foreach ($constants['json'] as $name => $value) {
  86. if (!strncmp($name, 'JSON_ERROR_', 11)) {
  87. $json_errors[$value] = $name;
  88. }
  89. }
  90. throw new XML_XRD_Loader_Exception(
  91. 'Error loading JRD: ' . $json_errors[json_last_error()],
  92. XML_XRD_Loader_Exception::LOAD
  93. );
  94. }
  95. /**
  96. * Loads the JSON object into the classes' data structures
  97. *
  98. * @param object $j JSON object containing the whole JSON document
  99. *
  100. * @return void
  101. */
  102. public function load(stdClass $j)
  103. {
  104. if (isset($j->subject)) {
  105. $this->xrd->subject = (string)$j->subject;
  106. }
  107. if (isset($j->aliases)) {
  108. foreach ($j->aliases as $jAlias) {
  109. $this->xrd->aliases[] = (string)$jAlias;
  110. }
  111. }
  112. if (isset($j->links)) {
  113. foreach ($j->links as $jLink) {
  114. $this->xrd->links[] = $this->loadLink($jLink);
  115. }
  116. }
  117. $this->loadProperties($this->xrd, $j);
  118. if (isset($j->expires)) {
  119. $this->xrd->expires = strtotime($j->expires);
  120. }
  121. }
  122. /**
  123. * Loads the Property elements from XML
  124. *
  125. * @param object $store Data store where the properties get stored
  126. * @param object $j JSON element with "properties" variable
  127. *
  128. * @return boolean True when all went well
  129. */
  130. protected function loadProperties(
  131. XML_XRD_PropertyAccess $store, stdClass $j
  132. ) {
  133. if (!isset($j->properties)) {
  134. return true;
  135. }
  136. foreach ($j->properties as $type => $jProp) {
  137. $store->properties[] = new XML_XRD_Element_Property(
  138. $type, (string)$jProp
  139. );
  140. }
  141. return true;
  142. }
  143. /**
  144. * Create a link element object from XML element
  145. *
  146. * @param object $j JSON link object
  147. *
  148. * @return XML_XRD_Element_Link Created link object
  149. */
  150. protected function loadLink(stdClass $j)
  151. {
  152. $link = new XML_XRD_Element_Link();
  153. foreach (array('rel', 'type', 'href', 'template') as $var) {
  154. if (isset($j->$var)) {
  155. $link->$var = (string)$j->$var;
  156. }
  157. }
  158. if (isset($j->titles)) {
  159. foreach ($j->titles as $lang => $jTitle) {
  160. if (!isset($link->titles[$lang])) {
  161. $link->titles[$lang] = (string)$jTitle;
  162. }
  163. }
  164. }
  165. $this->loadProperties($link, $j);
  166. return $link;
  167. }
  168. }
  169. ?>