PHP code example of eremin / serende

1. Go to this page and download the library: Download eremin/serende library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

eremin / serende example snippets


use Eremin\SerEnDe\Factory;
$decoder = Factory::createDecoder();
$encoder = Factory::createEncoder();

$test = [
    1 => 'a',
    'b' => 2,
    true,
    'refTarget' => 'REF_TARGET',
];
$test['ref'] = &$test['refTarget'];
$serialized = \serialize($test);
/// string(86) "a:5:{i:1;s:1:"a";s:1:"b";i:2;i:2;b:1;s:9:"refTarget";s:10:"REF_TARGET";s:3:"ref";R:5;}"
$parsed = $decoder->decodeFromString($serialized);
// object(Eremin\SerEnDe\Types\ArrayType)#25 (2) {
//   ["elements":"Eremin\SerEnDe\Types\ArrayType":private]=>
//   array(5) {
//     [0]=>
//     object(Eremin\SerEnDe\Types\ArrayType\Element)#28 (2) {
//       ["key":"Eremin\SerEnDe\Types\ArrayType\Element":private]=>
//       object(Eremin\SerEnDe\Types\IntType)#26 (1) {
//         ["rawContent"]=>
//         string(1) "1"
//       }
//       ["value":"Eremin\SerEnDe\Types\ArrayType\Element":private]=>
//       object(Eremin\SerEnDe\Types\StringType)#27 (1) {
//         ["rawContent"]=>
//         string(1) "a"
//       }
//     }
//     [1]=>
//     object(Eremin\SerEnDe\Types\ArrayType\Element)#31 (2) {
//       ["key":"Eremin\SerEnDe\Types\ArrayType\Element":private]=>
//       object(Eremin\SerEnDe\Types\StringType)#29 (1) {
//         ["rawContent"]=>
//         string(1) "b"
//       }
//       ["value":"Eremin\SerEnDe\Types\ArrayType\Element":private]=>
//       object(Eremin\SerEnDe\Types\IntType)#30 (1) {
//         ["rawContent"]=>
//         string(1) "2"
//       }
//     }
//     [2]=>
//     object(Eremin\SerEnDe\Types\ArrayType\Element)#34 (2) {
//       ["key":"Eremin\SerEnDe\Types\ArrayType\Element":private]=>
//       object(Eremin\SerEnDe\Types\IntType)#32 (1) {
//         ["rawContent"]=>
//         string(1) "2"
//       }
//       ["value":"Eremin\SerEnDe\Types\ArrayType\Element":private]=>
//       object(Eremin\SerEnDe\Types\BoolType)#33 (1) {
//         ["rawContent"]=>
//         string(1) "1"
//       }
//     }
//     [3]=>
//     object(Eremin\SerEnDe\Types\ArrayType\Element)#37 (2) {
//       ["key":"Eremin\SerEnDe\Types\ArrayType\Element":private]=>
//       object(Eremin\SerEnDe\Types\StringType)#35 (1) {
//         ["rawContent"]=>
//         string(9) "refTarget"
//       }
//       ["value":"Eremin\SerEnDe\Types\ArrayType\Element":private]=>
//       object(Eremin\SerEnDe\Types\StringType)#36 (1) {
//         ["rawContent"]=>
//         string(10) "REF_TARGET"
//       }
//     }
//     [4]=>
//     object(Eremin\SerEnDe\Types\ArrayType\Element)#40 (2) {
//       ["key":"Eremin\SerEnDe\Types\ArrayType\Element":private]=>
//       object(Eremin\SerEnDe\Types\StringType)#38 (1) {
//         ["rawContent"]=>
//         string(3) "ref"
//       }
//       ["value":"Eremin\SerEnDe\Types\ArrayType\Element":private]=>
//       object(Eremin\SerEnDe\Types\ReferenceType)#39 (2) {
//         ["referencedType":"Eremin\SerEnDe\Types\ReferenceType":private]=>
//         object(Eremin\SerEnDe\Types\StringType)#36 (1) {
//           ["rawContent"]=>
//           string(10) "REF_TARGET"
//         }
//         ["rawContent"]=>
//         string(1) "5"
//       }
//     }
//   }
//   ["rawContent"]=>
//   NULL
// } 

$serialized = $encoder->encode($parsed);
// a:5:{i:1;s:1:"a";s:1:"b";i:2;i:2;b:1;s:9:"refTarget";s:10:"REF_TARGET";s:3:"ref";R:5;}
$data = unserialize($serialized);
// array(5) {
//   [1]=>
//   string(1) "a"
//   ["b"]=>
//   int(2)
//   [2]=>
//   bool(true)
//   ["refTarget"]=>
//   &string(10) "REF_TARGET"
//   ["ref"]=>
//   &string(10) "REF_TARGET"
// }

$type = new \Eremin\SerEnDe\Types\NullType();
$encoder->encode($type);
// N;

$type = new \Eremin\SerEnDe\Types\BoolType();
$type->setValue(true);
$encoder->encode($type);
// b:1;
$type->getValue();
// bool(true)

$type = new \Eremin\SerEnDe\Types\IntType();
$type->setValue(-555);
$encoder->encode($type);
// i:-555;
$type->getValue();
// int(-555)

$type = new \Eremin\SerEnDe\Types\FloatType();
$type->setValue(-5e-5);
$encoder->encode($type);
// d:-5.0E-5;;
$type->getValue();
// float(-5.0E-5)

$type = new \Eremin\SerEnDe\Types\StringType();
$type->rawContent = "hello \"\\ world!";
$encoder->encode($type);
// s:15:"hello "\ world!";

class A implements Serializable {
    public $foo;
    public function serialize()
    {
        return \json_encode($this);
    }

    public function unserialize($serialized)
    {
        return \json_decode($serialized, true);
    }
}
class B {}
$type = new \Eremin\SerEnDe\Types\SerializableObjectType();
$type->rawContent = \json_encode(['foo' => 'bar']);
$encoder->encode($type);
// C:1:"A":13:{{"foo":"bar"}}
$type->className = 'B';
unserialize($encoder->encode($type));
// Warning: Class B has no unserializer
// object(B)#23 (0) {
// }