{"id":16101732,"url":"https://github.com/kelunik/builders","last_synced_at":"2025-03-18T08:30:37.943Z","repository":{"id":57005446,"uuid":"165147992","full_name":"kelunik/builders","owner":"kelunik","description":"Generating builders for value objects.","archived":false,"fork":false,"pushed_at":"2019-07-05T20:42:10.000Z","size":22,"stargazers_count":5,"open_issues_count":0,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-10-10T18:50:51.750Z","etag":null,"topics":["builder","builder-generator","php","php-code-generator"],"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/kelunik.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":"2019-01-10T23:45:27.000Z","updated_at":"2024-07-16T07:17:37.000Z","dependencies_parsed_at":"2022-08-21T14:30:26.369Z","dependency_job_id":null,"html_url":"https://github.com/kelunik/builders","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kelunik%2Fbuilders","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kelunik%2Fbuilders/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kelunik%2Fbuilders/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kelunik%2Fbuilders/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kelunik","download_url":"https://codeload.github.com/kelunik/builders/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":221708921,"owners_count":16867651,"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":["builder","builder-generator","php","php-code-generator"],"created_at":"2024-10-09T18:50:50.819Z","updated_at":"2024-10-27T17:07:09.204Z","avatar_url":"https://github.com/kelunik.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# builders\n\nThis packages allows generating builders for value objects automatically. One use case is creating test objects for unit and integration tests.\n\n## Installation\n\n```\ncomposer require kelunik/builders\n```\n\n## Usage\n\nGiven the following `User` object, the builder generator will generate a `UserBuilderMethods` class.\n\n```php\n\u003c?php\n\nnamespace Example;\n\nclass User\n{\n    /** @var int|null */\n    private $id;\n    /** @var string|null */\n    private $name;\n\n    public function getId(): ?int\n    {\n        return $this-\u003eid;\n    }\n\n    public function setId(?string $id): void\n    {\n        $this-\u003eid = $id;\n    }\n\n    public function getName(): ?string\n    {\n        return $this-\u003ename;\n    }\n\n    public function setName(?string $name): void\n    {\n        $this-\u003ename = $name;\n    }\n}\n```\n\n```php\n\u003c?php\n\nnamespace Example;\n\nuse Example;\n\nclass UserBuilderMethods implements \\Kelunik\\Builders\\Builder\n{\n    private $entity;\n\n    public function __construct()\n    {\n        $this-\u003eentity = new Example\\User;\n    }\n\n    final public function withId(?string $value)\n    {\n        $this-\u003eentity-\u003esetId($value);\n\n        return $this;\n    }\n\n    final public function withName(?string $value)\n    {\n        $this-\u003eentity-\u003esetName($value);\n\n        return $this;\n    }\n\n    final public function build(): Example\\User\n    {\n        return $this-\u003eentity;\n    }\n}\n```\n\n### Custom Builder Methods\n\nIn order to use the generated classes, you're advised to create a `UserBuilder` object that extends the generated `UserBuilderMethods`.\nThis separation allow you to add custom builder methods without affecting the builder generator when it throws away the old `UserBuilderMethods` and generates a new one.\n\n```php\n\u003c?php\n\nnamespace Example;\n\nclass UserBuilder extends UserBuilderMethods\n{\n    public function root()\n    {\n        return $this-\u003ewithId(1)-\u003ewithName('root');\n    }\n}\n```\n\n### Builder Consumption\n\nBuilding test objects is easiest if you add a `builders.php` defining functions as shortcuts.\n\n```php\n\u003c?php\n\nnamespace Example;\n\nfunction user() {\n    return new UserBuilder;\n}\n```\n\n```php\n\u003c?php\n\nuse Example\\User;\nuse function Example\\user;\n\nrequire __DIR__ . '/../vendor/autoload.php';\n\n$user = a(user()-\u003eroot()-\u003ewithName('kelunik'));\n\nvar_dump($user instanceof User);\nvar_dump($user);\n```\n\n### Code Generation\n\nThe code generator can be invoked with the following command, paths are relative to the composer root directory:\n\n```\nvendor/bin/builder-generator App\\NamespaceOfValueObjects src src-generated\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkelunik%2Fbuilders","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkelunik%2Fbuilders","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkelunik%2Fbuilders/lists"}