Last active
December 22, 2017 12:31
-
-
Save kobus1998/720ea2cb7bcb23c0a49dfe16e0850198 to your computer and use it in GitHub Desktop.
PHP simple pipeline class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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