PHP Builders is a lightweight PHP library designed to simplify the implementation of the Builder design pattern in PHP applications.
Install the package via Composer using the following command:
composer require omaralalwi/php-builders
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
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
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
}
use UserBuilder
as following:
$user = UserBuilder::build()
->setName('PHP Builders')
->setEmail('[email protected]')
->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] => [email protected]
)
*/
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] => [email protected]
)
*/
Return as JSON
Convert the built instance to JSON using the toJson()
method:
$userAsJson = $user->toJson();
echo $userAsJson;
/*
Output:
{"name":"PHP Builders","email":"[email protected]"}
*/
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('[email protected]')
->execute();
/*
$createdUser is an instance of the User model.
*/
Note: Traits like
Arrayable
,Objectable
, andJsonable
can also be included in your builder class as needed if you choose not to extendFluentBuilder
.
Contributions are welcome! To propose improvements or report issues, please open an issue or submit a pull request on the GitHub repository.
If you discover any security-related issues, please email the author at: [email protected]
.
This project is open-source and licensed under the MIT License.