/plugin/PBAPI/PBAPI/Response/simplexmlarray.php

https://bitbucket.org/chamilo/chamilo-ext-repo-photobucket-dev/ · PHP · 116 lines · 62 code · 11 blank · 43 comment · 5 complexity · 22f465e274fc1297f0a86c21fa0f4977 MD5 · raw file

  1. <?php
  2. use common\libraries\Path;
  3. /**
  4. * Photobucket API
  5. * Fluent interface for PHP5
  6. * XML to array response parser
  7. *
  8. * @author Photobucket
  9. * @package PBAPI
  10. * @subpackage Response
  11. *
  12. * @copyright Copyright Copyright (c) 2008, Photobucket, Inc.
  13. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  14. */
  15. /**
  16. * Load Response parent
  17. */
  18. //require_once('PBAPI/Response.php');
  19. /**
  20. * base class for SimpleXML
  21. */
  22. require_once dirname(__FILE__) . '/../Response.php';
  23. require_once dirname(__FILE__) . '/../Response/simplexml.php';
  24. /**
  25. * Response XML parser using SimpleXML
  26. *
  27. * @package PBAPI
  28. * @subpackage Response
  29. */
  30. class PBAPI_Response_simplexmlarray extends PBAPI_Response_simplexml
  31. {
  32. /**
  33. * Do XML parse with simplexml
  34. *
  35. * @param string $response_string string to parse
  36. * @param bool $onlycontent only return content 'node'
  37. * @return array associative array of response data
  38. */
  39. public function parse($string, $onlycontent = false)
  40. {
  41. $obj = parent :: parse($string, $onlycontent);
  42. $result = self :: xmlToArray($obj);
  43. return $result;
  44. }
  45. /**
  46. * Use SimpleXML Element to turn into an array
  47. *
  48. * @param SimpleXMLElement $node
  49. * @return array
  50. */
  51. public static function xmlToArray(SimpleXMLElement $node)
  52. {
  53. //eval attributes
  54. $attribs = false;
  55. if ($nodeAttrs = $node->attributes())
  56. {
  57. foreach ($nodeAttrs as $attrName => $attrValue)
  58. {
  59. $attrName = (string) $attrName;
  60. $attrValue = (string) $attrValue;
  61. $attribs[$attrName] = $attrValue;
  62. }
  63. }
  64. $results = array();
  65. if ($children = $node->children())
  66. {
  67. //node has children, do stuff.
  68. if ($attribs)
  69. $results['_attribs'] = $attribs;
  70. //prescan children to determine count
  71. $childnums = array();
  72. foreach ($children as $elementName => $childnode)
  73. {
  74. @$childnums[(string) $elementName] ++;
  75. }
  76. //build arrays
  77. foreach ($children as $elementName => $childnode)
  78. {
  79. $elementName = (string) $elementName;
  80. if ($childnums[$elementName] > 1)
  81. {
  82. $results[$elementName][] = self :: xmlToArray($childnode);
  83. }
  84. else
  85. {
  86. $results[$elementName] = self :: xmlToArray($childnode);
  87. }
  88. }
  89. }
  90. else
  91. {
  92. //node doesnt have children (leaf)
  93. if ($attribs)
  94. {
  95. $results['_attribs'] = $attribs;
  96. $results['content'] = (string) $node;
  97. }
  98. else
  99. {
  100. $results = (string) $node;
  101. }
  102. }
  103. return $results;
  104. }
  105. }