{"id":22496974,"url":"https://github.com/morebec/orkestra-normalization","last_synced_at":"2026-05-01T15:38:34.072Z","repository":{"id":49499606,"uuid":"359992412","full_name":"Morebec/orkestra-normalization","owner":"Morebec","description":"[READ ONLY] Orkestra Component allowing to easily normalize complex object graphs to arrays of primitives for persistence purposes","archived":false,"fork":false,"pushed_at":"2023-03-31T18:46:52.000Z","size":53,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"2.x","last_synced_at":"2025-03-03T18:03:52.686Z","etag":null,"topics":["document","normalization","orkestra","orm","persistence","php","serialization"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Morebec.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-04-21T00:59:58.000Z","updated_at":"2022-07-25T15:52:01.000Z","dependencies_parsed_at":"2024-12-06T20:15:08.218Z","dependency_job_id":null,"html_url":"https://github.com/Morebec/orkestra-normalization","commit_stats":{"total_commits":61,"total_committers":2,"mean_commits":30.5,"dds":"0.049180327868852514","last_synced_commit":"971d2977db7d5ba064a5885cb9ae96a4b269e0e0"},"previous_names":[],"tags_count":21,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Morebec%2Forkestra-normalization","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Morebec%2Forkestra-normalization/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Morebec%2Forkestra-normalization/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Morebec%2Forkestra-normalization/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Morebec","download_url":"https://codeload.github.com/Morebec/orkestra-normalization/tar.gz/refs/heads/2.x","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245925697,"owners_count":20694946,"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":["document","normalization","orkestra","orm","persistence","php","serialization"],"created_at":"2024-12-06T20:15:04.899Z","updated_at":"2026-05-01T15:38:34.034Z","avatar_url":"https://github.com/Morebec.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Normalization\nThe normalization component allows to easily normalize complex object graphs to arrays of primitives for persistence purposes.\n\nEssentially, it makes transforming POPOs into human-readable, platform independent serializable arrays\nof primitives a breeze.\n\nIt can be used to:\n- Serialize in any format that supports PHP arrays and primitives.\n- Dump objects to an SQL database without the need for an ORM.\n- Dump complex object graphs to a document store.\n- Load an object in memory from a serialized representation.\n- Convert HTTP requests to Typed Objects.\n\n\n## Installation\n\n## Usage\nLet's take an example of an object graph:\n```php\nclass Project \n{\n    /** @var string */\n    private $id;\n    \n    /** @var string */\n    private $title;\n    \n    /** @var string */\n    private $description;\n    \n    /** @var Task[] */\n    private $tasks;\n    \n    // ...\n}\n\nclass Task \n{\n    /** @var string */\n    private $id;\n    \n    /** @var string */\n    private $name;\n    \n    /** @var DateTime */\n    private $dueDate;\n    \n    /** @var bool */\n    private $completed;\n    \n    // ...\n}\n```\n\nHere's how one can normalize an object:\n\n```php\nuse Morebec\\Orkestra\\Normalization\\ObjectNormalizer;$project = new Project('prj123456789', 'A new Project', 'This is our latest project');\n$project-\u003eaddTask(new Task('tsk123456789', 'Deploy to production', $dueDate));\n\n// Normalize\n$normalizer = new ObjectNormalizer();\n$data = $normalizer-\u003enormalize($project);\n\nprint_r($data);\n// Would print:\n[\n    'id' =\u003e 'prj123456789',\n    'title' =\u003e 'A new Project',\n    'description' =\u003e 'This is our latest project',\n    'tasks' =\u003e [\n        [\n            'id' =\u003e 'tsk123456789',\n            'name' =\u003e 'Deploy to production',\n            'dueDate' =\u003e '2021-01-01T10:25:55+00:00',\n            'completed' =\u003e false\n        ]       \n    ]\n];\n```\n\nThis last representation can easily be serialized to json, xml, yaml or any other format.\n\n### Denormalization\nHere's how to convert a normalized object back to an instance of its class:\n\n```php\n\n// Would print:\nuse Morebec\\Orkestra\\Normalization\\ObjectNormalizer;$data = [\n    'id' =\u003e 'prj123456789',\n    'title' =\u003e 'A new Project',\n    'description' =\u003e 'This is our latest project',\n    'tasks' =\u003e [\n        [\n            'id' =\u003e 'tsk123456789',\n            'name' =\u003e 'Deploy to production',\n            'dueDate' =\u003e '2021-01-01T10:25:55+00:00',\n            'completed' =\u003e false\n        ]       \n    ]\n];\n$normalizer = new ObjectNormalizer();\n$project = $normalizer-\u003edenormalize($data, Project::class);\n```\n\n\u003e The process of denormalization works using reflection inspecting an object's structure.\n\u003e In order to know what into what type to denormalize a value to, the normalizer checks for `@var`\n\u003e annotations on the object for PHP \u003c 7.4, or the declared type for PHP \u003e= 7.4. Therefore, it is important\n\u003e to correctly declare the @var annotations if using an older version of PHP.\n\n\n### Custom normalizers/denormalizer.\nDepending on the structure of your objects you might want to personalize the way they are (de)normalized.\nA common example is with value objects wrapping primitives:\n\n```php\nclass Username {\n\n    /** @var string */    \n    private $value;\n\n    public function __construct(string $value) {\n        $this-\u003evalue = $value;\n    }\n    \n    public function __toString()\n    {\n        return $this-\u003evalue;\n    }\n}\n```\n\nSuch object out of the box would be normalized as follows:\n```php\n[\n    'value' =\u003e 'the_username'\n];\n```\n\nFor such object, and especially when they are part of an object graph, you might want to normalize them to\nthe primitive they wrap for example.\n\nTo perform this you need to specify a custom normalizer/denormalizer pair and add it to your normalizer:\n\n```php\n\nuse Morebec\\Orkestra\\Normalization\\Denormalizer\\DenormalizationContextInterface;\nuse Morebec\\Orkestra\\Normalization\\ObjectNormalizer;\nuse Morebec\\Orkestra\\Normalization\\Normalizer\\ObjectNormalizer\\FluentNormalizer;\nuse Morebec\\Orkestra\\Normalization\\Denormalizer\\ObjectDenormalizer\\FluentDenormalizer;\n\n$normalizer = new ObjectNormalizer();\n\n$normalizer-\u003eaddNormalizer(FluentNormalizer::for(Username::class)-\u003easString());\n$normalizer-\u003eaddDenormalizer(FluentDenormalizer::for(Username::class)-\u003eas(static function (DenormalizationContextInterface $context) {\n    return new Username($context-\u003egetValue());\n}));\n```\n\nThe `FluentNormalizer` and `FluentDenormalizer` are the most convenient way to define (De)Normalizers.\nIf you want to have full control over them you can implement the `NormalizerInterface` and `DenormalizerInterface`.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmorebec%2Forkestra-normalization","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmorebec%2Forkestra-normalization","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmorebec%2Forkestra-normalization/lists"}