Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/ijackua/einfach-representer

Proof of concept: object serializer/de-serializer with method chaining syntax
https://github.com/ijackua/einfach-representer

datamapper deserialization einfach-php representation serialization

Last synced: 17 days ago
JSON representation

Proof of concept: object serializer/de-serializer with method chaining syntax

Awesome Lists containing this project

README

        

## Einfach-Representer

[![Build Status][ico-travis]][link-travis] [![Latest Version on Packagist][ico-version]][link-packagist] [![Total Downloads][ico-downloads]][link-downloads] [![Software License][ico-license]](LICENSE.md)

[![SensioLabsInsight](https://insight.sensiolabs.com/projects/c44c1a61-0864-4ae6-9712-e2d7120fa9c5/small.png)](https://insight.sensiolabs.com/projects/c44c1a61-0864-4ae6-9712-e2d7120fa9c5) [![Coverage Status][ico-scrutinizer]][link-scrutinizer] [![Quality Score][ico-code-quality]][link-code-quality] [![Code Climate][ico-codeclimate]][link-codeclimate]

Proof of concept representer objects with chain syntax rules notation.
Performs object serialization and object restore.

Currently does not support nested values.

## Motivation

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.

## Examples

See tests for the most recent version.

Assume some Object with data that could not be simply json-encoded
```php
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

```php
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);
}
}
```

## Representation

```php
$post = new Post();
$projection = PostRepresenter::one($post)->toArray();
```

## Collection Representation

```php
$post1 = new Post();
$post2 = new Post();
$post3 = new Post();
$posts = [$post1, $post2, $post3];
$projection = PostRepresenter::collection($posts)->toArray();
```

## Restore object from representation

Restoring object from presentation array data

```php
$restoredPost = PostRepresenter::restore(Post::class)->fromArray($projection);
```

## Restore objects Collection from representation

```php
$restoredPosts = PostRepresenter::restoreCollection(Post::class)->fromArray($collectionProjection);
```

## Serialization

You can serialize object directly to JSON or YAML.
Serialization ability should be added via corresponding Trait

```php
class PostRepresenter
{
use \einfach\representer\Representer;
use \einfach\representer\serializer\JSON;
....
}

$projection = PostRepresenter::one($post)->toJSON();
```

```php
class PostRepresenter
{
use \einfach\representer\Representer;
use \einfach\representer\serializer\YAML;
....
}

$projection = PostRepresenter::one($post)->toYAML();
```

All the same goes for Collection representation.

## De-serialisation

Pretty similar to serialisation it has reverse functions

* `fromArray`
* `fromJSON`
* `fromYAML`

```php
class PostRepresenter
{
use \einfach\representer\Representer;
use \einfach\representer\serializer\JSON;
....
}

$projection = PostRepresenter::one($post)->toJSON();
$object = PostRepresenter::one($post)->fromJSON($projection);
```

## Functionality ideas:

* ~~Traits composition (Representers not inherited, but added via Traits)~~
* ~~Serialisation/de-serialisation (`toJSON`, `toYAML`)~~
* ~~De-serialisation (`fromJSON`, `fromYAML`, `fromArray`)~~
* ~~Collection representation `::collection` and `->collection()`.~~
* Wrapping collections `->wrap('items')` and `->removeWrap()` for `->collection()`
* Inverse property declaration (to allow any property name in projection, not coupled with source)
* Property rules: render_null (Manage default? Example `rename: function($object, $attr) { return uppercase($attr); } `)
* Property decoration/Nested serialization (`->representer(ArtistRepresenter::class)->class(Artist::class)`)
* Nested properties `->property('artist')->nested([ $this->property('films')->..., $this->property('name')->... ])`
* Ability for "array to array" and "object to object" representations
* Coersion (`->int`, `->float`, `->string`). A way to coerce complex types/classes, DateTime?
* External options in `::one`, `::collection` (should be passed to all $callables)
* Check that Representer inheritance overwrites rules (try to do partial overwrite with `->inherit(true)`)
* Try to do Representer mixins (via Traits?)
* Do a benchmark with https://github.com/phpbench/phpbench

## Credits

- [Ievgen Kuzminov][link-author]
- [All Contributors][link-contributors]

## License

The MIT License (MIT). Please see [License File](LICENSE) for more information.

[ico-version]: https://img.shields.io/packagist/v/einfach/representer.svg?style=flat
[ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat
[ico-travis]: https://img.shields.io/travis/iJackUA/einfach-representer/master.svg?style=flat
[ico-scrutinizer]: https://img.shields.io/scrutinizer/coverage/g/iJackUA/einfach-representer.svg?style=flat
[ico-code-quality]: https://img.shields.io/scrutinizer/g/iJackUA/einfach-representer.svg?style=flat
[ico-downloads]: https://img.shields.io/packagist/dt/einfach/representer.svg?style=flat
[ico-codeclimate]: https://img.shields.io/codeclimate/github/iJackUA/einfach-representer.svg?style=flat

[link-packagist]: https://packagist.org/packages/einfach/representer
[link-travis]: https://travis-ci.org/iJackUA/einfach-representer
[link-scrutinizer]: https://scrutinizer-ci.com/g/iJackUA/einfach-representer/code-structure
[link-code-quality]: https://scrutinizer-ci.com/g/iJackUA/einfach-representer
[link-downloads]: https://packagist.org/packages/einfach/representer
[link-author]: https://github.com/iJackUA
[link-contributors]: ../../contributors
[link-codeclimate]: https://codeclimate.com/github/iJackUA/einfach-representer