• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • Examples
  • File List

E:/E/GEAMP/www/openbiz/openbiz/others/Zend/Json.php

00001 <?php
00027 require_once 'Zend/Json/Expr.php';
00028 
00029 
00039 class Zend_Json
00040 {
00046     const TYPE_ARRAY  = 1;
00047     const TYPE_OBJECT = 0;
00048 
00054     public static $maxRecursionDepthAllowed=25;
00055 
00059     public static $useBuiltinEncoderDecoder = false;
00060 
00072     public static function decode($encodedValue, $objectDecodeType = Zend_Json::TYPE_ARRAY)
00073     {
00074         if (function_exists('json_decode') && self::$useBuiltinEncoderDecoder !== true) {
00075             return json_decode($encodedValue, $objectDecodeType);
00076         }
00077 
00078         require_once 'Zend/Json/Decoder.php';
00079         return Zend_Json_Decoder::decode($encodedValue, $objectDecodeType);
00080     }
00081 
00102     public static function encode($valueToEncode, $cycleCheck = false, $options = array())
00103     {
00104         if (is_object($valueToEncode) && method_exists($valueToEncode, 'toJson')) {
00105             return $valueToEncode->toJson();
00106         }
00107 
00108         // Pre-encoding look for Zend_Json_Expr objects and replacing by tmp ids
00109         $javascriptExpressions = array();
00110         if(isset($options['enableJsonExprFinder'])
00111            && ($options['enableJsonExprFinder'] == true)
00112         ) {
00116             require_once "Zend/Json/Encoder.php";
00117             $valueToEncode = self::_recursiveJsonExprFinder($valueToEncode, $javascriptExpressions);
00118         }
00119 
00120         // Encoding
00121         if (function_exists('json_encode') && self::$useBuiltinEncoderDecoder !== true) {
00122             $encodedResult = json_encode($valueToEncode);
00123         } else {
00124             require_once 'Zend/Json/Encoder.php';
00125             $encodedResult = Zend_Json_Encoder::encode($valueToEncode, $cycleCheck, $options);
00126         }
00127 
00128         //only do post-proccessing to revert back the Zend_Json_Expr if any.
00129         if (count($javascriptExpressions) > 0) {
00130             $count = count($javascriptExpressions);
00131             for($i = 0; $i < $count; $i++) {
00132                 $magicKey = $javascriptExpressions[$i]['magicKey'];
00133                 $value    = $javascriptExpressions[$i]['value'];
00134 
00135                 $encodedResult = str_replace(
00136                     //instead of replacing "key:magicKey", we replace directly magicKey by value because "key" never changes.
00137                     '"' . $magicKey . '"',
00138                     $value,
00139                     $encodedResult
00140                 );
00141             }
00142         }
00143 
00144          return $encodedResult;
00145     }
00146 
00161     protected static function _recursiveJsonExprFinder(
00162         &$value, array &$javascriptExpressions, $currentKey = null
00163     ) {
00164          if ($value instanceof Zend_Json_Expr) {
00165             // TODO: Optimize with ascii keys, if performance is bad
00166             $magicKey = "____" . $currentKey . "_" . (count($javascriptExpressions));
00167             $javascriptExpressions[] = array(
00168 
00169                 //if currentKey is integer, encodeUnicodeString call is not required.
00170                 "magicKey" => (is_int($currentKey)) ? $magicKey : Zend_Json_Encoder::encodeUnicodeString($magicKey),
00171                 "value"    => $value->__toString(),
00172             );
00173             $value = $magicKey;
00174         } elseif (is_array($value)) {
00175             foreach ($value as $k => $v) {
00176                 $value[$k] = self::_recursiveJsonExprFinder($value[$k], $javascriptExpressions, $k);
00177             }
00178         } elseif (is_object($value)) {
00179             foreach ($value as $k => $v) {
00180                 $value->$k = self::_recursiveJsonExprFinder($value->$k, $javascriptExpressions, $k);
00181             }
00182         }
00183         return $value;
00184     }
00185 
00212     public static function fromXml ($xmlStringContents, $ignoreXmlAttributes=true) {
00213         // Load the XML formatted string into a Simple XML Element object.
00214         $simpleXmlElementObject = simplexml_load_string($xmlStringContents);
00215 
00216         // If it is not a valid XML content, throw an exception.
00217         if ($simpleXmlElementObject == null) {
00218             require_once 'Zend/Json/Exception.php';
00219             throw new Zend_Json_Exception('Function fromXml was called with an invalid XML formatted string.');
00220         } // End of if ($simpleXmlElementObject == null)
00221 
00222         $resultArray = null;
00223 
00224         // Call the recursive function to convert the XML into a PHP array.
00225         $resultArray = self::_processXml($simpleXmlElementObject, $ignoreXmlAttributes);
00226 
00227         // Convert the PHP array to JSON using Zend_Json encode method.
00228         // It is just that simple.
00229         $jsonStringOutput = self::encode($resultArray);
00230         return($jsonStringOutput);
00231     } // End of function fromXml.
00232 
00258     protected static function _processXml ($simpleXmlElementObject, $ignoreXmlAttributes, $recursionDepth=0) {
00259         // Keep an eye on how deeply we are involved in recursion.
00260         if ($recursionDepth > self::$maxRecursionDepthAllowed) {
00261             // XML tree is too deep. Exit now by throwing an exception.
00262             require_once 'Zend/Json/Exception.php';
00263             throw new Zend_Json_Exception(
00264                 "Function _processXml exceeded the allowed recursion depth of " .
00265                 self::$maxRecursionDepthAllowed);
00266         } // End of if ($recursionDepth > self::$maxRecursionDepthAllowed)
00267 
00268         if ($recursionDepth == 0) {
00269             // Store the original SimpleXmlElementObject sent by the caller.
00270             // We will need it at the very end when we return from here for good.
00271             $callerProvidedSimpleXmlElementObject = $simpleXmlElementObject;
00272         } // End of if ($recursionDepth == 0)
00273 
00274         if ($simpleXmlElementObject instanceof SimpleXMLElement) {
00275             // Get a copy of the simpleXmlElementObject
00276             $copyOfSimpleXmlElementObject = $simpleXmlElementObject;
00277             // Get the object variables in the SimpleXmlElement object for us to iterate.
00278             $simpleXmlElementObject = get_object_vars($simpleXmlElementObject);
00279         } // End of if (get_class($simpleXmlElementObject) == "SimpleXMLElement")
00280 
00281         // It needs to be an array of object variables.
00282         if (is_array($simpleXmlElementObject)) {
00283             // Initialize a result array.
00284             $resultArray = array();
00285             // Is the input array size 0? Then, we reached the rare CDATA text if any.
00286             if (count($simpleXmlElementObject) <= 0) {
00287                 // Let us return the lonely CDATA. It could even be
00288                 // an empty element or just filled with whitespaces.
00289                 return (trim(strval($copyOfSimpleXmlElementObject)));
00290             } // End of if (count($simpleXmlElementObject) <= 0)
00291 
00292             // Let us walk through the child elements now.
00293             foreach($simpleXmlElementObject as $key=>$value) {
00294                 // Check if we need to ignore the XML attributes.
00295                 // If yes, you can skip processing the XML attributes.
00296                 // Otherwise, add the XML attributes to the result array.
00297                 if(($ignoreXmlAttributes == true) && (is_string($key)) && ($key == "@attributes")) {
00298                     continue;
00299                 } // End of if(($ignoreXmlAttributes == true) && ($key == "@attributes"))
00300 
00301                 // Let us recursively process the current XML element we just visited.
00302                 // Increase the recursion depth by one.
00303                 $recursionDepth++;
00304                 $resultArray[$key] = self::_processXml ($value, $ignoreXmlAttributes, $recursionDepth);
00305 
00306                 // Decrease the recursion depth by one.
00307                 $recursionDepth--;
00308             } // End of foreach($simpleXmlElementObject as $key=>$value) {
00309 
00310             if ($recursionDepth == 0) {
00311                 // That is it. We are heading to the exit now.
00312                 // Set the XML root element name as the root [top-level] key of
00313                 // the associative array that we are going to return to the original
00314                 // caller of this recursive function.
00315                 $tempArray = $resultArray;
00316                 $resultArray = array();
00317                 $resultArray[$callerProvidedSimpleXmlElementObject->getName()] = $tempArray;
00318             } // End of if ($recursionDepth == 0)
00319 
00320             return($resultArray);
00321         } else {
00322             // We are now looking at either the XML attribute text or
00323             // the text between the XML tags.
00324 
00325             // In order to allow Zend_Json_Expr from xml, we check if the node
00326             // matchs the pattern that try to detect if it is a new Zend_Json_Expr
00327             // if it matches, we return a new Zend_Json_Expr instead of a text node
00328             $pattern = '/^[\s]*new Zend_Json_Expr[\s]*\([\s]*[\"\']{1}(.*)[\"\']{1}[\s]*\)[\s]*$/';
00329             $matchings = array();
00330             $match = preg_match ($pattern, $simpleXmlElementObject, $matchings);
00331             if ($match) {
00332                 return new Zend_Json_Expr($matchings[1]);
00333             } else {
00334                 return (trim(strval($simpleXmlElementObject)));
00335             }
00336 
00337         } // End of if (is_array($simpleXmlElementObject))
00338     } // End of function _processXml.
00339 }

Generated on Thu Apr 19 2012 17:01:17 for openbiz by  doxygen 1.7.2