2017 © Pedro Peláez
 

library chainable

Allow for chainable, fluent method calls

image

skybluesofa/chainable

Allow for chainable, fluent method calls

  • Wednesday, October 12, 2016
  • by skybluesofa
  • Repository
  • 1 Watchers
  • 0 Stars
  • 166 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 2 Forks
  • 0 Open issues
  • 2 Versions
  • 20 % Grown

The README.md

Build Status Code Climate Test Coverage Total Downloads Version Software License, (*1)

Chainable

Allow for chainable, fluent method calls, (*2)

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., (*3)

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, (*4)

private function withBread($breadType=null) {
  $this->breadType = $breadType;
  return $this;
}

Calling fluent methods

Now you can call these methods fluently:, (*5)

Sandwich::withBread('white')->
  addCondiment('peanut butter')->
  addCondiment('jelly')->
  make();

or the same sandwich, but the methods are called in a different order:, (*6)

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:, (*7)

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., (*8)

Sandwich::withBread('white')->
  addCondiment('peanut butter')->
  addBananas()->
  make();

Public methods

This will return an error, as the 'addVegetable' method is not static:, (*9)

Sandwich::addVegetable('lettuce')->
  withBread('white')->
  make();

To work around this, use the 'chainableProxy' method before calling 'addVegetable':, (*10)

Sandwich::chainableProxy()->
  addVegetable('lettuce')->
  withBread('white')->
  make();

The Versions

12/10 2016

dev-master

9999999-dev https://github.com/skybluesofa/chainable

Allow for chainable, fluent method calls

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

The Development Requires

object fluent method chainable method_exists

03/10 2016

v0.1.0

0.1.0.0 https://github.com/skybluesofa/chainable

Allow for chainable, fluent method calls

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

The Development Requires

object fluent method chainable method_exists