transprime-research / laravel-chained
PHP Laravel-Chained
1.0.0
2021-10-24 13:24 UTC
Requires
- php: >=7.2
Requires (Dev)
- phpunit/phpunit: ~8.0
This package is auto-updated.
Last update: 2024-10-24 19:47:26 UTC
README
About Laravel-chained
Laravel chained help to chain method calls on any class.
Do it Like a PRO 🆗
Installation
composer require transprime-research/laravel-chained
Quick Usage
Say we have this class with a lot of static methods. Instead of:
$value = Str::lower('ChainedOnStr'); $value = Str::snake($value); $value = Str::before($value, '_'); $value = Str::length($value); //7
You use:
$value = chained(Str::class, ) ->to('lower', 'ChainedOnStr') ->to('snake') ->to('before', '_') ->to('length')(); //14
Or Aliased method calls:
$value = chained(Str::class) ->lower('ChainedOnStr') ->snake() ->before('_') ->length()(); //7
Other Usages
tap() method
$value = chained(Str::class) ->to('lower', 'ChainedOnStr') ->tap(function ($res) { var_dump($res); }) ->to('snake') ->to('length') ->up(); //Up is used instead of ()
Coming Soon
Chain on more classes
use Transprime\Chained\Chained; $value = chained(DB::class)->to('resolveDb', 'ChainedOnStr') ->chain(Str::class, function (Chained $chain) { return $chain->to('lower')->to('snake'); }) ->chain(Arr::class, function (Chained $chain) { return $chain->to('wrap')->to('add', 1, 'using_add'); })(); //Or chained(DB::class) ->to('resolveDb', 'ChainedOnStr') ->chain(Str::class) // next calls use `Str` class ->to('lower')->to('snake') ->chain(Arr::class) // next calls use `Arr` class ->to('wrap')->to('add', 1, 'using_add')();
Api implementation to be decided
Additional Information
This package is part of a series of "Code Dare".
See other packages in this series here:
- A smart PHP if...elseif...else statement https://github.com/omitobi/conditional
- A functional PHP pipe in object-oriented way https://github.com/transprime-research/piper
- Array now an object https://github.com/transprime-research/arrayed
- A smart PHP try...catch statement https://github.com/transprime-research/attempt
- A smart Carbon + Collection package https://github.com/omitobi/carbonate
- Jsonable Http Request(er) package with Collections response https://github.com/omitobi/laravel-habitue
Contributions
For new features, checkout with prefix feature/#issueid
e.g feature/#100-add-auto-deploy
- Clone this repository
- run
sh dockerizer.sh
orbash dockerizer.sh
- execute into the docker environment with
docker-compose exec conditional sh
(sh
can be another bash) - run tests with
vendor/bin/phpunit
The docker setup was made easy using Laravel Dockerizer
Similar packages
Licence
MIT (See LICENCE file)