PHP code example of prewk / seriplating

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

    

prewk / seriplating example snippets



use Prewk\Seriplating\GenericDeserializer;
use Prewk\Seriplating\GenericSerializer;
use Prewk\Seriplating\IdFactory;
use Prewk\Seriplating\IdResolver;
use Prewk\Seriplating\HierarchicalTemplate;
use Prewk\SeriplatingTemplate;
use Prewk\Seriplating\Seriplater;
use Prewk\Seriplating\Rule;

// Boilerplate classes
$t = new Seriplater(new Rule);
$idFactory = new IdFactory;
$idResolver = new IdResolver;
$genericSerializer = new GenericSerializer($idFactory);
$genericDeserializer = new GenericDeserializer($idResolver);
$hierarchy = new HierarchicalTemplate($idResolver);

// Bring your own repositories, implement Prewk\Seriplating\Contracts\RepositoryInterface
$fooRepository = new FooRepository;
$barRepository = new BarRepository;
$bazRepository = new BazRepository;

// A Foo entity has two Bars and a Baz
// Corresponding database tables could be foos, bars, and bazes

// Construct the serialization templates
// ..for Foo
$fooSeriplater = new GenericSerializer($genericSerializer, $genericDeserializer, fooRepository, [
    "id" => $t->id("foos"),
    "setting" => $t->value(),
    "bars" => $t->hasMany("bars"),
]);

// ..for Bar
$barSeriplater = new GenericSerializer($genericSerializer, $genericDeserializer, barRepository, [
    "id" => $t->id("bars"),
    "sort_order" => $t->increments(),
    "baz_id" => $t->references("bazes"),
]);

// ..for Baz
$bazSeriplater = new GenericSerializer($genericSerializer, $genericDeserializer, bazRepository, [
    "id" => $t->id("bazes"),
    "content" => $t->value(),
]);

// Register them in the hierarchical templating class
$hierarchy
    ->register($fooSeriplater)
    ->register($barSeriplater)
    ->register($bazSeriplater);



// Entity built up from your own database content
$entityWithChildren = [
    "id" => 1,
    "setting" => "some value",
    "bars" => [
        [
            "id" => 1,
            "sort_order" => 0,
            "baz_id" => 1,
        ],
        [
            "id" => 2,
            "sort_order" => 1,
            "baz_id" => 1,
        ],
    ],
    "bazes" => [
        [
            "id" => 1,
            "content" => "<p>Lorem ipsum<p>",
        ],
    ]
];

// Serialize
$serialization = $hierarchy->serialize("foos", $entityWithChildren);

// Deserialize
$deserialized = $hierarchy->deserialize("foos", $serialization);

// The appropriate provided repositories will now have been called and entities will be created in the db
$fooId = $deserialized["id"];


$template = [
    "id" => $t->id("foos"),
    "type" => $t->value(),
    "refers_to_different_things_depending_on_type" => $t->conditions("type", [
        "Bar" => $t->references("bars"),
        "Baz" => $t->references("bazes"),
        "Qux" => $t->value(),
    ]),
];


$entity = [
    "id" => 123,
    "data" => [
        "something" => [
            ["id" => 500, "stuff" => true], // something.0.id = 500
            ["id" => 600, "stuff" => true], // something.1.id = 600
            ["id" => 700, "stuff" => false], // something.2.id = 700
        ],
        "bar_id" => 5,
    ],
];

$template = [
    "id" => $t->id("foos"),
    "data" => $t->deep([
        "/\\.something\\.\d+.id$/" => $t->references("something"),
        "/^bar_id$/" => $t->inherits("id"),
    ])
];


$template1 = [
    "id" => $t->id("foos"),
    "bars" => $t->hasMany("bars"),
];
$template2 = [
    "id" => $t->id("bars"),
    "foo_id" => $t->references("foos"),
    "sort_order" => $t->increments(),
];


use Prewk\Seriplating\Formatters\XmlFormatter;

$serialization = ...

$xmlFormatter = new XmlFormatter;

file_put_contents("serialized.xml", $xmlFormatter->formatSerialized($serialization));

$serialization = $xmlFormatter->unformatSerialized(file_get_contents("serialized.xml"));