12 * obtain it through the world-wide-web, please send an email |
12 * obtain it through the world-wide-web, please send an email |
13 * to license@zend.com so we can send you a copy immediately. |
13 * to license@zend.com so we can send you a copy immediately. |
14 * |
14 * |
15 * @category Zend |
15 * @category Zend |
16 * @package Zend_Json |
16 * @package Zend_Json |
17 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
17 * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) |
18 * @license http://framework.zend.com/license/new-bsd New BSD License |
18 * @license http://framework.zend.com/license/new-bsd New BSD License |
19 * @version $Id: Encoder.php 22452 2010-06-18 18:13:23Z ralph $ |
19 * @version $Id: Encoder.php 25059 2012-11-02 21:01:06Z rob $ |
20 */ |
20 */ |
21 |
21 |
22 /** |
22 /** |
23 * Encode PHP constructs to JSON |
23 * Encode PHP constructs to JSON |
24 * |
24 * |
25 * @category Zend |
25 * @category Zend |
26 * @package Zend_Json |
26 * @package Zend_Json |
27 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
27 * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) |
28 * @license http://framework.zend.com/license/new-bsd New BSD License |
28 * @license http://framework.zend.com/license/new-bsd New BSD License |
29 */ |
29 */ |
30 class Zend_Json_Encoder |
30 class Zend_Json_Encoder |
31 { |
31 { |
32 /** |
32 /** |
72 * @return string The encoded value |
72 * @return string The encoded value |
73 */ |
73 */ |
74 public static function encode($value, $cycleCheck = false, $options = array()) |
74 public static function encode($value, $cycleCheck = false, $options = array()) |
75 { |
75 { |
76 $encoder = new self(($cycleCheck) ? true : false, $options); |
76 $encoder = new self(($cycleCheck) ? true : false, $options); |
77 |
|
78 return $encoder->_encodeValue($value); |
77 return $encoder->_encodeValue($value); |
79 } |
78 } |
80 |
79 |
81 /** |
80 /** |
82 * Recursive driver which determines the type of value to be encoded |
81 * Recursive driver which determines the type of value to be encoded |
83 * and then dispatches to the appropriate method. $values are either |
82 * and then dispatches to the appropriate method. $values are either |
84 * - objects (returns from {@link _encodeObject()}) |
83 * - objects (returns from {@link _encodeObject()}) |
85 * - arrays (returns from {@link _encodeArray()}) |
84 * - arrays (returns from {@link _encodeArray()}) |
86 * - basic datums (e.g. numbers or strings) (returns from {@link _encodeDatum()}) |
85 * - basic datums (e.g. numbers or strings) (returns from {@link _encodeDatum()}) |
87 * |
86 * |
88 * @param $value mixed The value to be encoded |
87 * @param mixed $value The value to be encoded |
89 * @return string Encoded value |
88 * @return string Encoded value |
90 */ |
89 */ |
91 protected function _encodeValue(&$value) |
90 protected function _encodeValue(&$value) |
92 { |
91 { |
93 if (is_object($value)) { |
92 if (is_object($value)) { |
106 * |
105 * |
107 * A special property is added to the JSON object called '__className' |
106 * A special property is added to the JSON object called '__className' |
108 * that contains the name of the class of $value. This is used to decode |
107 * that contains the name of the class of $value. This is used to decode |
109 * the object on the client into a specific class. |
108 * the object on the client into a specific class. |
110 * |
109 * |
111 * @param $value object |
110 * @param object $value |
112 * @return string |
111 * @return string |
113 * @throws Zend_Json_Exception If recursive checks are enabled and the object has been serialized previously |
112 * @throws Zend_Json_Exception If recursive checks are enabled and the object has been serialized previously |
114 */ |
113 */ |
115 protected function _encodeObject(&$value) |
114 protected function _encodeObject(&$value) |
116 { |
115 { |
133 |
132 |
134 $this->_visited[] = $value; |
133 $this->_visited[] = $value; |
135 } |
134 } |
136 |
135 |
137 $props = ''; |
136 $props = ''; |
138 |
137 if (method_exists($value, 'toJson')) { |
139 if ($value instanceof Iterator) { |
138 $props =',' . preg_replace("/^\{(.*)\}$/","\\1",$value->toJson()); |
140 $propCollection = $value; |
|
141 } else { |
139 } else { |
142 $propCollection = get_object_vars($value); |
140 if ($value instanceof IteratorAggregate) { |
143 } |
141 $propCollection = $value->getIterator(); |
144 |
142 } elseif ($value instanceof Iterator) { |
145 foreach ($propCollection as $name => $propValue) { |
143 $propCollection = $value; |
146 if (isset($propValue)) { |
144 } else { |
147 $props .= ',' |
145 $propCollection = get_object_vars($value); |
148 . $this->_encodeString($name) |
146 } |
149 . ':' |
147 |
150 . $this->_encodeValue($propValue); |
148 foreach ($propCollection as $name => $propValue) { |
151 } |
149 if (isset($propValue)) { |
152 } |
150 $props .= ',' |
153 |
151 . $this->_encodeString($name) |
154 return '{"__className":"' . get_class($value) . '"' |
152 . ':' |
|
153 . $this->_encodeValue($propValue); |
|
154 } |
|
155 } |
|
156 } |
|
157 $className = get_class($value); |
|
158 return '{"__className":' . $this->_encodeString($className) |
155 . $props . '}'; |
159 . $props . '}'; |
156 } |
160 } |
157 |
161 |
158 |
162 |
159 /** |
163 /** |
180 * |
184 * |
181 * Arrays are defined as integer-indexed arrays starting at index 0, where |
185 * Arrays are defined as integer-indexed arrays starting at index 0, where |
182 * the last index is (count($array) -1); any deviation from that is |
186 * the last index is (count($array) -1); any deviation from that is |
183 * considered an associative array, and will be encoded as such. |
187 * considered an associative array, and will be encoded as such. |
184 * |
188 * |
185 * @param $array array |
189 * @param array& $array |
186 * @return string |
190 * @return string |
187 */ |
191 */ |
188 protected function _encodeArray(&$array) |
192 protected function _encodeArray(&$array) |
189 { |
193 { |
190 $tmpArray = array(); |
194 $tmpArray = array(); |
220 * JSON encode a basic data type (string, number, boolean, null) |
224 * JSON encode a basic data type (string, number, boolean, null) |
221 * |
225 * |
222 * If value type is not a string, number, boolean, or null, the string |
226 * If value type is not a string, number, boolean, or null, the string |
223 * 'null' is returned. |
227 * 'null' is returned. |
224 * |
228 * |
225 * @param $value mixed |
229 * @param mixed& $value |
226 * @return string |
230 * @return string |
227 */ |
231 */ |
228 protected function _encodeDatum(&$value) |
232 protected function _encodeDatum(&$value) |
229 { |
233 { |
230 $result = 'null'; |
234 $result = 'null'; |
268 |
272 |
269 /** |
273 /** |
270 * Encode the constants associated with the ReflectionClass |
274 * Encode the constants associated with the ReflectionClass |
271 * parameter. The encoding format is based on the class2 format |
275 * parameter. The encoding format is based on the class2 format |
272 * |
276 * |
273 * @param $cls ReflectionClass |
277 * @param ReflectionClass $cls |
274 * @return string Encoded constant block in class2 format |
278 * @return string Encoded constant block in class2 format |
275 */ |
279 */ |
276 private static function _encodeConstants(ReflectionClass $cls) |
280 private static function _encodeConstants(ReflectionClass $cls) |
277 { |
281 { |
278 $result = "constants : {"; |
282 $result = "constants : {"; |
389 * Encodes the given $className into the class2 model of encoding PHP |
393 * Encodes the given $className into the class2 model of encoding PHP |
390 * classes into JavaScript class2 classes. |
394 * classes into JavaScript class2 classes. |
391 * NOTE: Currently only public methods and variables are proxied onto |
395 * NOTE: Currently only public methods and variables are proxied onto |
392 * the client machine |
396 * the client machine |
393 * |
397 * |
394 * @param $className string The name of the class, the class must be |
398 * @param string $className The name of the class, the class must be |
395 * instantiable using a null constructor |
399 * instantiable using a null constructor |
396 * @param $package string Optional package name appended to JavaScript |
400 * @param string $package Optional package name appended to JavaScript |
397 * proxy class name |
401 * proxy class name |
398 * @return string The class2 (JavaScript) encoding of the class |
402 * @return string The class2 (JavaScript) encoding of the class |
399 * @throws Zend_Json_Exception |
403 * @throws Zend_Json_Exception |
400 */ |
404 */ |
401 public static function encodeClass($className, $package = '') |
405 public static function encodeClass($className, $package = '') |
402 { |
406 { |