diff options
author | rvelices <rv-github@modusoptimus.com> | 2013-05-18 03:58:36 +0000 |
---|---|---|
committer | rvelices <rv-github@modusoptimus.com> | 2013-05-18 03:58:36 +0000 |
commit | 8083ba10ecc3d6dd028142888c25830ea879ee3f (patch) | |
tree | 8a9c3dcde3f6d3e61e2ab6b87c5723ffdd6557c1 /include/ws_core.inc.php | |
parent | ac75099200ad1ca0638b16a1bddd265aa462e75a (diff) |
bug 2900: Improve web service output
git-svn-id: http://piwigo.org/svn/trunk@22729 68402e56-0260-453c-a942-63ccdbb3a9ee
Diffstat (limited to '')
-rw-r--r-- | include/ws_core.inc.php | 114 |
1 files changed, 19 insertions, 95 deletions
diff --git a/include/ws_core.inc.php b/include/ws_core.inc.php index fdddbdf06..e72c7d57a 100644 --- a/include/ws_core.inc.php +++ b/include/ws_core.inc.php @@ -40,7 +40,6 @@ define( 'WS_ERR_MISSING_PARAM', 1002 ); define( 'WS_ERR_INVALID_PARAM', 1003 ); define( 'WS_XML_ATTRIBUTES', 'attributes_xml_'); -define( 'WS_XML_CONTENT', 'content_xml_'); /** * PwgError object can be returned from any web service function implementation. @@ -98,7 +97,6 @@ class PwgNamedArray class PwgNamedStruct { /*private*/ var $_content; - /*private*/ var $_name; /*private*/ var $_xmlAttributes; /** @@ -110,9 +108,8 @@ class PwgNamedStruct * encoded as xml attributes (if null - automatically prefer xml attributes * whenever possible) */ - function PwgNamedStruct($name, $content, $xmlAttributes=null, $xmlElements=null ) + function PwgNamedStruct($content, $xmlAttributes=null, $xmlElements=null ) { - $this->_name = $name; $this->_content = $content; if ( isset($xmlAttributes) ) { @@ -182,115 +179,42 @@ class PwgResponseEncoder * removes all XML formatting from $response (named array, named structs, etc) * usually called by every response encoder, except rest xml. */ - static function flattenResponse(&$response) + static function flattenResponse(&$value) { - PwgResponseEncoder::_mergeAttributesAndContent($response); - PwgResponseEncoder::_removeNamedArray($response); - PwgResponseEncoder::_removeNamedStruct($response); - if (is_array($response)) - { // need to call 2 times (first time might add new arrays) - array_walk_recursive($response, array('PwgResponseEncoder', '_remove_named_callback') ); - array_walk_recursive($response, array('PwgResponseEncoder', '_remove_named_callback') ); - } -//print_r($response); - PwgResponseEncoder::_mergeAttributesAndContent($response); - } - - private static function _remove_named_callback(&$value, $key) - { - do - { - $changed = 0; - $changed += PwgResponseEncoder::_removeNamedArray($value); - $changed += PwgResponseEncoder::_removeNamedStruct($value); - // print_r('walk '.$key."<br>\n"); - } - while ($changed); + self::flatten($value); } - private static function _mergeAttributesAndContent(&$value) + private static function flatten(&$value) { - if ( !is_array($value) ) - return; -/* $first_key = ''; - if (count($value)) { $ak = array_keys($value); $first_key = $ak[0]; } - - print_r( '_mergeAttributesAndContent is_struct='.PwgResponseEncoder::is_struct($value) - .' count='.count($value) - .' first_key='.$first_key - ."<br>\n" - );*/ - $ret = 0; - if (PwgResponseEncoder::is_struct($value)) + if (is_object($value)) { - if ( isset($value[WS_XML_ATTRIBUTES]) ) + $class = strtolower( @get_class($value) ); + if ($class == 'pwgnamedarray') { - $value = array_merge( $value, $value[WS_XML_ATTRIBUTES] ); - unset( $value[WS_XML_ATTRIBUTES] ); - $ret=1; + $value = $value->_content; } - if ( isset($value[WS_XML_CONTENT]) ) + if ($class == 'pwgnamedstruct') { - $cont_processed = 0; - if ( count($value)==1 ) - { - $value = $value[WS_XML_CONTENT]; - $cont_processed=1; - } - else - { - if (PwgResponseEncoder::is_struct($value[WS_XML_CONTENT])) - { - $value = array_merge( $value, $value[WS_XML_CONTENT] ); - unset( $value[WS_XML_CONTENT] ); - $cont_processed=1; - } - } - $ret += $cont_processed; - if (!$cont_processed) - { - $value['_content'] = $value[WS_XML_CONTENT]; - unset( $value[WS_XML_CONTENT] ); - $ret++; - } + $value = $value->_content; } } - foreach ($value as $key=>$v) + if (!is_array($value)) + return; + + if (self::is_struct($value)) { - if ( PwgResponseEncoder::_mergeAttributesAndContent($v) ) + if ( isset($value[WS_XML_ATTRIBUTES]) ) { - $value[$key]=$v; - $ret++; + $value = array_merge( $value, $value[WS_XML_ATTRIBUTES] ); + unset( $value[WS_XML_ATTRIBUTES] ); } } - return $ret; - } - - private static function _removeNamedArray(&$value) - { - if ( strtolower( @get_class($value) ) =='pwgnamedarray') - { - $value = $value->_content; - return 1; - } - return 0; - } - private static function _removeNamedStruct(&$value) - { - if ( strtolower( @get_class($value) ) =='pwgnamedstruct') + foreach ($value as $key=>&$v) { - if ( isset($value->_content['']) ) - { - $unknown = $value->_content['']; - unset( $value->_content[''] ); - $value->_content[$value->_name] = $unknown; - } - $value = $value->_content; - return 1; + self::flatten($v); } - return 0; } } |