2017 © Pedro PelĆ”ez
 

library pipolino

image

phuria/pipolino

  • Saturday, July 8, 2017
  • by phuria
  • Repository
  • 1 Watchers
  • 2 Stars
  • 37 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 4 Versions
  • 3 % Grown

The README.md

Pipolino

Build Status Scrutinizer Code Quality Code Coverage Packagist license php, (*1)

The package provides dispatcher that can be used to process one or more values in sequence. It is a combination of thephpleague/pipeline and PSR-7 middleware dispatcher (eg. Relay)., (*2)

Installation

composer require phuria/pipolino

Usage

$pipolino = (new Pipolino())
    ->addStage(function(callable $next, $payload) {
        return $next($payload * 2); // 20 * 2
    })
    ->addStage(function (callable $next, $payload) {
        return $next($payload + 1); // 40 + 1
    })
    ->addStage(function (callable $next, $payload) {
        return $next($payload / 10); // 41 / 10
    });

echo $pipolino->process(20); //output: 4.1

Immutable

Pipolino are implemented as immutable. Any change on object (eg. add new stage) creates new object., (*3)

Stage objects

Object can be used as a stage. It must have only implemented __invoke method., (*4)

class CacheStage
{
    private $cache;

    public function __construct(CacheInterface $cache)
    {
        $this->cache = $cache;
    }

    public function __invoke(callable $next, $result, array $criteria)
    {
        $hash = sha1(serialize($criteria));

        if ($this->cache->has($hash)) {
            return $this->cache->get($hash);
        }

        $result = $next($result, $criteria);
        $this->cache->set($hash, $result, 3600);

        return $result;
    }
}

Many arguments

Pipolino (in contrast to pipeline) accepts any number of arguments to process., (*5)

$pipolino = (new Pipolino)
    ->addStage(function (callable $next, $result, array $options) {
        if (null === $result) {
            return $options['onNull'];
        }

        return $next($result, $context);
    })
    ->addStage(function (callable $next, $result, array $options) {
        $formatted = $next(number_format($result, 2).' '.$options['currency'])

        // Can be replaced with: "return $forrmated;",
        // however, should call $next().
        return $next($formatted, $options); 
    });

$options = ['onNull' => '-', 'currency' => 'USD'];

echo $pipolino->process(null, $options); // output: -
echo $pipolino->process(10, $options); // output: 10.00 USD

Pipolino composite

Each pipolino can be used as stage. This allows for easy re-use pipolino., (*6)

$loadingProcess = (new Pipolino())
    ->add(new CheckCache())
    ->add(new LoadFromDB());

$showProcces = (new Pipolino())
    ->add(new JsonFormat())
    ->add($loadingProccess)
    ->add(new NullResult());

Default stage

Default stage is last stage in pipolino, and does not accept $next callable as first argument. If you need to alter behavior of default stage (return first argument) call Pipolino::withDefaultStage() method., (*7)

$pipolino = (new Pipolino)
    ->addStage(function (callable $net, $i) {
        if (is_string($i)) {
            $i = (int) $i;
        }

        return $next($i);
    })
    ->addStage(function (callable $next, $i) {
        if (is_int($i)) {
            return $i % 2 ? 'even' : 'odd';
        }

        return $next($i);
    })
    ->withDefaultStage(function () {
        throw new \Exception('Unable to guess type.');
    });

echo $pipolino->process(2); // output: even
echo $pipolino->process('4'); // output: even
echo $pipolino->proccess(2.50); // throws exception

The Versions

08/07 2017

dev-master

9999999-dev https://github.com/phuria/pipolino

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

The Development Requires

by Beniamin Jonatan Å imko

dispatcher pipeline

08/07 2017

0.3.0

0.3.0.0 https://github.com/phuria/pipolino

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

The Development Requires

by Beniamin Jonatan Å imko

dispatcher pipeline

03/03 2017

0.2.0

0.2.0.0 https://github.com/phuria/pipolino

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

The Development Requires

by Beniamin Jonatan Å imko

dispatcher pipeline

03/03 2017

0.1.0

0.1.0.0 https://github.com/phuria/pipolino

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

The Development Requires

by Beniamin Jonatan Å imko

dispatcher pipeline