{"id":18780257,"url":"https://github.com/nutgram/hydrator","last_synced_at":"2025-10-04T15:05:10.208Z","repository":{"id":40661352,"uuid":"484187342","full_name":"nutgram/hydrator","owner":"nutgram","description":"A turbocharged json object hydrator for your DTOs","archived":false,"fork":false,"pushed_at":"2025-03-31T23:08:00.000Z","size":166,"stargazers_count":11,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-01T00:22:04.889Z","etag":null,"topics":["data-transfer-object","dto","enum","hacktoberfest","hydrator","json","jsonmapper","mapper","php","php8"],"latest_commit_sha":null,"homepage":"","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/nutgram.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,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-04-21T19:57:55.000Z","updated_at":"2025-03-29T13:19:34.000Z","dependencies_parsed_at":"2023-12-30T19:25:46.737Z","dependency_job_id":"586f813d-1868-4c43-9472-a65f3e5a2911","html_url":"https://github.com/nutgram/hydrator","commit_stats":{"total_commits":32,"total_committers":4,"mean_commits":8.0,"dds":0.40625,"last_synced_commit":"20ca4fe2f4993c97f67b029a6949e9ea756b072f"},"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nutgram%2Fhydrator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nutgram%2Fhydrator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nutgram%2Fhydrator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nutgram%2Fhydrator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nutgram","download_url":"https://codeload.github.com/nutgram/hydrator/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248705497,"owners_count":21148548,"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":["data-transfer-object","dto","enum","hacktoberfest","hydrator","json","jsonmapper","mapper","php","php8"],"created_at":"2024-11-07T20:25:31.058Z","updated_at":"2025-10-04T15:05:05.163Z","avatar_url":"https://github.com/nutgram.png","language":"PHP","readme":"# Strongly typed hydrator for PHP 8.0+\n\n---\n\nFork of the original project https://github.com/sunrise-php/hydrator.\n\n## Installation\n\n```bash\ncomposer require nutgram/hydrator\n```\n\n## How to use?\n\n```php\nuse SergiX44\\Hydrator\\Hydrator;\n\n$hydrator = new Hydrator();\n\n// create and hydrate an object with an array\n$data = [/* the class props here */];\n$object = $hydrator-\u003ehydrate(SomeDto::class, $data);\n\n// hydrate an object with an array\n$data = [/* the class props here */];\n$hydrator-\u003ehydrate($object, $data);\n\n// creates and hydrate an object with JSON\n$json = '{...}';\n$object = $hydrator-\u003ehydrateWithJson(SomeDto::class, $json);\n\n// hydrate an object with JSON\n$json = '{...}';\n$hydrator-\u003ehydrateWithJson($object, $json);\n\n// pass JSON decoding flags\n$options = JSON_OBJECT_AS_ARRAY|JSON_BIGINT_AS_STRING;\n$hydrator-\u003ehydrateWithJson($object, $json, $options);\n```\n\n## Allowed property types\n\n### Required\n\nIf a property has no a default value, then the property is required.\n\n```php\npublic string $value;\n```\n\n### Optional\n\nIf a property has a default value, then the property is optional.\n\n```php\npublic string $value = 'foo';\n```\n\n### Null\n\nIf a property is nullable, then the property can accept null.\n\n```php\npublic ?string $value;\n```\n\nIf the property should be optional, then it must has a default value.\n\n```php\npublic ?string $value = null;\n```\n\n### Boolean\n\nAccepts the following values: true, false, 1, 0, \"1\", \"0\", \"yes\", \"no\", \"on\" and \"no\".\n\n```php\npublic bool $value;\n```\n\n```php\n['value' =\u003e true];\n['value' =\u003e 'yes'];\n```\n\n## Integer\n\nAccepts only integers (also as a string).\n\n```php\npublic int $value;\n```\n\n```php\n['value' =\u003e 42];\n['value' =\u003e '42'];\n```\n\n## Number\u003cint|float\u003e\n\nAccepts only numbers (also as a string).\n\n```php\npublic float $value;\n```\n\n```php\n['value' =\u003e 42.0];\n['value' =\u003e '42.0'];\n```\n\n## String\n\nAccepts only strings.\n\n```php\npublic string $value;\n```\n\n```php\n['value' =\u003e 'foo'];\n```\n\n## Array\u003carray-key, mixed\u003e\n\nAccepts only arrays.\n\n```php\npublic array $value;\n```\n\n```php\n['value' =\u003e [1, 2, 'foo']];\n```\n\n## Array\u003carray-key, class\u003e\n\nAccept a list of objects.\n\n```php\nfinal class SomeDto {\n    public readonly string $value;\n}\n```\n\n```php\nuse SergiX44\\Hydrator\\Annotation\\ArrayType;\n\n#[ArrayType(SomeDto::class)]\npublic array $value;\n```\n\n```php\n[\n    'value' =\u003e [\n        [\n            'value' =\u003e 'foo',\n        ],\n        [\n            'value' =\u003e 'bar',\n        ],\n    ],\n],\n```\n\n## Object\n\nAccepts only objects.\n\n```php\npublic object $value;\n```\n\n```php\n['value' =\u003e new stdClass];\n```\n\n## DateTime/DateTimeImmutable\n\nIntegers (also as a string) will be handled as a timestamp, otherwise accepts only valid date-time strings.\n\n```php\npublic DateTimeImmutable $value;\n```\n\n```php\n// 2010-01-01\n['value' =\u003e 1262304000];\n// 2010-01-01\n['value' =\u003e '1262304000'];\n// normal date\n['value' =\u003e '2010-01-01'];\n```\n\n## DateInterval\n\nAccepts only valid date-interval strings based on ISO 8601.\n\n```php\npublic DateInterval $value;\n```\n\n```php\n['value' =\u003e 'P1Y']\n```\n\n## Enum\u003cBackedEnum\u003e\n\nAccepts only values that exist in an enum.\n\n```php\nenum SomeEnum: int {\n    case foo = 0;\n    case bar = 1;\n}\n```\n\n```php\npublic SomeEnum $value;\n```\n\n```php\n['value' =\u003e 0]\n['value' =\u003e '1']\n```\n\n## Association\n\nAccepts a valid structure for an association\n\n```php\nfinal class SomeDto {\n    public string $value;\n}\n```\n\n```php\npublic SomeDto $value;\n```\n\n```php\n[\n    'value' =\u003e [\n        'value' =\u003e 'foo',\n    ],\n]\n```\n\n## Property alias\n\nIf you need to get a non-normalized key, use aliases.\n\nFor example, the Google Recaptcha API returns the following response:\n\n```json\n{\n    \"success\": false,\n    \"error-codes\": []\n}\n```\n\nTo correctly map the response, use the following model:\n\n```php\nuse SergiX44\\Hydrator\\Annotation\\Alias;\n\nfinal class RecaptchaVerificationResult {\n    public bool $success;\n\n    #[Alias('error-codes')]\n    public array $errorCodes = [];\n}\n```\n\n## Examples\n\n```php\nfinal class Product {\n    public string $name;\n    public Category $category;\n    #[ArrayType(Tag::class)]\n    public array $tags;\n    public Status $status;\n}\n\nfinal class Category {\n    public string $name;\n}\n\nfinal class Tag {\n    public string $name;\n}\n\nenum Status: int {\n    case ENABLED = 1;\n    case DISABLED = 0;\n}\n```\n\n```php\n$product = $hydrator-\u003ehydrate(Product::class, [\n    'name' =\u003e 'Stool',\n    'category' =\u003e [\n        'name' =\u003e 'Furniture',\n    ],\n    'tags' =\u003e [\n        [\n            'name' =\u003e 'Wood',\n        ],\n        [\n            'name' =\u003e 'Lacquered',\n        ],\n    ],\n    'status' =\u003e 0,\n]);\n```\n\n---\n\n## Test run\n\n```bash\ncomposer test\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnutgram%2Fhydrator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnutgram%2Fhydrator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnutgram%2Fhydrator/lists"}