omaralalwi/php-builders
Composer 安装命令:
composer require omaralalwi/php-builders
包简介
sample php traits to add ability to use builder design patterns with easy in PHP applications
关键字:
README 文档
README
PHP Builders is a lightweight PHP library designed to simplify the implementation of the Builder design pattern in PHP applications.
Table of Contents
Installation
Install the package via Composer using the following command:
composer require omaralalwi/php-builders
Usage
Note: if you not use any PHP frameworks like (symfony,laravel,codeigniter,Yii,cakePHP..etc), you should add this line:
require_once __DIR__ . '/vendor/autoload.php';
First Way
By Extending FluentBuilder in Builder Class
The FluentBuilder class integrates all available traits, enabling you to utilize all package features by simply extending this class.
namespace App\Builders; use Omaralalwi\PhpBuilders\FluentBuilder; class UserBuilder extends FluentBuilder { protected string $name; protected string $email; public function setName(string $name): self { $this->name = $name; return $this; } public function setEmail(string $email): self { $this->email = $email; return $this; } public function sendEmail(): self { // Logic for sending an email return $this; } }
second Way
By Include Traits
simplify you can achieve same result by include Buildable, Arrayable, Objectable, Jsonable traits directly in builder class, without extending the FluentBuilder class.
namespace App\Builders; use Omaralalwi\PhpBuilders\Traits\{Buildable, Arrayable, Objectable, Jsonable}; use App\Models\User; class UserBuilder { use Buildable, Arrayable, Objectable, Jsonable; // same code in first way example }
cast Builder Class
use UserBuilder as following:
$user = UserBuilder::build() ->setName('PHP Builders') ->setEmail('hello@phpbuilders.test') ->sendEmail();
then cast it to needed type as following:
Return as Array
Convert the built instance to an array using the toArray() method:
$userAsArray = $user->toArray(); print_r($userAsArray); /* Output: Array ( [name] => PHP Builders [email] => hello@phpbuilders.test ) */
Return as Object
Convert the built instance to an object using the toObject() method:
$userAsObject = $user->toObject(); print_r($userAsObject); /* Output: stdClass Object ( [name] => PHP Builders [email] => hello@phpbuilders.test ) */
Return as JSON
Convert the built instance to JSON using the toJson() method:
$userAsJson = $user->toJson(); echo $userAsJson; /* Output: {"name":"PHP Builders","email":"hello@phpbuilders.test"} */
Custom Execution Logic
namespace App\Builders; use Omaralalwi\PhpBuilders\FluentBuilder; use App\Models\User; class UserBuilder extends FluentBuilder { // same code in previous example, just we added execute method . public function execute(): User { // Pre-store logic (e.g., validation, preprocessing) return $this->store(); } protected function store(): User { return User::create([ 'name' => $this->name, 'email' => $this->email, ]); } }
$createdUser = UserBuilder::build() ->setName('PHP Builders') ->setEmail('hello@phpbuilders.test') ->execute(); /* $createdUser is an instance of the User model. */
Note: Traits like
Arrayable,Objectable, andJsonablecan also be included in your builder class as needed if you choose not to extendFluentBuilder.
Contributing
Contributions are welcome! To propose improvements or report issues, please open an issue or submit a pull request on the GitHub repository.
Security
If you discover any security-related issues, please email the author at: omaralwi2010@gmail.com.
License
This project is open-source and licensed under the MIT License.
omaralalwi/php-builders 适用场景与选型建议
omaralalwi/php-builders 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.3k 次下载、GitHub Stars 达 15, 最近一次更新时间为 2025 年 01 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「traits」 「builders」 「fluent-interface」 「design-patterns」 「builder-pattern」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 omaralalwi/php-builders 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 omaralalwi/php-builders 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 omaralalwi/php-builders 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Model mapping, unmapping and validation
Various traits related to xWP packages
Various traits related to xWP packages
A trait for Laravel models which have many users with roles.
Traits gathering fluent syntax common methods
The Opulence query builder library
统计信息
- 总下载量: 1.3k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 15
- 点击次数: 19
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-01-15