{"id":15587562,"url":"https://github.com/ijackua/einfach-representer","last_synced_at":"2025-03-27T00:19:14.190Z","repository":{"id":56975948,"uuid":"49532946","full_name":"iJackUA/einfach-representer","owner":"iJackUA","description":"Proof of concept: object serializer/de-serializer with method chaining syntax","archived":false,"fork":false,"pushed_at":"2016-02-10T18:18:58.000Z","size":45,"stargazers_count":0,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-01T05:44:14.020Z","etag":null,"topics":["datamapper","deserialization","einfach-php","representation","serialization"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/iJackUA.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-01-12T22:25:20.000Z","updated_at":"2017-02-28T07:46:43.000Z","dependencies_parsed_at":"2022-08-21T11:20:37.496Z","dependency_job_id":null,"html_url":"https://github.com/iJackUA/einfach-representer","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iJackUA%2Feinfach-representer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iJackUA%2Feinfach-representer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iJackUA%2Feinfach-representer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iJackUA%2Feinfach-representer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/iJackUA","download_url":"https://codeload.github.com/iJackUA/einfach-representer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245755681,"owners_count":20667027,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["datamapper","deserialization","einfach-php","representation","serialization"],"created_at":"2024-10-02T22:02:13.557Z","updated_at":"2025-03-27T00:19:14.156Z","avatar_url":"https://github.com/iJackUA.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"## Einfach-Representer\n\n[![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)\n\n[![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]\n\n\nProof of concept representer objects with chain syntax rules notation.\nPerforms object serialization and object restore.\n\nCurrently does not support nested values.\n\n## Motivation\n\nTo have an object with representation logic that is able to convert complex object into array/string representation and vice versa. \nAccording to the same rules.\nFor example: serialize for AJAX data output and restore backend domain model on POST operation. \nTo have representation free of persistency or domain logic.  \nOr to use inside backend app for some kind of data mapping.\n\n\n## Examples\n\nSee tests for the most recent version.\n\nAssume some Object with data that could not be simply json-encoded\n```php\nclass Post\n{\n    public $title = 'Cool story bro';\n    public $status = 1;\n    public $pubDate;\n\n    public function __construct()\n    {\n        $this-\u003epubDate = new \\DateTime();\n    }\n}\n```\n\nCreate `Representer` class with representation rules.\nYou can rename options, assing default value (in case if it will be null) and specify custom geter/setter\n\n```php\nclass PostRepresenter\n{\n    use \\einfach\\representer\\Representer;\n\n    public function rules()\n    {\n        return [\n            $this-\u003eproperty('title')\n                -\u003erename('titleAs')\n                -\u003edef('Hi there!'),\n\n            $this-\u003eproperty('status'),\n\n            $this-\u003eproperty('pubDate')\n                -\u003egetter([$this, 'showDate'])\n                -\u003esetter([$this, 'extractDate'])\n        ];\n    }\n\n    public function showDate($object, $attributeName)\n    {\n        return $object-\u003e$attributeName-\u003eformat('Y-m-d');\n    }\n\n    public function extractDate($object, $attributeName, $value)\n    {\n        return \\DateTime::createFromFormat('Y-m-d', $value);\n    }\n}\n```\n\n## Representation\n\n```php\n$post = new Post();\n$projection = PostRepresenter::one($post)-\u003etoArray();\n```\n\n## Collection Representation\n\n```php\n$post1 = new Post();\n$post2 = new Post();\n$post3 = new Post();\n$posts = [$post1, $post2, $post3];\n$projection = PostRepresenter::collection($posts)-\u003etoArray();\n```\n\n## Restore object from representation \n\nRestoring object from presentation array data\n\n```php\n$restoredPost = PostRepresenter::restore(Post::class)-\u003efromArray($projection);\n```\n\n## Restore objects Collection from representation\n\n```php\n$restoredPosts = PostRepresenter::restoreCollection(Post::class)-\u003efromArray($collectionProjection);\n```\n\n## Serialization\n\nYou can serialize object directly to JSON or YAML.\nSerialization ability should be added via corresponding Trait\n\n```php\nclass PostRepresenter\n{\n    use \\einfach\\representer\\Representer;\n    use \\einfach\\representer\\serializer\\JSON;\n    ....\n}\n\n$projection = PostRepresenter::one($post)-\u003etoJSON();\n```\n\n```php\nclass PostRepresenter\n{\n    use \\einfach\\representer\\Representer;\n    use \\einfach\\representer\\serializer\\YAML;\n    ....\n}\n\n$projection = PostRepresenter::one($post)-\u003etoYAML();\n```\n\nAll the same goes for Collection representation.\n\n## De-serialisation\n\nPretty similar to serialisation it has reverse functions\n\n* `fromArray`\n* `fromJSON`\n* `fromYAML`\n\n```php\nclass PostRepresenter\n{\n    use \\einfach\\representer\\Representer;\n    use \\einfach\\representer\\serializer\\JSON;\n    ....\n}\n\n$projection = PostRepresenter::one($post)-\u003etoJSON();\n$object = PostRepresenter::one($post)-\u003efromJSON($projection);\n```\n\n\n\n## Functionality ideas:\n\n* ~~Traits composition (Representers not inherited, but added via Traits)~~\n* ~~Serialisation/de-serialisation (`toJSON`, `toYAML`)~~\n* ~~De-serialisation (`fromJSON`, `fromYAML`, `fromArray`)~~\n* ~~Collection representation `::collection` and `-\u003ecollection()`.~~\n* Wrapping collections `-\u003ewrap('items')` and `-\u003eremoveWrap()` for `-\u003ecollection()`\n* Inverse property declaration (to allow any property name in projection, not coupled with source)\n* Property rules: render_null  (Manage default? Example `rename: function($object, $attr) { return uppercase($attr); } `)\n* Property decoration/Nested serialization (`-\u003erepresenter(ArtistRepresenter::class)-\u003eclass(Artist::class)`)\n* Nested properties `-\u003eproperty('artist')-\u003enested([ $this-\u003eproperty('films')-\u003e..., $this-\u003eproperty('name')-\u003e... ])`\n* Ability for \"array to array\" and \"object to object\" representations\n* Coersion (`-\u003eint`, `-\u003efloat`, `-\u003estring`). A way to coerce complex types/classes, DateTime?\n* External options in `::one`, `::collection` (should be passed to all $callables)\n* Check that Representer inheritance overwrites rules (try to do partial overwrite with `-\u003einherit(true)`)\n* Try to do Representer mixins (via Traits?)\n* Do a benchmark with https://github.com/phpbench/phpbench\n\n\n## Credits\n\n- [Ievgen Kuzminov][link-author]\n- [All Contributors][link-contributors]\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE) for more information.\n\n[ico-version]: https://img.shields.io/packagist/v/einfach/representer.svg?style=flat\n[ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat\n[ico-travis]: https://img.shields.io/travis/iJackUA/einfach-representer/master.svg?style=flat\n[ico-scrutinizer]: https://img.shields.io/scrutinizer/coverage/g/iJackUA/einfach-representer.svg?style=flat\n[ico-code-quality]: https://img.shields.io/scrutinizer/g/iJackUA/einfach-representer.svg?style=flat\n[ico-downloads]: https://img.shields.io/packagist/dt/einfach/representer.svg?style=flat\n[ico-codeclimate]: \thttps://img.shields.io/codeclimate/github/iJackUA/einfach-representer.svg?style=flat\n\n[link-packagist]: https://packagist.org/packages/einfach/representer\n[link-travis]: https://travis-ci.org/iJackUA/einfach-representer\n[link-scrutinizer]: https://scrutinizer-ci.com/g/iJackUA/einfach-representer/code-structure\n[link-code-quality]: https://scrutinizer-ci.com/g/iJackUA/einfach-representer\n[link-downloads]: https://packagist.org/packages/einfach/representer\n[link-author]: https://github.com/iJackUA\n[link-contributors]: ../../contributors\n[link-codeclimate]: https://codeclimate.com/github/iJackUA/einfach-representer\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fijackua%2Feinfach-representer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fijackua%2Feinfach-representer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fijackua%2Feinfach-representer/lists"}