skybluesofa/chainable

Allow for chainable, fluent method calls

v0.1.0 2016-10-03 16:44 UTC

This package is auto-updated.

Last update: 2024-04-20 00:36:14 UTC


README

Build Status Code Climate Test Coverage Total Downloads Version Software 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();