Skip to content

Commit 83427ad

Browse files
committed
first init
0 parents  commit 83427ad

File tree

9 files changed

+303
-0
lines changed

9 files changed

+303
-0
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Changelog
2+
3+
All notable changes to `php-builders` will be documented in this file
4+
5+
## 1.0.0 - 201X-XX-XX
6+
7+
- initial release

CONTRIBUTING.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Contributing
2+
3+
Contributions are **welcome** and will be fully **credited**.
4+
5+
Please read and understand the contribution guide before creating an issue or pull request.
6+
7+
## Etiquette
8+
9+
This project is open source, and as such, the maintainers give their free time to build and maintain the source code
10+
held within. They make the code freely available in the hope that it will be of use to other developers. It would be
11+
extremely unfair for them to suffer abuse or anger for their hard work.
12+
13+
Please be considerate towards maintainers when raising issues or presenting pull requests. Let's show the
14+
world that developers are civilized and selfless people.
15+
16+
It's the duty of the maintainer to ensure that all submissions to the project are of sufficient
17+
quality to benefit the project. Many developers have different skillsets, strengths, and weaknesses. Respect the maintainer's decision, and do not be upset or abusive if your submission is not used.
18+
19+
## Viability
20+
21+
When requesting or submitting new features, first consider whether it might be useful to others. Open
22+
source projects are used by many developers, who may have entirely different needs to your own. Think about
23+
whether or not your feature is likely to be used by other users of the project.
24+
25+
## Procedure
26+
27+
Before filing an issue:
28+
29+
- Attempt to replicate the problem, to ensure that it wasn't a coincidental incident.
30+
- Check to make sure your feature suggestion isn't already present within the project.
31+
- Check the pull requests tab to ensure that the bug doesn't have a fix in progress.
32+
- Check the pull requests tab to ensure that the feature isn't already in progress.
33+
34+
Before submitting a pull request:
35+
36+
- Check the codebase to ensure that your feature doesn't already exist.
37+
- Check the pull requests to ensure that another person hasn't already submitted the feature or fix.
38+
39+
## Requirements
40+
41+
If the project maintainer has any additional requirements, you will find them listed here.
42+
43+
- **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](https://pear.php.net/package/PHP_CodeSniffer).
44+
45+
- **Add tests!** - Your patch won't be accepted if it doesn't have tests.
46+
47+
- **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date.
48+
49+
- **Consider our release cycle** - We try to follow [SemVer v2.0.0](https://semver.org/). Randomly breaking public APIs is not an option.
50+
51+
- **One pull request per feature** - If you want to do more than one thing, send multiple pull requests.
52+
53+
- **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](https://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting.
54+
55+
**Happy coding**!

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) omar alalwi
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# PHP Builders
2+
3+
[![Latest Version](https://img.shields.io/github/release/omaralalwi/php-builders.svg?style=flat-square)](https://github.com/omaralalwi/php-builders/releases)
4+
[![License](https://img.shields.io/badge/license-MIT-brightgreen.svg)](https://opensource.org/licenses/MIT)
5+
6+
**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.
7+
8+
## Table of Contents
9+
10+
- [Installation](#installation)
11+
- [Usage](#usage)
12+
- [Creating a Builder Class](#creating-a-builder-class)
13+
- [Using the Builder](#using-the-builder)
14+
- [Contributing](#contributing)
15+
- [Security](#security)
16+
- [License](#license)
17+
18+
## Installation
19+
20+
You can install the package via Composer. Run the following command in your terminal:
21+
22+
```bash
23+
composer require omaralalwi/php-builders
24+
```
25+
26+
## Usage
27+
28+
### Creating a Builder Class
29+
30+
To create a builder class, extend the `FluentBuilder` class and define your properties and methods.
31+
32+
```php
33+
namespace App\Builders;
34+
35+
use Omaralalwi\PhpBuilders\FluentBuilder;
36+
37+
class UserBuilder extends FluentBuilder
38+
{
39+
protected string $name;
40+
protected string $email;
41+
42+
public function setName(string $name): self
43+
{
44+
$this->name = $name;
45+
return $this;
46+
}
47+
48+
public function setEmail(string $email): self
49+
{
50+
$this->email = $email;
51+
return $this;
52+
}
53+
54+
public function sendEmail(): self
55+
{
56+
// handle send email to user or make any action
57+
// then return instance of self
58+
return $this;
59+
}
60+
61+
// add any another functions then call them as you need
62+
}
63+
```
64+
65+
### Using the Builder
66+
67+
You can convert the built instance to an array or an object using the `toArray()` and `toObject()` methods.
68+
69+
```php
70+
// use and return an array
71+
$userArray = UserBuilder::build()
72+
->setName('PHP Builders')
73+
->setEmail('[email protected]')
74+
->sendEmail()
75+
->toArray();
76+
77+
getType($userArray) // Array
78+
$userArray['name'] // PHP Builders
79+
$userArray['email'] // [email protected]
80+
81+
// use and return object
82+
$userObject = UserBuilder::build()
83+
->setName('John Doe')
84+
->setEmail('[email protected]')
85+
->sendEmail()
86+
->toObject();
87+
88+
getType($userArray) // object
89+
$userArray->name // PHP Builders
90+
$userArray->email // [email protected]
91+
```
92+
93+
## Contributing
94+
95+
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).
96+
97+
## Security
98+
99+
If you discover any security-related issues, please email creator : `[email protected]`.
100+
101+
## License
102+
103+
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.

composer.json

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
{
2+
"name": "omaralalwi/php-builders",
3+
"description": "sample php traits to add ability to use builder design patterns with easy in PHP applications",
4+
"keywords": [
5+
"omaralalwi",
6+
"php-builders",
7+
"php",
8+
"php-patterns",
9+
"design-patterns",
10+
"builders",
11+
"design-patterns",
12+
"fluent-interface",
13+
"object-creation",
14+
"builder-pattern",
15+
"php-builders",
16+
"traits"
17+
],
18+
"homepage": "https://github.com/omaralalwi/php-builders",
19+
"license": "MIT",
20+
"type": "library",
21+
"version": "1.0.0",
22+
"authors": [
23+
{
24+
"name": "omar alalwi",
25+
"email": "[email protected]",
26+
"role": "Developer"
27+
}
28+
],
29+
"require": {
30+
"php": "^8.1"
31+
},
32+
"require-dev": {
33+
"phpunit/phpunit": "^9.0"
34+
},
35+
"autoload": {
36+
"psr-4": {
37+
"Omaralalwi\\PhpBuilders\\": "src"
38+
}
39+
},
40+
"autoload-dev": {
41+
"psr-4": {
42+
"Omaralalwi\\PhpBuilders\\Tests\\": "tests"
43+
}
44+
},
45+
"scripts": {
46+
"test": "vendor/bin/phpunit",
47+
"test-coverage": "vendor/bin/phpunit --coverage-html coverage"
48+
49+
},
50+
"config": {
51+
"sort-packages": true
52+
}
53+
}

src/FluentBuilder.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Omaralalwi\PhpBuilders;
4+
5+
use Omaralalwi\PhpBuilders\Traits\{Buildable, Arrayable, Objectable};
6+
7+
class FluentBuilder
8+
{
9+
use Buildable, Arrayable, Objectable;
10+
}

src/Traits/Arrayable.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Omaralalwi\PhpBuilders\Traits;
4+
5+
trait Arrayable
6+
{
7+
public function toArray(): array
8+
{
9+
$properties = [];
10+
$reflection = new \ReflectionClass($this);
11+
foreach ($reflection->getProperties() as $property) {
12+
$property->setAccessible(true);
13+
$properties[$property->getName()] = $property->getValue($this);
14+
}
15+
return $properties;
16+
}
17+
}

src/Traits/Buildable.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Omaralalwi\PhpBuilders\Traits;
4+
5+
use Omaralalwi\PhpBuilders\FluentBuilder;
6+
7+
trait Buildable
8+
{
9+
/**
10+
* Returns an instance of the builder class.
11+
*
12+
* This is a static factory method that creates a new instance of the class.
13+
*
14+
* @return static A new instance of the builder class.
15+
*/
16+
public static function build(): self
17+
{
18+
return new static;
19+
}
20+
}

src/Traits/Objectable.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Omaralalwi\PhpBuilders\Traits;
4+
5+
trait Objectable
6+
{
7+
public function toObject(): object
8+
{
9+
$object = new \stdClass();
10+
$reflection = new \ReflectionClass($this);
11+
foreach ($reflection->getProperties() as $property) {
12+
$property->setAccessible(true);
13+
$object->{$property->getName()} = $property->getValue($this);
14+
}
15+
return $object;
16+
}
17+
}

0 commit comments

Comments
 (0)