public function testOrElseOnSomeWithCallback()
 {
     $option = Option::create(0)->orElse(function () {
         throw new RuntimeException('orElse on some');
     });
     $this->assertSame(0, $option->get());
 }
 public function testTestGetOrCallOnSome()
 {
     $value = Option::create(0)->getOrCall(function () {
         return 1;
     });
     $this->assertSame(0, $value);
 }
 public function __construct()
 {
     parent::__construct();
     $this->mapIdentity = function ($value) {
         return $value;
     };
     $this->mapIdentityOption = function ($value) {
         return Option::create($value);
     };
 }
 public function testEachOnSome()
 {
     $option = Option::create(0)->each(function ($value) {
         $this->assertSame(0, $value);
     });
 }
 public function testMagicInvokeOnSomeWithoutParameter()
 {
     $option = Option::create(0);
     $this->assertSame(0, $option());
 }
 public function __construct()
 {
     $this->none = Option::none();
     $this->some = new Some(0);
 }
 public function testGetOrElseOnSomeWithCallback()
 {
     $this->assertSame(0, Option::create(0)->getOrElse(function () {
         throw new RuntimeException('getOrElse on some');
     }));
 }
 public function testMagicGetOnArrayWithoutKey()
 {
     $array = ['defined' => ['key' => 0]];
     $this->assertSame($this->none, Option::create($array)->undefined->key);
 }
 public function testMagicIssetOnArrayWithoutKey()
 {
     $array = ['defined' => ['key' => 0]];
     $this->assertFalse(isset(Option::create($array)->undefined->key));
 }
 public function testCreateNoneFromCallbackWithCustomEmptyCallback()
 {
     $option = Option::create(function () {
         return 0;
     }, function ($value) {
         return $value === 0;
     });
     $this->assertSame($this->none, $option);
 }
Exemple #11
0
 public function orElse($default)
 {
     return Option::create($this->getOrElse($default));
 }
 public function testGetOnSome()
 {
     $this->assertSame(0, Option::create(0)->get());
 }
Exemple #13
0
 public function __get($name)
 {
     return Option::create($this->getPropertyOrKey($name));
 }
 public function testGetOrThrowOnSome()
 {
     $this->assertSame(0, Option::create(0)->getOrThrow(new RuntimeException()));
 }
 public function testMagicToStringOnSome()
 {
     $this->assertSame('0', (string) Option::create(0));
 }
Exemple #16
0
    /**
     * invoke magic method.
     *
     * Make the Option callable.
     * Return the Option's value if is non-empty,
     * $default otherwise.
     *
     *  $default can be:
     * 1. a callback - which is called and its result
     *  returned if the Option is a None.
     * 2. any other PHP value - which is returned
     *  in case the Option is a None.
     *
     * <code>
     *  $option = Option::create(0);
     *  $option(1); // 0
     *  $option();  // 0
     *
     *  $option = Option::none();
     *  $option(1); // 1
     *  $option();  // NoneValueException
     * </code>
     *
     * @throws NoneValueException If None without argument is called.
     * @param string $default
     * @return mixed
     */
    public abstract function __invoke($default = null);
}
Option::init();