{"id":25015586,"url":"https://github.com/omaralalwi/php-builders","last_synced_at":"2025-07-06T20:38:46.723Z","repository":{"id":272676743,"uuid":"917404875","full_name":"omaralalwi/php-builders","owner":"omaralalwi","description":"sample php traits to add ability to use builder design patterns with easy in PHP applications","archived":false,"fork":false,"pushed_at":"2025-01-16T23:03:02.000Z","size":10,"stargazers_count":13,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-06-17T05:48:42.757Z","etag":null,"topics":["builder-pattern","design-patterns","fluent-interface","object-creation","php","php-builders","php-code","php-code-generator","php-library","php-patterns","traits"],"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/omaralalwi.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","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,"zenodo":null}},"created_at":"2025-01-15T23:13:48.000Z","updated_at":"2025-04-25T00:59:12.000Z","dependencies_parsed_at":null,"dependency_job_id":"c8e98d49-9fd6-4fb2-99a7-d075728f01b9","html_url":"https://github.com/omaralalwi/php-builders","commit_stats":null,"previous_names":["omaralalwi/php-builders"],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/omaralalwi/php-builders","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/omaralalwi%2Fphp-builders","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/omaralalwi%2Fphp-builders/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/omaralalwi%2Fphp-builders/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/omaralalwi%2Fphp-builders/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/omaralalwi","download_url":"https://codeload.github.com/omaralalwi/php-builders/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/omaralalwi%2Fphp-builders/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":263969536,"owners_count":23537446,"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-pattern","design-patterns","fluent-interface","object-creation","php","php-builders","php-code","php-code-generator","php-library","php-patterns","traits"],"created_at":"2025-02-05T08:30:35.640Z","updated_at":"2025-07-06T20:38:46.704Z","avatar_url":"https://github.com/omaralalwi.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# PHP Builders\n\n[![Latest Version](https://img.shields.io/github/release/omaralalwi/php-builders.svg?style=flat-square)](https://github.com/omaralalwi/php-builders/releases)\n[![License](https://img.shields.io/badge/license-MIT-brightgreen.svg)](https://opensource.org/licenses/MIT)\n\n**PHP Builders** is a lightweight PHP library designed to simplify the implementation of the Builder design pattern in PHP applications. \n\n---\n\n## Table of Contents\n\n- [Installation](#installation)\n- [Usage](#usage)\n  - [By Extending `Fluentbuilder` in Builder Class](#by-extending-fluentbuilder-in-builder-class)\n  - [By Include Traits in Builder Class](#by-include-traits)\n  - [Cast Builder Class](#cast-builder-class)\n    - [Return as Array](#return-as-array)\n    - [Return as Object](#return-as-object)\n    - [Return as JSON](#return-as-json)\n- [Custom Execution Logic](#custom-execution-logic)\n- [Contributing](#contributing)\n- [Security](#security)\n- [License](#license)\n\n---\n\n## Installation\n\nInstall the package via Composer using the following command:\n\n```bash\ncomposer require omaralalwi/php-builders\n```\n\n---\n\n## Usage\n\n**Note**: if you not use any PHP frameworks like (symfony,laravel,codeigniter,Yii,cakePHP..etc), you should add this line:\n\n```php\nrequire_once __DIR__ . '/vendor/autoload.php';\n```\n\n**First Way**\n\n### By Extending FluentBuilder in Builder Class\n\nThe `FluentBuilder` class integrates all available traits, enabling you to utilize all package features by simply extending this class.\n\n```php\nnamespace App\\Builders;\n\nuse Omaralalwi\\PhpBuilders\\FluentBuilder;\n\nclass UserBuilder extends FluentBuilder\n{\n    protected string $name;\n    protected string $email;\n\n    public function setName(string $name): self\n    {\n        $this-\u003ename = $name;\n        return $this;\n    }\n\n    public function setEmail(string $email): self\n    {\n        $this-\u003eemail = $email;\n        return $this;\n    }\n\n    public function sendEmail(): self\n    {\n        // Logic for sending an email\n        return $this;\n    }\n}\n```\n\n**second Way**\n\n### By Include Traits\n\nsimplify you can achieve same result by include `Buildable`, `Arrayable`, `Objectable`, `Jsonable` traits directly in builder class, without extending the `FluentBuilder` class.\n\n```php\nnamespace App\\Builders;\n\nuse Omaralalwi\\PhpBuilders\\Traits\\{Buildable, Arrayable, Objectable, Jsonable};\nuse App\\Models\\User;\n\nclass UserBuilder\n{\n   use Buildable,\n        Arrayable,\n        Objectable,\n        Jsonable;\n        \n        // same code in first way example\n}\n```\n\n### cast Builder Class\n\nuse `UserBuilder` as following:\n\n```php\n$user = UserBuilder::build()\n    -\u003esetName('PHP Builders')\n    -\u003esetEmail('hello@phpbuilders.test')\n    -\u003esendEmail();\n```\nthen cast it to needed type as following:\n\n**Return as Array**\n\nConvert the built instance to an array using the `toArray()` method:\n\n```php\n$userAsArray = $user-\u003etoArray();\nprint_r($userAsArray);\n/*\nOutput:\nArray\n(\n    [name] =\u003e PHP Builders\n    [email] =\u003e hello@phpbuilders.test\n)\n*/\n```\n\n**Return as Object**\n\nConvert the built instance to an object using the `toObject()` method:\n\n```php\n$userAsObject = $user-\u003etoObject();\nprint_r($userAsObject);\n/*\nOutput:\nstdClass Object\n(\n    [name] =\u003e PHP Builders\n    [email] =\u003e hello@phpbuilders.test\n)\n*/\n```\n\n**Return as JSON**\n\nConvert the built instance to JSON using the `toJson()` method:\n\n```php\n$userAsJson = $user-\u003etoJson();\necho $userAsJson;\n/*\nOutput:\n{\"name\":\"PHP Builders\",\"email\":\"hello@phpbuilders.test\"}\n*/\n```\n\n---\n\n#### Custom Execution Logic\n\n```php\nnamespace App\\Builders;\n\nuse Omaralalwi\\PhpBuilders\\FluentBuilder;\nuse App\\Models\\User;\n\nclass UserBuilder extends FluentBuilder\n{\n   // same code in previous example, just we added execute method .\n   \n    public function execute(): User\n    {\n        // Pre-store logic (e.g., validation, preprocessing)\n        return $this-\u003estore();\n    }\n\n    protected function store(): User\n    {\n        return User::create([\n            'name' =\u003e $this-\u003ename,\n            'email' =\u003e $this-\u003eemail,\n        ]);\n    }\n}\n```\n\n```php\n$createdUser = UserBuilder::build()\n    -\u003esetName('PHP Builders')\n    -\u003esetEmail('hello@phpbuilders.test')\n    -\u003eexecute();\n/*\n$createdUser is an instance of the User model.\n*/\n```\n\n\u003e **Note:** Traits like `Arrayable`, `Objectable`, and `Jsonable` can also be included in your builder class as needed if you choose not to extend `FluentBuilder`.\n\n---\n\n## Contributing\n\nContributions are welcome! To propose improvements or report issues, please open an issue or submit a pull request on the [GitHub repository](https://github.com/omaralalwi/php-builders).\n\n---\n\n## Security\n\nIf you discover any security-related issues, please email the author at: `omaralwi2010@gmail.com`.\n\n---\n\n## License\n\nThis project is open-source and licensed under the [MIT License](LICENSE).\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fomaralalwi%2Fphp-builders","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fomaralalwi%2Fphp-builders","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fomaralalwi%2Fphp-builders/lists"}