{"id":16272549,"url":"https://github.com/sc-networks/hydrator","last_synced_at":"2025-03-19T23:31:06.067Z","repository":{"id":57048080,"uuid":"141702635","full_name":"SC-Networks/Hydrator","owner":"SC-Networks","description":"A pragmatic hydrator and extractor library","archived":false,"fork":false,"pushed_at":"2022-01-21T07:15:06.000Z","size":52,"stargazers_count":2,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-17T12:21:53.596Z","etag":null,"topics":["extract","extract-data","extraction","hydrate","hydration","hydrator","php","php7","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/SC-Networks.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":"2018-07-20T11:12:11.000Z","updated_at":"2022-02-12T13:35:01.000Z","dependencies_parsed_at":"2022-08-23T17:50:25.174Z","dependency_job_id":null,"html_url":"https://github.com/SC-Networks/Hydrator","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SC-Networks%2FHydrator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SC-Networks%2FHydrator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SC-Networks%2FHydrator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SC-Networks%2FHydrator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/SC-Networks","download_url":"https://codeload.github.com/SC-Networks/Hydrator/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244524820,"owners_count":20466506,"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":["extract","extract-data","extraction","hydrate","hydration","hydrator","php","php7","php8"],"created_at":"2024-10-10T18:18:13.389Z","updated_at":"2025-03-19T23:31:05.737Z","avatar_url":"https://github.com/SC-Networks.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# scn/hydrator\n\n[![Latest Stable Version](https://poser.pugx.org/scn/hydrator/v/stable)](https://packagist.org/packages/scn/hydrator)\n[![Monthly Downloads](https://poser.pugx.org/scn/hydrator/d/monthly)](https://packagist.org/packages/scn/hydrator)\n[![License](https://poser.pugx.org/scn/hydrator/license)](LICENSE)\n\n## A pragmatic hydrator and extractor library\n\n### Installation\n\nVia Composer:\n```\n$ composer require scn/hydrator\n```\n\n### Usage\n\nSee the `examples` folder for usage instructions by code.\n\n#### Extraction\n\nFirst of all you'll need a class that implements `\\Scn\\Hydrator\\Config\\ExtractorConfigInterface`.\nThe only method `getExtractorProperties` has to return an array with string keys and callables as values.\n\nThe string keys of this array are used to craft the result of the extraction.\n\nThe corresponding callables must have the signature `callable(string $propertyName): mixed`. The `$propertyName`\nparameter will be the corresponding string key. You will usually not need this information.\n\nIf this callable is an instance of `\\Closure`, its' `$this` and `static` context are bound to the entity object to \nextract. As a result of this the closure will have access to private and protected properties and methods of the entity.\n\nA short example:\n\n```php\n\u003c?php\n\nrequire_once 'vendor/autoload.php';\n\nclass ExtractorConfig implements Configuration\\ExtractorConfigInterface\n{\n\n    public function getExtractorProperties(): array\n    {\n        return [\n            'property' =\u003e function (string $propertyName): string {\n                return $this-\u003eprivateProperty; // `$this` will be the entity to extract\n            }\n        ];\n    }\n}\n\nclass Entity\n{\n    private $privateProperty = 'private value';\n}\n\n$hydrator = new \\Hydrator();\n\n$result = $hydrator-\u003eextract(\n    new ExtractorConfig(),\n    new Entity()\n);\n\nvar_dump(assert($result === ['property' =\u003e 'private value'])); // -\u003e bool(true)\n\n```\n\n### Hydration\n\nFor hydration you'll need a class implementing `\\Scn\\Hydrator\\Configuration\\HydratorConfigInterface`.\nThe only method `getHydratorProperties` has to return an array with string keys and callables as values.\n\nAs an alternative you can use the `\\Scn\\Hydrator\\Configuration\\GenericHydratorConfig`.\n\nThe string keys have to correspond the keys in the data to hydrate your object with.\n\nThe corresponding callables must have the signature `callable(mixed $value, string $propertyName): void`.\nThe `$propertyName` parameter will be the corresponding string key. You will usually not need this information.\n\nIf this callable is an instance of `\\Closure`, its' `$this` and `static` context are bound to the entity object to \nhydrate. As a result of this the closure will have access to private and protected properties and methods of the entity.\n\nA short example:\n\n```php\n\u003c?php\n\nrequire_once __DIR__.'vendor/autoload.php';\n\nclass HydratorConfig implements Configuration\\HydratorConfigInterface\n{\n\n    public function getHydratorProperties(): array\n    {\n        return [\n            'property' =\u003e function (string $value, string $propertyName): void {\n                $this-\u003eprivateProperty = $value; // $this will be the entity to hydrate\n            }\n        ];\n    }\n}\n\nclass Entity\n{\n    private $privateProperty = 'private value';\n\n    public function getPropertyValue(): string\n    {\n        return $this-\u003eprivateProperty;\n    }\n}\n\n$hydrator = new \\Hydrator();\n$data = [\n    'property' =\u003e 'hydrated private value',\n];\n\n$entity = new Entity();\n\n$hydrator-\u003ehydrate(\n    new HydratorConfig(), \n    $entity, \n    $data\n);\n\nvar_dump(assert('hydrated private value' === $entity-\u003egetPropertyValue())); // -\u003e bool(true)\n```\n\n#### Hydration flags\n\nThe `hydrate` method has a fourth, optional bit flag parameter `$flags`.\n\nCurrently there are two options available\n\n##### `\\Scn\\Hydrator\\Hydrator::NO_STRICT_KEYS` (1)\n\nIf this bit is set in `$flags`, the hydrator will ignore keys in the data array that have no corresponding key in the array\nthe `getHydratorProperties` method returns.\n\nThis bit is not set by default, additional keys in the data array will lead to an `\\InvalidArgumentException` exception thrown by the hydrator.\n\n##### `\\Scn\\Hydrator\\Hydrator::IGNORE_KEYS` (2)\n\nIf this bit is set, the hydrator will ignore the keys of the data array but assume the entries in the data array are in\nthe same order as in the hydrator configuration array.\n\nExample:\n\n```php\n\u003c?php\n\nrequire_once __DIR__.'vendor/autoload.php';\n\nclass HydratorConfig implements Configuration\\HydratorConfigInterface\n{\n\n    public function getHydratorProperties(): array\n    {\n        return [\n            'property_a' =\u003e function (string $value, string $propertyName): void {\n                $this-\u003eprivatePropertyA = $value;\n            },\n            'property_b' =\u003e function (string $value, string $propertyName): void {\n                $this-\u003eprivatePropertyB = $value;\n            },\n            'property_c' =\u003e function (string $value, string $propertyName): void {\n                $this-\u003eprivatePropertyC = $value;\n            },\n\n        ];\n    }\n}\n\nclass Entity\n{\n    private $privatePropertyA;\n    private $privatePropertyB;\n    private $privatePropertyC;\n\n    public function getPropertyValues(): array\n    {\n        return [\n            $this-\u003eprivatePropertyA,\n            $this-\u003eprivatePropertyB,\n            $this-\u003eprivatePropertyC,\n        ];\n    }\n}\n\n$hydrator = new \\Hydrator();\n$data = ['value a', 'value_b', 'value_c'];\n\n$entity = new Entity();\n\n$hydrator-\u003ehydrate(\n    new HydratorConfig(),\n    $entity,\n    $data,\n    Hydrator::IGNORE_KEYS\n);\n\nvar_dump(assert($data === $entity-\u003egetPropertyValues())); // -\u003e bool(true)\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsc-networks%2Fhydrator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsc-networks%2Fhydrator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsc-networks%2Fhydrator/lists"}