{"id":18929522,"url":"https://github.com/thecodingmachine/fluid-hydrator","last_synced_at":"2025-04-15T15:31:05.463Z","repository":{"id":57067866,"uuid":"95011817","full_name":"thecodingmachine/fluid-hydrator","owner":"thecodingmachine","description":null,"archived":false,"fork":false,"pushed_at":"2018-11-06T16:55:24.000Z","size":21,"stargazers_count":3,"open_issues_count":3,"forks_count":3,"subscribers_count":1,"default_branch":"1.x","last_synced_at":"2025-04-11T18:59:47.076Z","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":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/thecodingmachine.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-06-21T14:16:16.000Z","updated_at":"2018-11-06T16:55:26.000Z","dependencies_parsed_at":"2022-08-24T14:54:08.177Z","dependency_job_id":null,"html_url":"https://github.com/thecodingmachine/fluid-hydrator","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/thecodingmachine%2Ffluid-hydrator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thecodingmachine%2Ffluid-hydrator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thecodingmachine%2Ffluid-hydrator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thecodingmachine%2Ffluid-hydrator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/thecodingmachine","download_url":"https://codeload.github.com/thecodingmachine/fluid-hydrator/tar.gz/refs/heads/1.x","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249097869,"owners_count":21212364,"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-08T11:33:24.242Z","updated_at":"2025-04-15T15:31:05.195Z","avatar_url":"https://github.com/thecodingmachine.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# FluidHydrator\n\n*This package is based on [thecodingmachine/metahydrator](https://packagist.org/packages/thecodingmachine/metahydrator), and\naims to ease instantiation code (ie when not using dependency injection). In order to do so, it follows fluent design pattern.*\n\n## How to use\n\nAs you have probably deduced by now, the main class of this package is `FluidHydrator`. It implements interface `Hydrator`\n(refer to [mouf/tdbm-hydrator](https://packagist.org/packages/mouf/tdbm-hydrator) for more details).\n\n```php\n$hydrator = new FluidHydrator;\n```\n\n### Primitive types\nUse method `field` to declare a primitive field. Then, declare its type using `int()`, `bool()`, `string()`, etc.\n```php\n$hydrator-\u003efield('foo')-\u003eint();\n```\nTo declare an unstructured array field (typically some decoded JSON), use the `simpleArray` function.\n```php\n$hydrator-\u003efield('foo')-\u003esimpleArray();\n```\n\nAs we are these can be chained as following:\n```php\n$hydrator\n    -\u003efield('foo')-\u003estring()-\u003ethen()\n    -\u003efield('bar')-\u003eint() // Note that call o method then() is optional!\n    -\u003efield('baz')-\u003efloat()\n;\n```\nA type method leads to a state where you may add options to the field, mostly validators.\n```php\n$hydrator\n    -\u003efield('foo')-\u003estring()-\u003erequired()-\u003emaxLength(55)\n;\n```\n\n### Array\nOption `array()` allows to change the current type from T to array\u003cT\u003e, where current validators are used for validating\neach entry. These can be used multiple times.\n```php\n$hydrator\n    // 'foo' must be an array of non-empty arrays containing non-empty strings of length inferior to 55\n    -\u003efield('foo')-\u003estring()-\u003erequired()-\u003emaxLength(55)-\u003earray()-\u003erequired()-\u003earray()\n;\n```\n\n\n\n### Object types\nYou may also use a non-primitive type (ie a class) for a `field()`. Method `object()` then needs you to specify\nthe hydrator used to parse the sub-data.\nYou may pass an existant hydrator using method `hydrator()`\n```php\n$hydrator\n    -\u003efield('garply')-\u003eobject(Garply::class)-\u003ehydrator($garplyHydrator)\n;\n```\nor even use default hydrator (being TDBMHydrator), if you do not wish to check the data sanity\n```php\n$hydrator\n    -\u003efield('garply')-\u003eobject(Garply::class)-\u003ehydrator()\n;\n```\nIf you want to declare the sub-hydrator on the fly, you can: declare it between a `begin()` and a `end()`\n```php\n$hydrator\n    -\u003efield('garply')-\u003eobject(Garply::class)\n    -\u003ebegin()\n        -\u003efield('qux')-\u003estring()\n        -\u003efield('quux')-\u003ebool()\n    -\u003eend()\n;\n```\nNote that after `end()` or `hydrator()`, you are in the same state as when typing a primitive field. Therefore, you can\nadd validators, and even switch from T to T[] with `array()`!\n\n### Sub-Object\nIn some cases, you will want to access sub-objects from the top-level value being hydrated. This way, the existing value\nwill not be replaced, but directly hydrated, respecting references and allowing partial edition.\n\nThe field type you're looking for here is `subobject()`; the writing is substantially similar to `object()`, except for\nthe option `array()` that you should not used, since it's not (yet) supported.\n```php\n$hydrator\n    -\u003efield('garply')-\u003esubobject(Garply::class)\n    -\u003ebegin()\n        -\u003efield('qux')-\u003estring()\n        -\u003efield('quux')-\u003ebool()\n    -\u003eend()-\u003erequired()\n;\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthecodingmachine%2Ffluid-hydrator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthecodingmachine%2Ffluid-hydrator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthecodingmachine%2Ffluid-hydrator/lists"}