PageRenderTime 28ms CodeModel.GetById 19ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/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
  2use 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/**
 17 * Load Response parent
 18 */
 19//require_once('PBAPI/Response.php');
 20/**
 21 * base class for SimpleXML
 22 */
 23require_once dirname(__FILE__) . '/../Response.php';
 24require_once dirname(__FILE__) . '/../Response/simplexml.php';
 25
 26/**
 27 * Response XML parser using SimpleXML
 28 *
 29 * @package PBAPI
 30 * @subpackage Response
 31 */
 32class PBAPI_Response_simplexmlarray extends PBAPI_Response_simplexml
 33{
 34
 35    /**
 36     * Do XML parse with simplexml
 37     *
 38     * @param string $response_string string to parse
 39     * @param bool $onlycontent only return content 'node'
 40     * @return array associative array of response data
 41     */
 42    public function parse($string, $onlycontent = false)
 43    {
 44        $obj = parent :: parse($string, $onlycontent);
 45        $result = self :: xmlToArray($obj);
 46        
 47        return $result;
 48    }
 49
 50    /**
 51     * Use SimpleXML Element to turn into an array
 52     *
 53     * @param SimpleXMLElement $node
 54     * @return array
 55     */
 56    public static function xmlToArray(SimpleXMLElement $node)
 57    {
 58        //eval attributes
 59        $attribs = false;
 60        if ($nodeAttrs = $node->attributes())
 61        {
 62            foreach ($nodeAttrs as $attrName => $attrValue)
 63            {
 64                $attrName = (string) $attrName;
 65                $attrValue = (string) $attrValue;
 66                $attribs[$attrName] = $attrValue;
 67            }
 68        }
 69        
 70        $results = array();
 71        if ($children = $node->children())
 72        {
 73            //node has children, do stuff.
 74            if ($attribs)
 75                $results['_attribs'] = $attribs;
 76            
 77     //prescan children to determine count
 78            $childnums = array();
 79            foreach ($children as $elementName => $childnode)
 80            {
 81                @$childnums[(string) $elementName] ++;
 82            }
 83            
 84            //build arrays
 85            foreach ($children as $elementName => $childnode)
 86            {
 87                $elementName = (string) $elementName;
 88                if ($childnums[$elementName] > 1)
 89                {
 90                    $results[$elementName][] = self :: xmlToArray($childnode);
 91                }
 92                else
 93                {
 94                    $results[$elementName] = self :: xmlToArray($childnode);
 95                }
 96            }
 97        
 98        }
 99        else
100        {
101            //node doesnt have children (leaf)
102            if ($attribs)
103            {
104                $results['_attribs'] = $attribs;
105                $results['content'] = (string) $node;
106            }
107            else
108            {
109                $results = (string) $node;
110            }
111        }
112        
113        return $results;
114    }
115
116}