2017 © Pedro Peláez
 

library chainable

Exposes a Class that makes method chaining fluent and easy.

image

thejawker/chainable

Exposes a Class that makes method chaining fluent and easy.

  • Wednesday, February 21, 2018
  • by thejawker
  • Repository
  • 1 Watchers
  • 1 Stars
  • 108 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 6 Versions
  • 0 % Grown

The README.md

Fluent Chaining for Legacy code

Are you tired of using packages that require you to write code like the following:, (*1)

$someClass = new SomeClass();
$someClass->someMethod();
$someClass->someOtherMethod();

// Now you can just write it like this.
new Chain(SomeClass::class)
    ->someMethod()
    ->someOtherMethod();

// Or even easier
ch(SomeClass::class)
    ->someMethod()
    ->someOtherMethod();

Installation

Require the package from Composer:, (*2)

``` bash composer require thejawker/chainable, (*3)


## Usage ### Basic Usage You can use the Chain in two easy different ways, depending on your liking or necessity. ```php // Through passing the Class's classname. $someClass = new Chain(SomeClass::class); // Or through passing the actual instance $someClass = new Chain(new SomeClass($withParams))

Getting Properties

Getting properties is just the same, these won't return $this., (*4)

$property = new Chain(SomeClass::class)->property;

Escaping Chaining

Often methods do need to return an actual value, otherwise we can stop programming all together. Chain will allow you to easily turn off the chaining behaviour. This can be done very easily., (*5)

$someClass = new Chain(SomeClass::class)->someMethod()
$property = $someClass->escape()->getValue() // will return the original value

You can also unescape to get the Chainable functionality back. To be honest I can't find a use case for this yet, but hey! You never know!, (*6)

$someClass = new Chain(SomeClass::class)->someMethod();
$sameInstance = $someClass->escape();
$sameInstance->unescape()->otherMethod();

Get The Original Instance

Sometimes it is needed to return the original instance because some code might be checking if it is an instanceof a class. You can easily chain the ->instance() method on there and you get the original instance back, not a clone, the actual thing. Note: you can't go back from here., (*7)

public function calculate() 
{
    return new Chain(new LegacyClass)
        ->someMethod()
        ->setSome('stuff')
        ->maybeMore()
        ->instance();
}

Wanna Go Pro?

I've also included a convenient shortcut for those who think instantiating a Chain is too much work. The following will yield exactly the same result but then just with much more ease., (*8)

ch(LegacyClass::class)
    ->also()
    ->works('yay!')
    ->fuckYess();

Testing

I've included some tests, feel free to send me pull-requests if you see room for improvement. bash composer test, (*9)

License

The MIT License (MIT). Please see License File for more information., (*10)

The Versions

21/02 2018

dev-master

9999999-dev https://github.com/thejawker/laravel-route-module-macro

Exposes a Class that makes method chaining fluent and easy.

  Sources   Download

MIT

The Requires

  • php >=7.0.0

 

The Development Requires

chaining oop fluent thejawker

13/12 2017

0.0.5

0.0.5.0 https://github.com/thejawker/laravel-route-module-macro

Exposes a Class that makes method chaining fluent and easy. Also exposes a Trait.

  Sources   Download

MIT

The Requires

  • php >=7.0.0

 

The Development Requires

chaining oop fluent thejawker

15/10 2017

0.0.4

0.0.4.0 https://github.com/thejawker/laravel-route-module-macro

Exposes a Class that makes method chaining fluent and easy. Also exposes a Trait.

  Sources   Download

MIT

The Requires

  • php >=7.0.0

 

The Development Requires

chaining oop fluent thejawker

27/09 2017

0.0.3

0.0.3.0 https://github.com/thejawker/laravel-route-module-macro

Exposes a Class that makes method chaining fluent and easy. Also exposes a Trait.

  Sources   Download

MIT

The Requires

  • php >=7.0.0

 

The Development Requires

chaining oop fluent thejawker

27/09 2017

0.0.2

0.0.2.0 https://github.com/thejawker/laravel-route-module-macro

Exposes a Class that makes method chaining fluent and easy. Also exposes a Trait.

  Sources   Download

MIT

The Requires

  • php >=7.0.0

 

The Development Requires

chaining oop fluent thejawker

27/09 2017

0.0.1

0.0.1.0 https://github.com/thejawker/laravel-route-module-macro

Exposes a Class that makes method chaining fluent and easy. Also exposes a Trait.

  Sources   Download

MIT

The Requires

  • php >=7.0.0

 

The Development Requires

chaining oop fluent thejawker