{"id":19818987,"url":"https://github.com/ekvedaras/class-factory","last_synced_at":"2025-05-01T11:32:35.054Z","repository":{"id":58250840,"uuid":"530759758","full_name":"ekvedaras/class-factory","owner":"ekvedaras","description":"🏭 Factory for creating objects via constructor arguments","archived":false,"fork":false,"pushed_at":"2024-07-08T22:34:06.000Z","size":80,"stargazers_count":5,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-08-09T05:14:07.548Z","etag":null,"topics":["class","entities","factory","php","value-objects"],"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/ekvedaras.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.md","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-08-30T17:25:48.000Z","updated_at":"2024-07-08T22:34:03.000Z","dependencies_parsed_at":"2024-07-09T03:03:17.104Z","dependency_job_id":"09547ab7-d7e2-40a7-9e45-5f1341e91069","html_url":"https://github.com/ekvedaras/class-factory","commit_stats":{"total_commits":38,"total_committers":4,"mean_commits":9.5,"dds":"0.23684210526315785","last_synced_commit":"b06fe5ccfc9c61015031ecfc471ceee2bc6f339a"},"previous_names":[],"tags_count":10,"template":false,"template_full_name":"spatie/package-skeleton-php","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ekvedaras%2Fclass-factory","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ekvedaras%2Fclass-factory/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ekvedaras%2Fclass-factory/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ekvedaras%2Fclass-factory/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ekvedaras","download_url":"https://codeload.github.com/ekvedaras/class-factory/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224253281,"owners_count":17280936,"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":["class","entities","factory","php","value-objects"],"created_at":"2024-11-12T10:17:29.458Z","updated_at":"2025-05-01T11:32:35.048Z","avatar_url":"https://github.com/ekvedaras.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Factory for creating objects via constructor arguments\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/ekvedaras/class-factory.svg?style=flat-square)](https://packagist.org/packages/ekvedaras/class-factory)\n[![Tests](https://github.com/ekvedaras/class-factory/actions/workflows/run-tests.yml/badge.svg?branch=main)](https://github.com/ekvedaras/class-factory/actions/workflows/run-tests.yml)\n[![Total Downloads](https://img.shields.io/packagist/dt/ekvedaras/class-factory.svg?style=flat-square)](https://packagist.org/packages/ekvedaras/class-factory)\n\nA factory class that passes each property directly to constructor.\nThis way your class does not need to deal with received array to create itself from and there is no reflection magic involved.\nThis is mostly useful for creating plain classes like value objects, entities, DTOs, etc.\n\n## Installation\n\nYou can install the package via composer:\n\n```bash\ncomposer require --dev ekvedaras/class-factory\n```\n\n## PhpStorm plugin\n\nProvides autocomplete and refactoring capabillities for PhpStorm.\n\n* Plugin: [ClassFactory](https://plugins.jetbrains.com/plugin/19824-classfactory)\n* Repository: [class-factory-phpstorm](https://github.com/ekvedaras/class-factory-phpstorm)\n\n## Usage\n\n```php\nuse EKvedaras\\ClassFactory\\ClassFactory;\nuse EKvedaras\\ClassFactory\\ClosureValue;\n\nclass Account {\n    public function __construct(\n        public readonly int $id,\n        public string $name,\n        public array $orders,\n        public \\Closure $monitor,\n    ) {\n    }\n}\n\n/** @extends ClassFactory\u003cAccount\u003e */\nclass AccountFactory extends ClassFactory {\n    protected string $class = Account::class;\n    \n    protected function definition(): array\n    {\n        return [\n            'id' =\u003e 1,\n            'name' =\u003e 'John Doe',\n            'orders' =\u003e [\n                OrderFactory::new()-\u003estate(['id' =\u003e 1]),\n                OrderFactory::new()-\u003estate(['id' =\u003e 2]),\n            ],\n            'monitor' =\u003e new ClosureValue(fn () =\u003e true),\n        ];\n    }\n\n    public function johnSmith(): static\n    {\n        return $this-\u003estate([\n            'id' =\u003e 2,\n            'name' =\u003e 'John Smith',\n        ]);\n    }\n}\n\n$account = AccountFactory::new()\n    -\u003ejohnSmith()                                                           // Can use predefiened states\n    -\u003estate(['name' =\u003e 'John Smitgh Jnr'])                                  // Can override factory state on the fly\n    -\u003estate(['name' =\u003e fn (array $attributes) =\u003e \"{$attributes['name']}.\"]) // Can use closures and have access to already defined attributes\n    -\u003estate(function (array $attributes) {                                  // Can use closures and modify state of multiple attributes by returning an array\n        $id = $attributes['id'] + 1;    \n        \n        return ['id' =\u003e $id];\n    })\n    -\u003eafter(fn (Account $account) =\u003e sort($account-\u003eorders))                // Can modify constructed object after it was created\n    -\u003estate(['monitor' =\u003e new ClosureValue(fn () =\u003e false)])                // Can set state of closure type properties using `ClosureValue` wrapper\n    -\u003emake(['id' =\u003e 3])                                                     // Can provide final modifications and return the new object\n```\n\n### Customising class creation\n\nIf you don't want class to be created by directly passing attributes to constructor, you can override `newInstance` method in the factory and do change the behavior.\n\n```php\nprotected function newInstance(array $properties): object\n{\n    return Account::makeUsingProperties($properties);\n}\n```\n\n## Testing\n\n```bash\ncomposer test\n```\n\n## Changelog\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.\n\n## Contributing\n\nPlease see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details.\n\n## Security Vulnerabilities\n\nPlease review [our security policy](../../security/policy) on how to report security vulnerabilities.\n\n## Credits\n\n- [Ernestas Kvedaras](https://github.com/ekvedaras)\n- [All Contributors](../../contributors)\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fekvedaras%2Fclass-factory","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fekvedaras%2Fclass-factory","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fekvedaras%2Fclass-factory/lists"}