Skip to content

Instantly share code, notes, and snippets.

@agoalofalife
Last active July 6, 2017 08:55
Show Gist options
  • Save agoalofalife/695370ebb6dbce310b89eb352d4963a4 to your computer and use it in GitHub Desktop.
Save agoalofalife/695370ebb6dbce310b89eb352d4963a4 to your computer and use it in GitHub Desktop.
An example of processing depending on values
<?php
interface Handler
{
public function handle($value, Closure $next);
}
// handler if the value
class NumberHandler implements Handler
{
// method for handler
public function handle($value, Closure $next)
{
if (gettype($value) != 'number')
{
//in the case where a value of another type to pass it on
return $next($value);
}
return round($value);
}
}
//similarly for string processing
class StringHandler implements Handler
{
public function handle($value, Closure $next)
{
if (gettype($value) != "string")
{
return $next($value);
}
return strtoupper($value);
}
}
//similarly for string array
class ArrayHandler implements Handler
{
public function handle($value, Closure $next)
{
if (gettype($value) != "array")
{
return $next($value);
}
sort($value);
return $value;
}
}
// create class Pipeline and pass inside Container
$test = [1,23,4,3];
$pipeline = new \Illuminate\Pipeline\Pipeline(new \Illuminate\Container\Container());
// in method send pass out value
// in method through operations we will process
$pipeline->send($test)->through([new NumberHandler(), new StringHandler(), new ArrayHandler()]);
// method then pass closure function which works if the value is never used
$response = $pipeline->then(function ($some){
dd("I do not know the value");
});
// in this case, we get the sorted array
dd($response);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment