https://github.com/skybluesofa/chainable
Allow for chainable, fluent method calls
https://github.com/skybluesofa/chainable
Last synced: 3 months ago
JSON representation
Allow for chainable, fluent method calls
- Host: GitHub
- URL: https://github.com/skybluesofa/chainable
- Owner: skybluesofa
- License: mit
- Created: 2016-10-02T19:54:21.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2016-10-12T17:25:22.000Z (over 8 years ago)
- Last Synced: 2025-01-04T18:28:59.616Z (5 months ago)
- Language: PHP
- Size: 14.6 KB
- Stars: 0
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://travis-ci.org/skybluesofa/chainable) [](https://codeclimate.com/github/skybluesofa/chainable) [](https://codeclimate.com/github/skybluesofa/chainable/coverage) [](https://packagist.org/packages/skybluesofa/chainable) [](https://packagist.org/packages/skybluesofa/chainable) [](LICENSE)
# Chainable
Allow for chainable, fluent method calls## Chaining Methods
Check out the [Tests/Sandwich(https://github.com/skybluesofa/chainable/blob/master/tests/Sandwich.php] class. It gives a bunch of uses for the Chainable trait.Basic use of this trait:
* Set the visibility for the chainable functions to 'private'
* Ensure that you either modify the current class or return a cloned version of the class
* Ensure that you return a reference to the class
```
private function withBread($breadType=null) {
$this->breadType = $breadType;
return $this;
}
```### Calling fluent methods
Now you can call these methods fluently:
```
Sandwich::withBread('white')->
addCondiment('peanut butter')->
addCondiment('jelly')->
make();
```
or the same sandwich, but the methods are called in a different order:
```
Sandwich::addCondiment('peanut butter')->
withBread('white')->
addCondiment('jelly')->
make();
```### Modifying the output
Any time up until you return something other than a reference to the chained class, you can modify what will be output:
```
Sandwich::withBread('wheat')->
addCondiment('peanut butter')->
addCondiment('grape jelly')->
withBread('white')->
removeCondiment('grape jelly')->
addCondiment('strawberry jelly')->
make();
```### Missing methods
This will return an exception, as the 'addBananas' method does not exist.
```
Sandwich::withBread('white')->
addCondiment('peanut butter')->
addBananas()->
make();
```### Public methods
This will return an error, as the 'addVegetable' method is not static:
```
Sandwich::addVegetable('lettuce')->
withBread('white')->
make();
```To work around this, use the 'chainableProxy' method before calling 'addVegetable':
```
Sandwich::chainableProxy()->
addVegetable('lettuce')->
withBread('white')->
make();
```