Beispiel #1
0
 public function testEmptyOrNot()
 {
     $opt = Optional::fromNullable([0, 1, 2]);
     $this->assertFalse($opt->isEmpty());
     $this->assertEquals([0, 1, 2], $opt->get());
     $opt = Optional::fromNullable(null);
     $this->assertTrue($opt->isEmpty());
     try {
         $opt->get();
     } catch (\BadMethodCallException $ex) {
         return;
     }
     $this->fail('An expected exception has not been raised.');
 }
Beispiel #2
0
 /**
  * Reduce the stream using the given function. The reduce method will be called with
  * two Optional value.
  * 
  * @param callable|functions\BiFunction $reducer The reduce function to apply.
  * @param mixed $default Default value
  * 
  * @return Optional Value after the reduce operation if exists.
  */
 public function reduceWithDefault($reducer, $default)
 {
     $res = $this->reduce($reducer);
     return $res->isEmpty() ? Optional::fromNullable($default) : $res;
 }