Proof of concept: object serializer/de-serializer with method chaining syntax
Proof of concept representer objects with chain syntax rules notation.
Performs object serialization and object restore.
Currently does not support nested values.
To have an object with representation logic that is able to convert complex object into array/string representation and vice versa.
According to the same rules.
For example: serialize for AJAX data output and restore backend domain model on POST operation.
To have representation free of persistency or domain logic.
Or to use inside backend app for some kind of data mapping.
See tests for the most recent version.
Assume some Object with data that could not be simply json-encoded
class Post
{
public $title = 'Cool story bro';
public $status = 1;
public $pubDate;
public function __construct()
{
$this->pubDate = new \DateTime();
}
}
Create Representer
class with representation rules.
You can rename options, assing default value (in case if it will be null) and specify custom geter/setter
class PostRepresenter
{
use \einfach\representer\Representer;
public function rules()
{
return [
$this->property('title')
->rename('titleAs')
->def('Hi there!'),
$this->property('status'),
$this->property('pubDate')
->getter([$this, 'showDate'])
->setter([$this, 'extractDate'])
];
}
public function showDate($object, $attributeName)
{
return $object->$attributeName->format('Y-m-d');
}
public function extractDate($object, $attributeName, $value)
{
return \DateTime::createFromFormat('Y-m-d', $value);
}
}
$post = new Post();
$projection = PostRepresenter::one($post)->toArray();
$post1 = new Post();
$post2 = new Post();
$post3 = new Post();
$posts = [$post1, $post2, $post3];
$projection = PostRepresenter::collection($posts)->toArray();
Restoring object from presentation array data
$restoredPost = PostRepresenter::restore(Post::class)->fromArray($projection);
$restoredPosts = PostRepresenter::restoreCollection(Post::class)->fromArray($collectionProjection);
You can serialize object directly to JSON or YAML.
Serialization ability should be added via corresponding Trait
class PostRepresenter
{
use \einfach\representer\Representer;
use \einfach\representer\serializer\JSON;
....
}
$projection = PostRepresenter::one($post)->toJSON();
class PostRepresenter
{
use \einfach\representer\Representer;
use \einfach\representer\serializer\YAML;
....
}
$projection = PostRepresenter::one($post)->toYAML();
All the same goes for Collection representation.
Pretty similar to serialisation it has reverse functions
fromArray
fromJSON
fromYAML
class PostRepresenter
{
use \einfach\representer\Representer;
use \einfach\representer\serializer\JSON;
....
}
$projection = PostRepresenter::one($post)->toJSON();
$object = PostRepresenter::one($post)->fromJSON($projection);
toJSON
, toYAML
)fromJSON
, fromYAML
, fromArray
)::collection
and ->collection()
.->wrap('items')
and ->removeWrap()
for ->collection()
rename: function($object, $attr) { return uppercase($attr); }
)->representer(ArtistRepresenter::class)->class(Artist::class)
)->property('artist')->nested([ $this->property('films')->..., $this->property('name')->... ])
->int
, ->float
, ->string
). A way to coerce complex types/classes, DateTime?::one
, ::collection
(should be passed to all $callables)->inherit(true)
)The MIT License (MIT). Please see License File for more information.