You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
**PHP Builders** is a sample PHP library that provides traits to implement the Builder design pattern easily in PHP applications. It allows developers to create builder classes that can construct complex objects in a fluent and readable manner.
6
+
**PHP Builders** is a lightweight PHP library designed to simplify the implementation of the Builder design pattern in PHP applications.
7
+
8
+
---
7
9
8
10
## Table of Contents
9
11
10
12
-[Installation](#installation)
11
13
-[Usage](#usage)
12
-
-[Creating a Builder Class](#creating-a-builder-class)
13
-
-[Using the Builder](#using-the-builder)
14
+
-[By Extending `Fluentbuilder` in Builder Class](#by-extending-fluentbuilder-in-builder-class)
15
+
-[By Include Traits in Builder Class](#by-include-traits)
You can install the package via Composer. Run the following command in your terminal:
29
+
Install the package via Composer using the following command:
21
30
22
31
```bash
23
32
composer require omaralalwi/php-builders
24
33
```
25
34
35
+
---
36
+
26
37
## Usage
27
38
28
-
### Creating a Builder Class
39
+
**Note**: if you not use any PHP frameworks like (symfony,laravel,codeigniter,Yii,cakePHP..etc), you should add this line:
40
+
41
+
```php
42
+
require_once __DIR__ . '/vendor/autoload.php';
43
+
```
44
+
45
+
**First Way**
46
+
47
+
### By Extending FluentBuilder in Builder Class
29
48
30
-
To create a builder class, extend the `FluentBuilder` class and define your properties and methods.
49
+
The `FluentBuilder` class integrates all available traits, enabling you to utilize all package features by simply extending this class.
31
50
32
51
```php
33
52
namespace App\Builders;
@@ -38,7 +57,7 @@ class UserBuilder extends FluentBuilder
38
57
{
39
58
protected string $name;
40
59
protected string $email;
41
-
60
+
42
61
public function setName(string $name): self
43
62
{
44
63
$this->name = $name;
@@ -50,58 +69,154 @@ class UserBuilder extends FluentBuilder
50
69
$this->email = $email;
51
70
return $this;
52
71
}
53
-
72
+
54
73
public function sendEmail(): self
55
74
{
56
-
// handle send email to user or make any action
57
-
// then return instance of self
58
-
return $this;
75
+
// Logic for sending an email
76
+
return $this;
59
77
}
60
-
61
-
// add any another functions then call them as you need
62
78
}
63
79
```
64
80
65
-
### Using the Builder
81
+
**second Way**
66
82
67
-
You can convert the built instance to an array or an object using the `toArray()` and `toObject()` methods.
83
+
### By Include Traits
68
84
69
-
#### Return Array
85
+
simplify you can achieve same result by include `Buildable`, `Arrayable`, `Objectable`, `Jsonable` traits directly in builder class, without extending the `FluentBuilder` class.
> **Note:** Traits like `Arrayable`, `Objectable`, and `Jsonable` can also be included in your builder class as needed if you choose not to extend `FluentBuilder`.
204
+
205
+
---
206
+
97
207
## Contributing
98
208
99
-
Contributions are welcome! If you have suggestions for improvements or new features, feel free to open an issue or submit a pull request on the [GitHub repository](https://github.com/omaralalwi/php-builders).
209
+
Contributions 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).
210
+
211
+
---
100
212
101
213
## Security
102
214
103
-
If you discover any security-related issues, please email creator : `[email protected]`.
215
+
If you discover any security-related issues, please email the author at: `[email protected]`.
216
+
217
+
---
104
218
105
219
## License
106
220
107
-
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
221
+
This project is open-source and licensed under the [MIT License](LICENSE).
0 commit comments