PHP code example of phuria / pipolino
1. Go to this page and download the library: Download phuria/pipolino library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
phuria / pipolino example snippets
$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
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 = (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
$loadingProcess = (new Pipolino())
->add(new CheckCache())
->add(new LoadFromDB());
$showProcces = (new Pipolino())
->add(new JsonFormat())
->add($loadingProccess)
->add(new NullResult());
$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