{"id":20191401,"url":"https://github.com/htfy96/magictransformer.php","last_synced_at":"2026-05-08T22:32:36.333Z","repository":{"id":56986455,"uuid":"63217367","full_name":"htfy96/MagicTransformer.php","owner":"htfy96","description":"Bidirectional array-like struct mapper in PHP","archived":false,"fork":false,"pushed_at":"2016-07-13T08:40:59.000Z","size":23,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-01-13T18:52:33.025Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/htfy96.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-07-13T05:35:08.000Z","updated_at":"2016-11-06T05:06:18.000Z","dependencies_parsed_at":"2022-08-21T10:10:14.683Z","dependency_job_id":null,"html_url":"https://github.com/htfy96/MagicTransformer.php","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/htfy96%2FMagicTransformer.php","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/htfy96%2FMagicTransformer.php/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/htfy96%2FMagicTransformer.php/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/htfy96%2FMagicTransformer.php/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/htfy96","download_url":"https://codeload.github.com/htfy96/MagicTransformer.php/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241629736,"owners_count":19993707,"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":[],"created_at":"2024-11-14T03:48:44.861Z","updated_at":"2026-05-08T22:32:36.296Z","avatar_url":"https://github.com/htfy96.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# MagicTransformer.php\nBidirectional array-like struct mapper in PHP. \n\n[中文](https://github.com/htfy96/MagicTransformer.php/tree/cn)\n\n## Initiative\nModel transformation is common in PHP project. Traditional way is to write `A::transformToB` and `A::setFromB` method. However in most cases the logic behind these two methods is almost the same, except that one constructs object and the other extract information from object.\n\nThen comes this library: define transformation once, use it bidirectionally.\n\n## Usage\n```php\nrequire_once 'magic_transformer.php';\nuse MagicTransform\\M as M;\n```\n\nA mapper is an object that implments `MagicTransform\\iBidirectionTrans`:\n\n```php\ninterface iBidirectionTrans {\n    public function forward_map($left_val);\n    public function reverse_map($right_val, \u0026$left_obj);\n}\n```\n\n```php\n//Define transformation\n$trans = M::make_mapper(\n            [\n                'abc' =\u003e M::$self[0], // the value of 'abc' is $left[0]\n                'ccc' =\u003e M::make_chain(\n                    M::$self[0], // The 2nd argument(which is a mapper) will receive $left[1] as $left\n                    [\n                        'yyy' =\u003e M::$self // Use self to reference $left\n                    ]\n                ),\n                'ddd' =\u003e M::self['eee']['fff'], // to reference $left['eee']['fff']\n                'eee' =\u003e M::make_chain(\n                    M::$self[2], // now $left(in make_list_mapper) is $left[2]\n                    M::make_list_mapper( // Apply the mapper to each item of list\n                        M::make_func_mapper( // customize mapper!\n                            function($left_val) { // forward\n                                return $left_val + 1;\n                            },\n                            function ($right_val, \u0026$left_obj) {\n                                $left_obj = $right_val - 1;\n                            }\n                        )\n                    )\n                )\n            ]\n        );\n\n$left = ['0th', '1st', [1,2,3], 'eee' =\u003e ['fff' =\u003e 4]];\n\n$right = $trans-\u003eforward_map($left);\nprint_r($right);\n\n/*\n * Array\n(\n    [abc] =\u003e 0th\n    [ccc] =\u003e Array\n        (\n            [yyy] =\u003e 1st\n        )\n\n    [ddd] =\u003e 4\n    [eee] =\u003e Array\n        (\n            [0] =\u003e 2\n            [1] =\u003e 3\n            [2] =\u003e 4\n        )\n */\n\n// Now let's modify $right\n\n$right['abc'] = '0th0th';\n$right['ccc']['yyy'] = ['1st1st'];\n$right['ddd'] = 100;\n$right['eee'][1] = 7;\n\n$left = array(); // You can use your ORM here!\n$trans-\u003ereverse_map($right, $left);\n\nprint_r($left);\n/*\nArray\n(\n    [0] =\u003e 0th0th\n    [1] =\u003e Array\n        (\n            [0] =\u003e 1st1st\n        )\n\n    [eee] =\u003e Array\n        (\n            [fff] =\u003e 100\n        )\n\n    [2] =\u003e Array\n        (\n            [0] =\u003e 1\n            [1] =\u003e 6\n            [2] =\u003e 3\n        )\n\n)\n*/\n```\n\nAnd that's all!\n\n## API\n\nAll following API is in `MagicTransform` namespace.\n### Interface\n#### iBidirectionTrans\n```php\ninterface iBidirectionTrans {\n    public function forward_map($left_val);\n    public function reverse_map($right_val, \u0026$left_obj);\n}\n```\n\nAll mapper shall implement this interface.\n\n### Mapper/mapper maker\nAll mapper maker is defined in class `M`:\n#### make_key_mapper\nmaps `$left[$key1][$key2]...`\n\n```php\nM::make_key_mapper('abc', 'def'); //$left['abc']['def']\n```\n\n#### __0/__1/__2/__3\nalias of make_key_mapper(0), ..., make_key_mapper(3)\n\n#### self\nA mapper which always returns itself and never modify left value when called.\n\nMagic function [] overloaded as sugar of `make_key_mapper`\n\n#### make_chain\nMap in order Arg1, Arg2, ..., ArgN\n\n```php\nM::make_chain(M::$__0, M::$__1); // $left[0][1], which is equivalent to make_key_mapper(0, 1)\n```\n\n#### make_list_mapper\naccept a mapper and apply it to each item of $left\n\n```php\nM::make_list_mapper(M::$__0); //[$left[0][0], $left[1][0], ...]\n```\n\n#### make__func_mapper\nCustomize your own mapper. The first argument is forward mapper and the second is reverse mapper.\n\n```php\nM::make_func_mapper( // customize mapper!\n                    function($left_val) { // forward\n                        return $left_val + 1;\n                    },\n                    function ($right_val, \u0026$left_obj) {\n                        $left_obj = $right_val - 1;\n                    }\n                ); // $left + 1\n```\n\n#### make_mapper\nTransform a simple array to mapper automaticly.\n\nTransformation should be defined with this method.\n\n```php\nmake_mapper(\n            [\n                'aaa' =\u003e M::$__0\n            ]\n        );\n```\n\n## License\n\nApache License.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhtfy96%2Fmagictransformer.php","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhtfy96%2Fmagictransformer.php","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhtfy96%2Fmagictransformer.php/lists"}