Traits
1 month ago
Either.php
1 month ago
Fns.php
1 month ago
Invoker.php
1 month ago
Logic.php
1 month ago
Obj.php
1 month ago
Relation.php
1 month ago
Str.php
1 month ago
Undefined.php
1 month ago
functions.php
1 month ago
Either.php
158 lines
| 1 | <?php |
| 2 | |
| 3 | namespace OTGS\Installer\FP; |
| 4 | |
| 5 | use OTGS\Installer\Collect\Support\Macroable; |
| 6 | use OTGS\Installer\FP\Traits\Functor; |
| 7 | use OTGS\Installer\FP\Traits\Pointed; |
| 8 | use OTGS\Installer\FP\Traits\ConstApplicative; |
| 9 | use OTGS\Installer\FP\Traits\Applicative; |
| 10 | |
| 11 | abstract class Either { |
| 12 | use Functor; |
| 13 | use Macroable; |
| 14 | |
| 15 | public static function init() { |
| 16 | self::macro( 'of', Right::of() ); |
| 17 | |
| 18 | self::macro( 'left', Left::of() ); |
| 19 | |
| 20 | self::macro( 'right', Right::of() ); |
| 21 | |
| 22 | self::macro( 'fromNullable', curryN( 1, function ( $value ) { |
| 23 | return is_null( $value ) ? self::left( $value ) : self::right( $value ); |
| 24 | } ) ); |
| 25 | |
| 26 | self::macro( 'fromBool', curryN( 1, function ( $value ) { |
| 27 | return (bool) $value ? self::right( $value ) : self::left( $value ); |
| 28 | } ) ); |
| 29 | |
| 30 | } |
| 31 | |
| 32 | public function join() { |
| 33 | if ( ! $this->value instanceof Either ) { |
| 34 | return $this; |
| 35 | } |
| 36 | |
| 37 | return $this->value->join(); |
| 38 | } |
| 39 | |
| 40 | abstract public function chain( callable $fn ); |
| 41 | |
| 42 | abstract public function bichain( callable $leftFn, callable $rightFn); |
| 43 | |
| 44 | abstract public function orElse( callable $fn ); |
| 45 | abstract public function bimap( callable $leftFn, callable $rightFn ); |
| 46 | abstract public function coalesce( callable $leftFn, callable $rightFn ); |
| 47 | abstract public function alt( Either $alt ); |
| 48 | abstract public function filter( callable $fn ); |
| 49 | |
| 50 | } |
| 51 | |
| 52 | class Left extends Either { |
| 53 | |
| 54 | use ConstApplicative; |
| 55 | use Pointed; |
| 56 | |
| 57 | public function map( callable $fn ) { |
| 58 | return $this; |
| 59 | } |
| 60 | |
| 61 | public function bimap( callable $leftFn, callable $rightFn ) { |
| 62 | return Either::left( $leftFn( $this->value ) ); |
| 63 | } |
| 64 | |
| 65 | public function coalesce( callable $leftFn, callable $rightFn ) { |
| 66 | return Either::of( $leftFn( $this->value ) ); |
| 67 | } |
| 68 | |
| 69 | public function get() { |
| 70 | throw new \Exception( "Can't extract the value of Left" ); |
| 71 | } |
| 72 | |
| 73 | public function getOrElse( $other ) { |
| 74 | return $other; |
| 75 | } |
| 76 | |
| 77 | public function orElse( callable $fn ) { |
| 78 | return Either::right( $fn( $this->value ) ); |
| 79 | } |
| 80 | |
| 81 | public function chain( callable $fn ) { |
| 82 | return $this; |
| 83 | } |
| 84 | |
| 85 | public function bichain( callable $leftFn, callable $rightFn ) { |
| 86 | return $leftFn( $this->value ); |
| 87 | } |
| 88 | |
| 89 | public function getOrElseThrow( $value ) { |
| 90 | throw new \Exception( $value ); |
| 91 | } |
| 92 | |
| 93 | public function filter( callable $fn ) { |
| 94 | return $this; |
| 95 | } |
| 96 | |
| 97 | public function tryCatch( callable $fn ) { |
| 98 | return $this; |
| 99 | } |
| 100 | |
| 101 | public function alt( Either $alt ) { |
| 102 | return $alt; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | class Right extends Either { |
| 107 | |
| 108 | use Applicative; |
| 109 | use Pointed; |
| 110 | |
| 111 | public function map( callable $fn ) { |
| 112 | return Either::of( $fn( $this->value ) ); |
| 113 | } |
| 114 | |
| 115 | public function bimap( callable $leftFn, callable $rightFn ) { |
| 116 | return $this->map( $rightFn ); |
| 117 | } |
| 118 | |
| 119 | public function coalesce( callable $leftFn, callable $rightFn ) { |
| 120 | return $this->map( $rightFn ); |
| 121 | } |
| 122 | |
| 123 | public function getOrElse( $other ) { |
| 124 | return $this->value; |
| 125 | } |
| 126 | |
| 127 | public function orElse( callable $fn ) { |
| 128 | return $this; |
| 129 | } |
| 130 | |
| 131 | public function getOrElseThrow( $value ) { |
| 132 | return $this->value; |
| 133 | } |
| 134 | |
| 135 | public function chain( callable $fn ) { |
| 136 | return $this->map( $fn )->join(); |
| 137 | } |
| 138 | |
| 139 | public function bichain( callable $leftFn, callable $rightFn ) { |
| 140 | return $rightFn( $this->value ); |
| 141 | } |
| 142 | |
| 143 | public function filter( callable $fn ) { |
| 144 | return Logic::ifElse( $fn, Either::right(), Either::left(), $this->value ); |
| 145 | } |
| 146 | |
| 147 | public function tryCatch( callable $fn ) { |
| 148 | return tryCatch( function () use ( $fn ) { |
| 149 | return $fn( $this->value ); |
| 150 | } ); |
| 151 | } |
| 152 | |
| 153 | public function alt( Either $alt ) { |
| 154 | return $this; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | Either::init(); |