{"id":22014831,"url":"https://github.com/nobrainr/morphism-php","last_synced_at":"2025-05-07T00:30:46.779Z","repository":{"id":62511437,"uuid":"114801074","full_name":"nobrainr/morphism-php","owner":"nobrainr","description":"Transform any Array to PHP Object. Scale your data processing","archived":false,"fork":false,"pushed_at":"2023-10-09T06:55:43.000Z","size":18,"stargazers_count":5,"open_issues_count":2,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-31T04:34:29.302Z","etag":null,"topics":["mapper","mapping","morphism","object","php"],"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/nobrainr.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,"governance":null,"roadmap":null,"authors":null}},"created_at":"2017-12-19T19:02:13.000Z","updated_at":"2024-05-17T00:35:34.000Z","dependencies_parsed_at":"2024-01-28T16:01:17.274Z","dependency_job_id":"b466c351-ad04-4577-8e35-42974db4991a","html_url":"https://github.com/nobrainr/morphism-php","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nobrainr%2Fmorphism-php","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nobrainr%2Fmorphism-php/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nobrainr%2Fmorphism-php/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nobrainr%2Fmorphism-php/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nobrainr","download_url":"https://codeload.github.com/nobrainr/morphism-php/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252791028,"owners_count":21804697,"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":["mapper","mapping","morphism","object","php"],"created_at":"2024-11-30T04:18:18.707Z","updated_at":"2025-05-07T00:30:46.701Z","avatar_url":"https://github.com/nobrainr.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Morphism PHP\n\n[![Build Status][travis-image]][travis-url]\n\u003e Helps you to transform any object structure to another.\n\nThis library is inspired by Yann Renaudin's : [Morphism library](https://github.com/emyann/morphism)\n\n## Contribution \n\n- Twitter: [@TDeneulin][twitter-account]\n- Pull requests and stars are always welcome 🙏🏽 For bugs and feature requests, [please create an issue](https://github.com/Gmulti/morphism-php/issues)\n\n\n## Getting started 🚀 \n\nInstall `morphism-php` using composer : `composer require gmulti/morphism-php`.\n\n```php\nuse Morphism\\Morphism;\n```\n\n## What does it do? 🤔\n\nMorphism uses a semantic configuration to go through the collection of graph objects you have to process. Then it extracts and computes the value from the specified path(s). Finally, it sets this value to the destination property from the schema.\n\n## Usage 🍔\nMorphism is curried function that allows a partial application with a semantic configuration. You can use it in many ways:\n\n### Example\n```php\n// Target type you want to have\nclass User {\n    public function __construct($firstName, $lastName, $phoneNumber){\n        $this-\u003efirstName   = $firstName;\n        $this-\u003elastName    = $lastName;\n        $this-\u003ephoneNumber = $phoneNumber;\n        $this-\u003ecity = null;\n   }\n}\n\n// Data source you want to map\n$data = array(\n    \"name\"      =\u003e \"Iron Man\",\n    \"firstName\" =\u003e \"Tony\",\n    \"lastName\"  =\u003e \"Stark\",\n    \"address\" =\u003e array(\n        \"city\"    =\u003e \"New York City\",\n        \"country\" =\u003e \"USA\"\n    ),\n    \"phoneNumber\" =\u003e array(\n        array(\n            \"type\"   =\u003e \"home\",\n            \"number\" =\u003e \"212 555-1234\"\n        ),\n        array(\n            \"type\"   =\u003e \"mobile\",\n            \"number\" =\u003e \"646 555-4567\"\n        )\n    )\n);\n\n// Mapping Schema ( see more examples below )\n$schema = array(\n    \"city\" =\u003e \"address.city\",\n    \"name\" =\u003e function($data){\n        return strtoupper($data[\"name\"]);\n    }\n);\n\nMorphism::setMapper(\"User\", $schema);\n\n// Map using the registered type and the registry\n$result = Morphism::map(\"User\", $data);\n\n/// *** OUTPUT *** ///\n\nclass User {\n    public $city // string(13) \"New York City\"\n    public $name  // string(8) \"iron man\"\n}\n```\n\n### Multidimensional array\n```php\n// Target type you want to have\nclass User {\n}\n\n// Data source you want to map\n$data = array(\n    array(\n        \"name\"      =\u003e \"Iron Man\",\n        \"firstName\" =\u003e \"Tony\",\n        \"lastName\"  =\u003e \"Stark\",\n        \"address\" =\u003e array(\n            \"city\"    =\u003e \"New York City\",\n            \"country\" =\u003e \"USA\"\n        ),\n        \"phoneNumber\" =\u003e array(\n            array(\n                \"type\"   =\u003e \"home\",\n                \"number\" =\u003e \"212 555-1234\"\n            ),\n            array(\n                \"type\"   =\u003e \"mobile\",\n                \"number\" =\u003e \"646 555-4567\"\n            )\n        )\n    ),\n    array(\n        \"name\"      =\u003e \"Spiderman\",\n        \"firstName\" =\u003e \"Peter\",\n        \"lastName\"  =\u003e \"Parker\",\n        \"address\" =\u003e array(\n            \"city\"    =\u003e \"New York City\",\n            \"country\" =\u003e \"USA\"\n        ),\n        \"phoneNumber\" =\u003e array(\n            array(\n                \"type\"   =\u003e \"home\",\n                \"number\" =\u003e \"999 999-9999\"\n            )\n        )\n    )\n);\n\n// Mapping Schema ( see more examples below )\n$schema = array(\n    \"city\" =\u003e \"address.city\",\n    \"name\" =\u003e function($data){\n        return strtoupper($data[\"name\"]);\n    }\n);\n\nMorphism::setMapper(\"User\", $schema);\n\n// Map using the registered type and the registry\n$result = Morphism::map(\"User\", $data);\n\n/// *** OUTPUT *** ///\n\narray(\n    class User {\n        public $city // string(13) \"New York City\"\n        public $name  // string(8) \"iron man\"\n    },\n    class User {\n        public $city // string(13) \"New York City\"\n        public $name  // string(8) \"spiderman\"\n    }\n)\n```\n\n## Schema Examples\n\n### Dataset sample\n```php\n$data = array(\n    \"name\"      =\u003e \"Iron Man\",\n    \"firstName\" =\u003e \"Tony\",\n    \"lastName\"  =\u003e \"Stark\",\n    \"address\" =\u003e array(\n        \"city\"    =\u003e \"New York City\",\n        \"country\" =\u003e \"USA\"\n    ),\n    \"phoneNumber\" =\u003e array(\n        array(\n            \"type\"   =\u003e \"home\",\n            \"number\" =\u003e \"212 555-1234\"\n        ),\n        array(\n            \"type\"   =\u003e \"mobile\",\n            \"number\" =\u003e \"646 555-4567\"\n        )\n    )\n);\n\n// Target type you want to have\nclass User {\n}\n```\n\n### Agregator\n\n```php\n// Schema\n$schema = array(\n    \"fullName\" =\u003e array(\"firstName\", \"lastName\")\n);\n\nMorphism::setMapper(\"User\", $schema);\n\n// Map using the registered type and the registry\n$result = Morphism::map(\"User\", $data);\n\n/// *** OUTPUT *** ///\n\nclass User {\n    public $fullName // \"Tony Stark\"\n}\n```\n\n### Computing over Flattening / Projection\n\n```php\n// Schema\n$schema = array(\n    \"city\" =\u003e (object) array(\n        \"path\" =\u003e \"address.city\",\n        \"fn\"   =\u003e function($city) {\n            return strtolower($city);\n        }\n    ),\n    \"nbContacts\" =\u003e function($data){\n        return count($data[\"phoneNumber\"]);\n    }\n);\n\nMorphism::setMapper(\"User\", $schema);\n\n// Map using the registered type and the registry\n$result = Morphism::map(\"User\", $data);\n\n/// *** OUTPUT *** ///\n\nclass User {\n    public $city // \"new york city\" \u003c= strtolower\n    public $nbContacts // 2 \u003c= computed from the object\n}\n```\n\n## License\n\nMIT © [Thomas Deneulin][twitter-account]\n\n[twitter-account]: https://twitter.com/TDeneulin\n[travis-image]: https://travis-ci.org/Gmulti/morphism-php.svg?branch=master\n[travis-url]: https://travis-ci.org/Gmulti/morphism-php\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnobrainr%2Fmorphism-php","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnobrainr%2Fmorphism-php","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnobrainr%2Fmorphism-php/lists"}