cvs: pecl /sdo/DAS Json.php

From: Date: Tue, 17 Oct 2006 13:25:17 +0000
Subject: cvs: pecl /sdo/DAS Json.php
Groups: php.pecl.cvs 
Request: Send a blank email to pecl-cvs+get-6495@lists.php.net to get a copy of this message
slaws Tue Oct 17 13:25:17 2006 UTC Modified files: /pecl/sdo/DAS Json.php Log: Changes to reflect merge of json-rpc protocol into SCA code

http://cvs.php.net/viewvc.cgi/pecl/sdo/DAS/Json.php?r1=1.1&r2=1.2&diff_format=u Index: pecl/sdo/DAS/Json.php diff -u pecl/sdo/DAS/Json.php:1.1 pecl/sdo/DAS/Json.php:1.2 --- pecl/sdo/DAS/Json.php:1.1 Mon Oct 2 13:05:15 2006 +++ pecl/sdo/DAS/Json.php Tue Oct 17 13:25:17 2006 @@ -1,7 +1,7 @@ <?php /* +----------------------------------------------------------------------+ -| (c) Copyright IBM Corporation 2005. | +| (c) Copyright IBM Corporation 2006. | | All Rights Reserved. | +----------------------------------------------------------------------+ | | @@ -18,7 +18,7 @@ +----------------------------------------------------------------------+ | Author: SL | +----------------------------------------------------------------------+ -$Id: Json.php,v 1.1 2006/10/02 13:05:15 slaws Exp $ +$Id: Json.php,v 1.2 2006/10/17 13:25:17 slaws Exp $ */ @@ -31,86 +31,143 @@ * */ -class SDO_DAS_Json { +if ( ! class_exists('SDO_DAS_Json', false) ) { + class SDO_DAS_Json { - private $data_factory; + //private $xml_das; + private $data_factory; - /** - * Builds a SDO_DAS_DataFactory containing a single generic open type - * This is required when converting JSON into an SDO. As JSON provides - * no schema for the data we are going to read this is the most basic - * geneic type model. - */ - public function __construct() - { - // construct a generic type model hat handles any type of - $this->data_factory = SDO_DAS_DataFactory::getDataFactory(); - $this->data_factory->addType('GenericNS', - 'GenericType', - array('open'=>true)); - } - - /** - * Take a JSON string and convert it into an SDO - * This is the most basic implementation of this function possible - * in that all it does is recurse around the PHP object that - * results from using the json_decode() function and creates - * an SDO data graph based on the GenericType defined above. - * We really should something smarter such as a two pass generation - * of the SDO - * Pass 1 - recurse through the PHP object guessing the type - * of each object and construct a type model to match - * Pass 2 - recurse through the PHP object again creating - * an SDO data graph based on the type model from Pass 1 - * and the data from the PHP oject - * or do a proper shema free implementation of data loading in SDO. - */ - public function decode($jsonString) - { - // decode json string into PHP variable representation - $jsonVar = json_decode($jsonString); + /** + * Builds a SDO_DAS_DataFactory containing a single generic open type + * This is required when converting JSON into an SDO. As JSON provides + * no schema for the data we are going to read so this is the most basic + * geneic type model. + */ + public function __construct() + { + // as some point we want to give the option to + // be able to provide a real schema for the SDO + // that is going to be generated + //$xml_das = SDO_DAS_XML::create(some schema); + + // construct a generic type model hat handles any type of + $this->data_factory = SDO_DAS_DataFactory::getDataFactory(); - $sdo = $this->data_factory->create('GenericNS', 'GenericType'); + $this->data_factory->addType('GenericNS', + 'GenericType', + array('open'=>true)); + } - $this->_copyObjectToSDO($jsonVar, $sdo); + /** + * Take a JSON string and convert it into an SDO + * This is the most basic implementation of this function possible + * in that all it does is recurse around the PHP object that + * results from using the json_decode() function and creates + * an SDO data graph based on the GenericType defined above. + * This process makes some assumptions + * 1/ a JSON object maps directly to an SDO obect + * 2/ all JSON simple types appear as strings in an SDO object + * 3/ a JSON array maps to an SDO object whose property names take the form + * arrayname# + * where arrayname is the name of the JSON property that holds the array + * # is the index of the array item + * PHP allows properties to be accessed using an array index so this + * kind of works on the way in. There will be problems going back to + * JSON though as this will come out as an object with strange parameter + * names rather than an array + * + * The next stage of complexity is to do a two pass generation + * of the SDO + * Pass 1 - recurse through the PHP object guessing the type + * of each object and construct a type model to match + * Pass 2 - recurse through the PHP object again creating + * an SDO data graph based on the type model from Pass 1 + * and the data from the PHP oject + * + * The stage of complexity after this is to allow XML schema to be + * passed in to define the shape of the SDO tree that is expected to + * be generated. This doesn't sit that easily with JSON RPC as would + * would not expect to find an XML schema. We need to do some work + * on how complex types might be described for the JSON format. + */ + public function decode($jsonString) + { + // decode json string into PHP variable representation + $json = json_decode($jsonString); + //print_r($json); - return $sdo; - } + // copy the rpc elements into an SDO object. We force any + // top level object here to be an SDO object on the assumption + // that we will not be passed fragments of JSON. I.e. $jason_string + // will always start with '{' and end with '}' + $sdo = $this->data_factory->create('GenericNS', 'GenericType'); + $this->_copyObjectToSDO($json, $sdo); + //print_r($sdo); + + return $sdo; + } - /** - * A recursive function that copies PHP objects and - * properties to an SDO data graph - */ - private function _copyObjectToSDO ( $object, $sdo ) - { - foreach ( $object as $paramName => $paramValue ) { - $type = gettype($paramValue); - - if ( $type == "object" ) { - $newSdo = $this->data_factory->create('GenericNS', 'GenericType'); - $sdo[$paramName] = $newSdo; - $this->_copyObjectToSDO($paramValue, $newSdo); + /** + * Loops round all of the properties in a PHP object arranging for them + * to be copied into an SDO + */ + private function _copyObjectToSDO($object, $sdo) + { + foreach ( $object as $param_name => $param_value ) { + $this->_copyToSDO($param_name, $param_value, $sdo); + } + } + + /** + * A recursive function that copies PHP arrays to an SDO data graph + */ + private function _copyArrayToSDO($array_name, $array, $sdo) + { + $index = 0; + foreach ( $array as $array_entry ) { + $array_index = $array_name . $index; + $this->_copyToSDO($array_index, $array_entry, $sdo); + $index = $index + 1; + } + } + + /** + * Makes the decision on how the PHP object should be copied + * and recurse as necessary + */ + private function _copyToSDO($item_name, $item, $sdo) + { + $item_type = gettype($item); + //echo "Name: " . $item_name . " Value: " . $item . " Type: ". $item_type . "\n"; + + if ( $item_type == "object" ) { + $new_sdo = $this->data_factory->create('GenericNS', 'GenericType'); + $sdo[$item_name] = $new_sdo; + $this->_copyObjectToSDO($item, $new_sdo); + } else if ( $item_type == "array" ) { + $new_sdo = $this->data_factory->create('GenericNS', 'GenericType'); + $sdo[$item_name] = $new_sdo; + $this->_copyArrayToSDO($item_name, $item, $new_sdo); } else { - $sdo[$paramName] = $paramValue; - + $sdo[$item_name] = $item; // Could do some work here to determine the type of the parameter // from string/number/boolean. PHP thinks they are all strings } - } - } + } - /** - * The encoding process is simple as we rely entirly on the - * json_encode funtion from the JSON PECL package and SDO's ability - * to pretend to be an array - */ - public function encode ( $sdo ) - { - return json_encode($sdo); - } + /** + * The encoding process is simple as we rely entirly on the + * json_encode funtion from the JSON PECL package and SDO's ability + * to pretend to be an array + */ + public function encode ( $sdo ) + { + return json_encode($sdo); + } - public function __destruct() - { + public function __destruct() + { + } } } ?>
« previous php.pecl.cvs (#6495) next »