@@ -106,11 +106,6 @@ class PythonPickle extends AbstractAdapter
106106 const OP_BINBYTES = 'B ' ; // push bytes; counted binary string argument
107107 const OP_SHORT_BINBYTES = 'C ' ; // " " ; " " " " < 256 bytes
108108
109- /**
110- * @var bool Whether or not this is a PHP 6 binary
111- */
112- protected static $ _isPhp6 = null ;
113-
114109 /**
115110 * @var bool Whether or not the system is little-endian
116111 */
@@ -162,9 +157,6 @@ public function __construct($opts=array())
162157 if (self ::$ _isLittleEndian === null ) {
163158 self ::$ _isLittleEndian = (pack ('l ' , 1 ) === "\x01\x00\x00\x00" );
164159 }
165- if (self ::$ _isPhp6 === null ) {
166- self ::$ _isPhp6 = !version_compare (PHP_VERSION , '6.0.0 ' , '< ' );
167- }
168160
169161 $ this ->_marker = new \stdClass ();
170162 }
@@ -441,7 +433,7 @@ protected function _writeFloat($value)
441433 */
442434 protected function _writeString ($ value )
443435 {
444- if ( ($ id =$ this ->_searchMomo ($ value )) !== false ) {
436+ if ( ($ id =$ this ->_searchMemo ($ value )) !== false ) {
445437 $ this ->_writeGet ($ id );
446438 return ;
447439 }
@@ -463,24 +455,24 @@ protected function _writeString($value)
463455 $ this ->_pickle .= self ::OP_STRING . $ this ->_quoteString ($ value ) . "\r\n" ;
464456 }
465457
466- $ this ->_momorize ($ value );
458+ $ this ->_memorize ($ value );
467459 }
468460
469461 /**
470462 * Write an associative array value as dictionary
471463 *
472- * @param array $value
464+ * @param array|Traversable $value
473465 * @return void
474466 */
475- protected function _writeArrayDict (array $ value )
467+ protected function _writeArrayDict ($ value )
476468 {
477- if (($ id =$ this ->_searchMomo ($ value )) !== false ) {
478- $ this ->_writeGet ($ id );;
469+ if (($ id =$ this ->_searchMemo ($ value )) !== false ) {
470+ $ this ->_writeGet ($ id );
479471 return ;
480472 }
481473
482474 $ this ->_pickle .= self ::OP_MARK . self ::OP_DICT ;
483- $ this ->_momorize ($ value );
475+ $ this ->_memorize ($ value );
484476
485477 foreach ($ value as $ k => $ v ) {
486478 $ this ->_pickle .= $ this ->_write ($ k )
@@ -497,15 +489,15 @@ protected function _writeArrayDict(array $value)
497489 */
498490 protected function _writeArrayList (array $ value )
499491 {
500- if (($ id = $ this ->_searchMomo ($ value )) !== false ) {
492+ if (($ id = $ this ->_searchMemo ($ value )) !== false ) {
501493 $ this ->_writeGet ($ id );
502494 return ;
503495 }
504496
505497 $ this ->_pickle .= self ::OP_MARK . self ::OP_LIST ;
506- $ this ->_momorize ($ value );
498+ $ this ->_memorize ($ value );
507499
508- foreach ($ value as $ k => $ v ) {
500+ foreach ($ value as $ v ) {
509501 $ this ->_pickle .= $ this ->_write ($ v ) . self ::OP_APPEND ;
510502 }
511503 }
@@ -518,8 +510,25 @@ protected function _writeArrayList(array $value)
518510 */
519511 protected function _writeObject ($ value )
520512 {
521- // can't serialize php objects to python objects yet
522- $ this ->_writeArrayDict (get_object_vars ($ value ));
513+ // The main differences between a SplFixedArray and a normal PHP array is
514+ // that the SplFixedArray is of fixed length and allows only integers
515+ // within the range as indexes.
516+ if ($ value instanceof \SplFixedArray) {
517+ $ this ->_writeArrayList ($ value ->toArray ());
518+
519+ // Use the object method toArray if available
520+ } elseif (method_exists ($ value , 'toArray ' )) {
521+ $ this ->_writeArrayDict ($ value ->toArray ());
522+
523+ // If the object is an iterator simply iterate it
524+ // and convert it to an dictionary
525+ } elseif ($ value instanceof \Traversable) {
526+ $ this ->_writeArrayDict ($ value );
527+
528+ // other objects are simply converted by using its properties
529+ } else {
530+ $ this ->_writeArrayDict (get_object_vars ($ value ));
531+ }
523532 }
524533
525534 /**
@@ -540,20 +549,20 @@ protected function _writeStop()
540549 * @param mixed $value
541550 * @return void
542551 */
543- protected function _momorize ($ value )
552+ protected function _memorize ($ value )
544553 {
545554 $ id = count ($ this ->_memo );
546555 $ this ->_memo [$ id ] = $ value ;
547556 $ this ->_writePut ($ id );
548557 }
549558
550559 /**
551- * Search a value in the meno and return the id
560+ * Search a value in the memo and return the id
552561 *
553562 * @param mixed $value
554563 * @return int|false The id or false
555564 */
556- protected function _searchMomo ($ value )
565+ protected function _searchMemo ($ value )
557566 {
558567 return array_search ($ value , $ this ->_memo , true );
559568 }
@@ -1100,10 +1109,6 @@ protected function _loadUnicode()
11001109 $ pattern = '/ \\\\u([a-fA-F0-9]{4})/u ' ; // \uXXXX
11011110 $ data = preg_replace_callback ($ pattern , array ($ this , '_convertMatchingUnicodeSequence2Utf8 ' ), $ data );
11021111
1103- if (self ::$ _isPhp6 ) {
1104- $ data = unicode_decode ($ data , 'UTF-8 ' );
1105- }
1106-
11071112 $ this ->_stack [] = $ data ;
11081113 }
11091114
@@ -1168,10 +1173,6 @@ protected function _loadBinUnicode()
11681173 list (, $ n ) = unpack ('l ' , $ n );
11691174 $ data = $ this ->_read ($ n );
11701175
1171- if (self ::$ _isPhp6 ) {
1172- $ data = unicode_decode ($ data , 'UTF-8 ' );
1173- }
1174-
11751176 $ this ->_stack [] = $ data ;
11761177 }
11771178
0 commit comments