2017 © Pedro Pelรกez
 

library seriquent

Eloquent anonymized (de)serialization

image

prewk/seriquent

Eloquent anonymized (de)serialization

  • Thursday, October 27, 2016
  • by prewk
  • Repository
  • 2 Watchers
  • 1 Stars
  • 4,131 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 3 Open issues
  • 26 Versions
  • 3 % Grown

The README.md

Seriquent Build Status Coverage Status

  1. Serialize a tree of Eloquent models into a self-referential array with anonymized primary keys
  2. Deserialize it back into the database with new primary keys

Installation

composer require prewk/seriquent, (*1)

Example

Your database has a foos table, belonging to a qux table., (*2)

foos has a one-to-many relation to the bars table., (*3)

Both bars and qux have a polymorphic one-to-many relation to the bazs table (They are "bazable")., (*4)

Database contents

                โ•”โ•โ•โ•โ•โ•โ•โ•โ•—    โ•”โ•โ•โ•โ•โ•โ•โ•โ•—
             +--โ•‘ Foo 5 โ•‘----โ•‘ Qux 2 โ•‘
             |  โ•šโ•โ•โ•โ•โ•โ•โ•โ•    โ•šโ•โ•โ•โ•โ•โ•โ•โ•
             |                   |
             |                   |
    /--------+--------\          |
โ•”โ•โ•โ•โ•โ•โ•โ•โ•—โ•”โ•โ•โ•โ•โ•โ•โ•โ•—โ•”โ•โ•โ•โ•โ•โ•โ•โ•—      |
โ•‘ Bar 2 โ•‘โ•‘ Bar 3 โ•‘โ•‘ Bar 4 โ•‘      |
โ•šโ•โ•โ•โ•โ•โ•โ•โ•โ•šโ•โ•โ•โ•โ•โ•โ•โ•โ•šโ•โ•โ•โ•โ•โ•โ•โ•      |
    |        |                   |
    |        |                   |
โ•”โ•โ•โ•โ•โ•โ•โ•โ•—โ•”โ•โ•โ•โ•โ•โ•โ•โ•—           โ•”โ•โ•โ•โ•โ•โ•โ•โ•—
โ•‘ Baz 5 โ•‘โ•‘ Baz 6 โ•‘           โ•‘ Baz 7 โ•‘
โ•šโ•โ•โ•โ•โ•โ•โ•โ•โ•šโ•โ•โ•โ•โ•โ•โ•โ•           โ•šโ•โ•โ•โ•โ•โ•โ•โ•

โ•”โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•—
โ•‘ foos                โ•‘
โ•Ÿโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ข
โ•‘ id: 5               โ•‘
โ•‘ qux_id: 2           โ•‘
โ•‘ name: "Lorem ipsum" โ•‘
โ•šโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
โ•”โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•—โ•”โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•—โ•”โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•—
โ•‘ bars      โ•‘โ•‘ bars      โ•‘โ•‘ bars      โ•‘
โ•Ÿโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ขโ•Ÿโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ขโ•Ÿโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ข
โ•‘ id: 2     โ•‘โ•‘ id: 3     โ•‘โ•‘ id: 4     โ•‘
โ•‘ foo_id: 5 โ•‘โ•‘ foo_id: 5 โ•‘โ•‘ foo_id: 5 โ•‘
โ•šโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•šโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•šโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
โ•”โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•—
โ•‘ quxes                 โ•‘
โ•Ÿโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ข
โ•‘ id: 2                 โ•‘
โ•‘ data: ["a", "b", "c"] โ•‘
โ•šโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
โ•”โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•—โ•”โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•—โ•”โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•—
โ•‘ bazs            โ•‘โ•‘ bazs            โ•‘โ•‘ bazs            โ•‘
โ•Ÿโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ขโ•Ÿโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ขโ•Ÿโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ข
โ•‘ id: 5           โ•‘โ•‘ id: 6           โ•‘โ•‘ id: 7           โ•‘
โ•‘ bazable_type: 5 โ•‘โ•‘ bazable_type: 5 โ•‘โ•‘ bazable_type: 5 โ•‘
โ•‘ bazable_id: 5   โ•‘โ•‘ bazable_id: 5   โ•‘โ•‘ bazable_id: 5   โ•‘
โ•šโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•šโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•šโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•


Desired results

{
    "Foo": [
        { "@id": "@1", "qux": "@7", "name": "Lorem ipsum" }
    ],
    "Bar": [
        { "@id": "@2", "foo": "@1" },
        { "@id": "@4", "foo": "@1" },
        { "@id": "@6", "foo": "@1" }
    ],
    "Baz": [
        { "@id": "@3", "bazable": ["Bar", "@2"] },
        { "@id": "@5", "bazable": ["Bar", "@4"] },
        { "@id": "@8", "bazable": ["Qux", "@7"] }
    ],
    "Qux": [
        { "@id": "@7", "data": ["a", "b", "c"] }
    ]
}
  • Foo, Bar etc are the Eloquent models' FQCNs
  • All entities get a unique internal id "@id": "@123"
  • An entity refers to another entity by its internal id and the relation name "foo": "@1"
  • Regular columns are just values "name": "Lorem ipsum"

Eloquent models and serialization blueprints

<?php
use Illuminate\Database\Eloquent\Model;

class Foo extends Model {
    public static function getBlueprint() {
         return ["name", "bars", "qux"];
    }
    public function bars() { return $this->hasMany("Bar"); }
    public function qux() { return $this->belongsTo("Qux"); }
}

class Bar extends Model {
    public static function getBlueprint() {
         return ["foo", "bazs"];
    }
    public function foo() { return $this->belongsTo("Foo"); }
    public function bazs() { return $this->morphMany("Baz", "bazable"); }
}

class Baz extends Model {
    public static function getBlueprint() {
         return ["bazable"];
    }
    public function bazable() { return $this->morphTo(); }
}

class Qux extends Model {
    protected $casts = ["data" => "json"];
    public static function getBlueprint() {
         return ["foo", "bazs", "data"];
    }
    public function foo() { return $this->hasOne("Foo"); }
    public function bazs() { return $this->morphMany("Baz", "bazable"); }
}

Describe a model's fields and relations to serialize/deserialize with a static getBlueprint method returning an array of strings., (*5)

The strings are either a column name or a relation name. Primary key is automatically fetched., (*6)

Usage

<?php
use Prewk\Seriquent;

// The factory helper method
$seriquent = Seriquent::make();

// Deserialize from Foo with id 5
$serialization = $seriquent->serialize(Foo::findOrFail(5));

// Save to disk
file_put_contents("serialization.json", json_encode($serialization));

// Load from disk
$serialization = json_decode(file_get_contents("serialization.json"), true);

// Deserialize into database
$results = $seriquent->deserialize($serialization);

// $results is an <internal @id> => <database id> associative array

License

MIT, (*7)

The Versions

29/09 2016

dev-eloquent-morph-map-support

dev-eloquent-morph-map-support http://github.com/prewk/seriquent

Eloquent anonymized (de)serialization

  Sources   Download

MIT

The Requires

 

The Development Requires