phuria/pipolino

0.3.0 2017-07-08 19:47 UTC

This package is auto-updated.

Last update: 2024-08-29 04:27:25 UTC


README

Build Status Scrutinizer Code Quality Code Coverage Packagist license php

该软件包提供了一种分发器,可以用于按顺序处理一个或多个值。它是 thephpleague/pipelinePSR-7 中间件分发器(例如 Relay) 的组合。

安装

composer require phuria/pipolino

使用

$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

不可变

Pipolino 是作为 不可变 实现的。任何对对象(例如添加新阶段)的更改都会创建新对象。

阶段对象

对象可以用作阶段。它必须只实现了 __invoke 方法。

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;
    }
}

多个参数

Pipolino(与管道不同)接受任何数量的参数进行处理。

$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 组合

每个 pipolino 都可以用作阶段。这允许方便地重用 pipolino。

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

默认阶段

默认阶段是 pipolino 中的最后一个阶段,并且不接受 $next 可调用对象作为第一个参数。如果您需要更改默认阶段的操作(返回第一个参数),请调用 Pipolino::withDefaultStage() 方法。

$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