/** * This method returns a Left\Type when an exception is encountered or a Right\Type * when try-block has executed successfully. * * @public * @static * @param callable $tryblock the try-block to be processed * @return IEither\Type either a Left\Type or a Right\Type */ public static function try_(callable $tryblock) : IEither\Type { try { return IEither\Type::right($tryblock()); } catch (Throwable\Runtime\Exception $re) { return IEither\Type::left($re); } catch (\Exception $ue) { return IEither\Type::left(new Throwable\Unknown\Exception($ue)); } }
/** * This method returns this option's object in an IEither\Right\Type if defined; otherwise, * returns the specified object in an IEither\Left\Type. * * @access public * @static * @param IOption\Type $xs the operand * @param Core\Type $x the object to be returned * if option is not defined * @return IEither\Type the either */ public static function toRight(IOption\Type $xs, Core\Type $x) : IEither\Type { return $xs->__isDefined() ? IEither\Type::right($xs->item()) : IEither\Type::left($x); }
/** * This method tests the "toString" method. * * @dataProvider data_toString */ public function test_toString(array $provided, array $expected) { $p0 = IEither\Type::left(IInt32\Type::box($provided[0]))->toString(); $e0 = $expected[0]; $this->assertInstanceOf('\\Saber\\Data\\IString\\Type', $p0); $this->assertSame($e0, $p0->unbox()); $p1 = IEither\Type::right(IInt32\Type::box($provided[0]))->toString(); $e1 = $expected[0]; $this->assertInstanceOf('\\Saber\\Data\\IString\\Type', $p1); $this->assertSame($e1, $p1->unbox()); }
/** * This method returns the latter value should the former value evaluates * to null. * * @access public * @static * @param IEither\Type $x the value to be evaluated * @param IEither\Type $y the default value * @return IEither\Type the result */ public static function nvl(IEither\Type $x = null, IEither\Type $y = null) : IEither\Type { return $x ?? $y ?? IEither\Type::left(); }
/** * This method applies each item in this either to the subroutine function. * * @access public * @final * @param callable $subroutine the subroutine function to be used * @return IEither\Type the either */ public final function map(callable $subroutine) : IEither\Type { return $this->either->__isLeft() ? IEither\Type::left($subroutine($this->either->item(), IInt32\Type::zero())) : IEither\Type::right($this->either->projectRight()->item()); }