PHPackages                             einfach/representer - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. einfach/representer

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

einfach/representer
===================

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

062PHP

Since Jan 18Pushed 10y ago1 watchersCompare

[ Source](https://github.com/iJackUA/einfach-representer)[ Packagist](https://packagist.org/packages/einfach/representer)[ RSS](/packages/einfach-representer/feed)WikiDiscussions master Synced yesterday

READMEChangelogDependenciesVersions (2)Used By (0)

Einfach-Representer
-------------------

[](#einfach-representer)

[![Build Status](https://camo.githubusercontent.com/1f231b531a7d03077f3fa952ca79c45b42743c421661edf3b0b3a9f1f2f495de/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f694a61636b55412f65696e666163682d726570726573656e7465722f6d61737465722e7376673f7374796c653d666c6174)](https://travis-ci.org/iJackUA/einfach-representer) [![Latest Version on Packagist](https://camo.githubusercontent.com/a4abf4334907296aad186dd2bc2f028c1246adb74cf6ca80d80d7126f0fbbfc4/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f65696e666163682f726570726573656e7465722e7376673f7374796c653d666c6174)](https://packagist.org/packages/einfach/representer) [![Total Downloads](https://camo.githubusercontent.com/3951b4fe6f2788b70136283e67197612bd91be75a3730458b8cb45ee0fc635d2/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f65696e666163682f726570726573656e7465722e7376673f7374796c653d666c6174)](https://packagist.org/packages/einfach/representer) [![Software License](https://camo.githubusercontent.com/f251623e510f5909f16ae3f4e6e548dac11340b9fde1a99be26b015b39272c00/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c6174)](LICENSE.md)

[![SensioLabsInsight](https://camo.githubusercontent.com/afe6d059296312355a0cfdc4cc612bf3854c4af7e55aaf6a819b4281b944e1fc/68747470733a2f2f696e73696768742e73656e73696f6c6162732e636f6d2f70726f6a656374732f63343463316136312d303836342d346165362d393731322d6532643731323066613963352f736d616c6c2e706e67)](https://insight.sensiolabs.com/projects/c44c1a61-0864-4ae6-9712-e2d7120fa9c5) [![Coverage Status](https://camo.githubusercontent.com/b35e50822906a40f3eea3b20c50b7b884de57c78610c5f3e6a222f7bc3e1f4c9/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f694a61636b55412f65696e666163682d726570726573656e7465722e7376673f7374796c653d666c6174)](https://scrutinizer-ci.com/g/iJackUA/einfach-representer/code-structure) [![Quality Score](https://camo.githubusercontent.com/bb81df359390ab3ac9fc0af67b85be08a9fc03c171c17b18118c406d7146f718/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f694a61636b55412f65696e666163682d726570726573656e7465722e7376673f7374796c653d666c6174)](https://scrutinizer-ci.com/g/iJackUA/einfach-representer) [![Code Climate](https://camo.githubusercontent.com/714c003c340a077c45e3d9a078bdd047c5df881980916020dfb794b970511310/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636c696d6174652f6769746875622f694a61636b55412f65696e666163682d726570726573656e7465722e7376673f7374796c653d666c6174)](https://codeclimate.com/github/iJackUA/einfach-representer)

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

Currently does not support nested values.

Motivation
----------

[](#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
--------

[](#examples)

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

Representation
--------------

[](#representation)

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

Collection Representation
-------------------------

[](#collection-representation)

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

Restore object from representation
----------------------------------

[](#restore-object-from-representation)

Restoring object from presentation array data

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

Restore objects Collection from representation
----------------------------------------------

[](#restore-objects-collection-from-representation)

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

Serialization
-------------

[](#serialization)

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.

De-serialisation
----------------

[](#de-serialisation)

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

Functionality ideas:
--------------------

[](#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

Credits
-------

[](#credits)

- [Ievgen Kuzminov](https://github.com/iJackUA)
- [All Contributors](../../contributors)

License
-------

[](#license)

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

###  Health Score

21

—

LowBetter than 18% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity43

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/568566?v=4)[Yevhen Kuzminov](/maintainers/iJackUA)[@iJackUA](https://github.com/iJackUA)

---

Top Contributors

[![iJackUA](https://avatars.githubusercontent.com/u/568566?v=4)](https://github.com/iJackUA "iJackUA (35 commits)")

---

Tags

datamapperdeserializationeinfach-phprepresentationserialization

### Embed Badge

![Health badge](/badges/einfach-representer/health.svg)

```
[![Health](https://phpackages.com/badges/einfach-representer/health.svg)](https://phpackages.com/packages/einfach-representer)
```

###  Alternatives

[mck89/peast

Peast is PHP library that generates AST for JavaScript code

19139.2M44](/packages/mck89-peast)[sauladam/shipment-tracker

Parses tracking information for several carriers, like UPS, USPS, DHL and GLS by simply scraping the data. No need for any kind of API access.

9843.5k](/packages/sauladam-shipment-tracker)[moonshine/layouts-field

Field for repeating groups of fields for MoonShine

107.9k](/packages/moonshine-layouts-field)[tcds-io/php-jackson

A lightweight, flexible object serializer for PHP, inspired by FasterXML/jackson

112.9k10](/packages/tcds-io-php-jackson)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
