PHP code example of harp-orm / serializer

1. Go to this page and download the library: Download harp-orm/serializer 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/ */

    

harp-orm / serializer example snippets


use Harp\Serializer;

$serializers = new Serializer\Serializers([
    new Serializer\Native('nativeSerializerdArray'),
    new Serializer\Csv('csvString'),
    new Serializer\Json('jsonProperty'),
]);

$obj = new stdClass();

$obj->nativeSerializerdArray = array('test' => 'param');
$obj->csvString = array('val', 'val2');
$obj->jsonProperty = array('test' => 'asd');

$serializers->serialize($obj);

// Will output:
// stdClass Object
// (
//     [nativeSerializerdArray] => a:1:{s:4:"test";s:5:"param";}
//     [csvString] => val,val2
//     [jsonProperty] => {"test":"asd"}
// )
print_r($obj);

// Will unserialize all the relevant properties
$serializers->unserialize($obj);

use Serializable;

class SimpleObject implements Serializable
{
    public $prop1;
    public $prop2;

    public function serialize()
    {
        return $this->prop1.','.$this->prop2;
    }

    public function unserialize($data)
    {
        list($this->prop1, $this->prop2) = explode(',', $data);
    }
}

$serializers = new Serializer\Serializers([
    new Serializer\Object('test', 'SimpleObject'),
]);

$obj = new stdClass();
$obj->test = new SimpleObject();
$obj->test->prop1 = 10;
$obj->test->prop2 = 20;

$serializers->serialize($obj);

// Will output:
// stdClass Object
// (
//     [test] => 10,20
// )
print_r($obj);

// Will unserialize all the relevant properties
$serializers->unserialize($obj);

// Model
class Payment extends AbstractModel
{
    public $id;
    public $response;
}

// Repo
class Payment extends AbstractRepo
{
    public function initialize()
    {
        $this
            ->addSerializers([
                new Serializer\Json('response'),
            ]);
    }
}

$model = new Model\Payment(['response' => $someArray]);

// The response property will get serialized as a json string.
Repo\Payment::get()->save($model);

class TestConfig {
    use SerializersTrait;
}

$config = new TestConfig();

$config
    ->serializeCsv('name');

// Return the Asserts object
$config->getSerializers();