| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCommunity\Framework\Support; |
| 4 |
|
| 5 |
/** |
| 6 |
* @phpstan-consistent-constructor |
| 7 |
*/ |
| 8 |
class Pipe |
| 9 |
{ |
| 10 |
use Tappable, Conditionable, MacroableTrait; |
| 11 |
|
| 12 |
/** |
| 13 |
* The value being piped. |
| 14 |
* |
| 15 |
* @var mixed |
| 16 |
*/ |
| 17 |
protected $value; |
| 18 |
|
| 19 |
/** |
| 20 |
* Create a new pipe instance. |
| 21 |
* |
| 22 |
* @param mixed $value |
| 23 |
* @return void |
| 24 |
*/ |
| 25 |
public function __construct($value) |
| 26 |
{ |
| 27 |
$this->value = $value; |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Get a new pipe instance for the given value. |
| 32 |
* |
| 33 |
* @param mixed $value |
| 34 |
* @return static |
| 35 |
*/ |
| 36 |
public static function of($value) |
| 37 |
{ |
| 38 |
return new static($value); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Pass the value to the callback and return a new pipe for the result. |
| 43 |
* |
| 44 |
* Any extra arguments are passed to the callback after the value. |
| 45 |
* |
| 46 |
* @param callable $callback |
| 47 |
* @param mixed ...$args |
| 48 |
* @return static |
| 49 |
*/ |
| 50 |
public function pipe(callable $callback, ...$args) |
| 51 |
{ |
| 52 |
// A copy, so by-reference callbacks like sort() can't change this pipe. |
| 53 |
$value = $this->value; |
| 54 |
|
| 55 |
return new static($callback($value, ...$args)); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Get the piped value. |
| 60 |
* |
| 61 |
* @return mixed |
| 62 |
*/ |
| 63 |
public function value() |
| 64 |
{ |
| 65 |
return $this->value; |
| 66 |
} |
| 67 |
} |
| 68 |
|