Skip to content

Instantly share code, notes, and snippets.

@kobus1998
Last active December 22, 2017 12:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kobus1998/720ea2cb7bcb23c0a49dfe16e0850198 to your computer and use it in GitHub Desktop.
Save kobus1998/720ea2cb7bcb23c0a49dfe16e0850198 to your computer and use it in GitHub Desktop.
PHP simple pipeline class
<?php
namespace App\Utill;
class Pipe {
/**
* @var Any return value of last function
*/
private $result = null;
/**
* Constructor
* @param Callable function to pipe (optional)
* @param Any argument of function
* @return self
*/
function __construct (Callable $function = null, $arg = null)
{
if ($function != null)
{
$this->result = $arg;
$this->pipe($function);
}
return $this;
}
/**
* Execute current function and save return value
* @param Callable function
* @return self
*/
public function pipe (Callable $function)
{
if ($this->result != null)
{
$this->result = $function($this->result);
}
else
{
$this->result = $function();
}
return $this;
}
/**
* Get last return value
* @return Any
*/
public function getResult ()
{
return $this->result;
}
/**
* Clean the result
* @return self
*/
public function end ()
{
$this->result = null;
return $this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment