{"id":22092090,"url":"https://github.com/krakphp/marshal","last_synced_at":"2025-10-15T01:42:40.073Z","repository":{"id":57009029,"uuid":"76676804","full_name":"krakphp/marshal","owner":"krakphp","description":"Marshal/Transform Data","archived":false,"fork":false,"pushed_at":"2017-06-12T07:50:59.000Z","size":22,"stargazers_count":2,"open_issues_count":2,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-07-19T21:23:35.112Z","etag":null,"topics":["api-serialization","hydrator","marhsaller","marshaller"],"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/krakphp.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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-12-16T18:37:59.000Z","updated_at":"2021-08-26T08:35:06.000Z","dependencies_parsed_at":"2022-08-21T14:50:50.458Z","dependency_job_id":null,"html_url":"https://github.com/krakphp/marshal","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"purl":"pkg:github/krakphp/marshal","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/krakphp%2Fmarshal","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/krakphp%2Fmarshal/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/krakphp%2Fmarshal/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/krakphp%2Fmarshal/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/krakphp","download_url":"https://codeload.github.com/krakphp/marshal/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/krakphp%2Fmarshal/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":271215036,"owners_count":24720097,"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","status":"online","status_checked_at":"2025-08-19T02:00:09.176Z","response_time":63,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["api-serialization","hydrator","marhsaller","marshaller"],"created_at":"2024-12-01T03:08:15.941Z","updated_at":"2025-10-15T01:42:35.053Z","avatar_url":"https://github.com/krakphp.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Krak Marshal\n\nA library for marshaling data with a functional design. This is useful for transforming/marshaling data for API output, un-marshaling and hydrating serialized data, or for any other types types of transforming.\n\n## Usage\n\n```php\n\u003c?php\n\nuse Krak\\Marshal as m;\n\n$users = getUsers(); // get users from an orm or something\n\n$m = m\\map(function($user)\n{\n    return m\\merge([\n        m\\keys(['id', 'first_name', 'last_name']),\n        function($user) {\n            return [\n                'like_count' =\u003e (int) $user-\u003elike_count,\n                'biography' =\u003e substr($user-\u003ebiography, 0, 64),\n                'created_at_ts' =\u003e $user-\u003ecreated_at-\u003egetTimestamp(),\n            ];\n        }\n    ]);\n});\n\n$marshaled = $m($users);\n\n/*\n[\n    [\n        'id' =\u003e ...,\n        'first_name' =\u003e ...,\n        'last_name' =\u003e ...,\n        'biography' =\u003e ...,\n        'created_at_ts' =\u003e ...,\n    ],\n    ...\n]\n*/\n```\n\n## Accessors\n\nFor certain marshalers, you'll want to marshal an entity in the form of the array or object. For that, we have accessors. An accessor implements the `Krak\\Marshal\\Access` interface.\n\n```php\n\u003c?php\n\ninterface Access {\n    public function get($data, $key, $default = null);\n    public function has($data, $key);\n}\n```\n\nusage:\n\n```php\n\u003c?php\n\n$access = new Krak\\Marshal\\ArrayKeyAccess();\n$data = ['a' =\u003e 1];\n$access-\u003ehas($data, 'a'); // true\n$access-\u003ehas($data, 'b'); // false\n$access-\u003eget($data, 'a'); // 1\n$access-\u003eget($data, 'b', 0); // 0\n```\n\n## Hydrators\n\nHydrators are used to marshal array data into an object. Each hydrator is any callable with the following interface\n\n```php\n\u003c?php\n\ninterface Hydrator {\n    public function __invoke($class, $data);\n}\n```\n\n```php\n\u003c?php\n\nclass MyClass {\n    public $a;\n    public $b;\n}\n\n$hydrate = publicPropertyHydrator();\n$obj = $hydrate(new MyClass(), [\n    'a' =\u003e 1,\n    'b' =\u003e 2,\n]);\n\n// $obj now is hydrated with those values\n```\n\n## API\n\n### hydrate($class, $hydrator = null)\n\nCreates a marshaler that will hydrate the data with the given `$class` parameter and forwards the `$class` and the `$data` from the marshaler to the `$hydrator`. If no hydrator is supplied, the default `hydrator()` will be used.\n\n```php\n\u003c?php\n\nclass MyClass {\n    public $a;\n}\n\n$marshal = Krak\\Marshal\\hydrate(MyClass::class);\n$obj = $marshal(['a' =\u003e 1]);\nassert($obj instanceof MyClass);\n```\n\n### keyMap($map)\n\ntransforms the keys of the data into a new key\n\n### rename(array $map)\n\nrenames key fields into a new name\n\n### only(array $fields)\n\nIt only includes the given fields. Everything else is filtered out.\n\n### except(array $fields)\n\nIt includes all except the given fields. Everything else is kept.\n\n### filter(callable $filter)\n\nFilters the collection via the filter func which has the signature `$filter($value, $key): bool`\n\n### dates($format = 'r')\n\nFormats all instances of `DateTimeInterface` with the given format specifier.\n\n### objectVars()\n\nConverts the properties of an object into an array. This is just an alias of `get_object_vars`.\n\n### typeCast(array $fields, $type)\n\nType casts the given fields into a specific type.\n\n### pipe($marshalers)\n\nCreates a marshaler that pipes the result of one marshaler into the next marshaler\n\n```php\n\u003c?php\n\n$m = pipe([camelizeKeys(), keys(['id', 'firstName'])]);\n$m(['id' =\u003e 1, 'first_name' =\u003e 2]);\n```\n\n### merge($marshalers)\n\nCreates a marshaler that will apply $marshalers onto a value and then merges all of the results with array_merge. This expects the $marshalers to return arrays.\n\n### keys($fields, Access $acc = null)\n\nCreates a marshaler that retuns the fields of the data\n\n### map($marshaler)\n\nCreates a marshaler which takes a collection and returns an array of each of the marshaled items\n\n### collection($marshalers, Access $acc = null)\n\nCreates a marshaler of a collection based off of the collection of marshalers passed in. Each $marshaler in `$key =\u003e $marshaler` will marshal each $value in `$key =\u003e $value` based on the `$key`.\n\n### on($marshalers, Access $acc = null)\n\nSimilar to `collection`, it marshals the fields of the collection based off of the map of marshalers passed in. The only difference is that it updates the fields in the original collection and returns the entire modified collection.\n\n### stringyKeys($cb)\n\nMaps a key by allowing a stringy instance passed to callback for key manipulation\n\n### underscoredKeys()\n\nConverts the keys to underscore style keys using the `Stringy::underscored` function\n\n### camelizeKeys()\n\nConverts the keys to camelCase style keys using the `Stringy::camelize` function\n\n### identity()\n\nCreates a marshaler for the identity function\n\n### mock($val)\n\nCreates a marshaler that returns $val always. This is useful for testing\n\n### notnull($marshaler)\n\nCreates a marshaler that will not allow null values to be passed to the marshaler. if a null value is passed, it just returns null and doesn't call the marshaler\n\n### hydrator()\n\nReturns a statically cached instance of a default hydrator instance.\n\n### classNameHydrator($hydrator)\n\nTreats the `$class` parameter as a class name and will instantiate a class and delgate to the internal hydrator\n\n### publicPropertyHydrator()\n\nAssigns the properties from array into the `$class` object passed in via publicly accessible properties.\n\n### fromXML()\n\nParses XML string into an array.\n\n```php\n\u003c?php\n\n$xml = \u003c\u003c\u003cXML\n\u003c?xml version=\"1.0\" ?\u003e\n\u003croot a=\"1\"\u003e\n    \u003citem\u003eValue\u003c/item\u003e\n    \u003citems\u003e\n        \u003citem\u003e1\u003c/item\u003e\n        \u003citem\u003e2\u003c/item\u003e\n    \u003c/items\u003e\n    \u003cmap id=\"1\"\u003e\n        \u003ca\u003eA\u003c/a\u003e\n        \u003cb\u003eB\u003c/b\u003e\n    \u003c/map\u003e\n    \u003cnode\u003e\n        \u003c![CDATA[\u003ccdata\u003e]]\u003e\n    \u003c/node\u003e\n\u003c/root\u003e\nXML;\n$unmarshal = Krak\\Marshal\\fromXML();\nvar_dump($unmarshal($xml));\n/** Outputs:\narray(5) {\n  [\"@a\"]=\u003e\n  string(1) \"1\"\n  [\"item\"]=\u003e\n  string(5) \"Value\"\n  [\"items\"]=\u003e\n  array(1) {\n    [\"item\"]=\u003e\n    array(2) {\n      [0]=\u003e\n      string(1) \"1\"\n      [1]=\u003e\n      string(1) \"2\"\n    }\n  }\n  [\"map\"]=\u003e\n  array(3) {\n    [\"@id\"]=\u003e\n    string(1) \"1\"\n    [\"a\"]=\u003e\n    string(1) \"A\"\n    [\"b\"]=\u003e\n    string(1) \"B\"\n  }\n  [\"node\"]=\u003e\n  string(7) \"\u003ccdata\u003e\"\n}\n*/\n```\n\n### class ArrayKeyAccess\n\nPerforms access on arrays via the key.\n\n### class ObjectPropertyAccess\n\nPerforms access on object properties via the property name\n\n### class AnyAccess\n\nDelegates access to either `ArrayKeyAccess` or `ObjectPropertyAccess` if the data is an array or not.\n\nThis is the default accessor used.\n\n### access()\n\nReturns a statically cached instance of AnyAccess\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkrakphp%2Fmarshal","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkrakphp%2Fmarshal","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkrakphp%2Fmarshal/lists"}