예제 #1
0
 public function testOrElseOnSomeWithCallback()
 {
     $option = Option::create(0)->orElse(function () {
         throw new RuntimeException('orElse on some');
     });
     $this->assertSame(0, $option->get());
 }
예제 #2
0
 public function testTestGetOrCallOnSome()
 {
     $value = Option::create(0)->getOrCall(function () {
         return 1;
     });
     $this->assertSame(0, $value);
 }
예제 #3
0
 public function __construct()
 {
     parent::__construct();
     $this->mapIdentity = function ($value) {
         return $value;
     };
     $this->mapIdentityOption = function ($value) {
         return Option::create($value);
     };
 }
예제 #4
0
 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());
 }
예제 #6
0
 public function __construct()
 {
     $this->none = Option::none();
     $this->some = new Some(0);
 }
예제 #7
0
 public function testGetOrElseOnSomeWithCallback()
 {
     $this->assertSame(0, Option::create(0)->getOrElse(function () {
         throw new RuntimeException('getOrElse on some');
     }));
 }
예제 #8
0
 public function testMagicGetOnArrayWithoutKey()
 {
     $array = ['defined' => ['key' => 0]];
     $this->assertSame($this->none, Option::create($array)->undefined->key);
 }
예제 #9
0
 public function testMagicIssetOnArrayWithoutKey()
 {
     $array = ['defined' => ['key' => 0]];
     $this->assertFalse(isset(Option::create($array)->undefined->key));
 }
예제 #10
0
 public function testCreateNoneFromCallbackWithCustomEmptyCallback()
 {
     $option = Option::create(function () {
         return 0;
     }, function ($value) {
         return $value === 0;
     });
     $this->assertSame($this->none, $option);
 }
예제 #11
0
 public function orElse($default)
 {
     return Option::create($this->getOrElse($default));
 }
예제 #12
0
 public function testGetOnSome()
 {
     $this->assertSame(0, Option::create(0)->get());
 }
예제 #13
0
 public function __get($name)
 {
     return Option::create($this->getPropertyOrKey($name));
 }
예제 #14
0
 public function testGetOrThrowOnSome()
 {
     $this->assertSame(0, Option::create(0)->getOrThrow(new RuntimeException()));
 }
 public function testMagicToStringOnSome()
 {
     $this->assertSame('0', (string) Option::create(0));
 }
예제 #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();