コード例 #1
0
ファイル: AnyCollector.php プロジェクト: hguenot/phpstream
 /**
  * Return one of the collected element.
  * 
  * @return Optional Any collected element or {@see Optional::absent()} if no element has been collected.
  */
 public function get()
 {
     if (empty($this->values)) {
         return Optional::absent();
     }
     return Optional::of($this->values[intval(rand(0, count($this->values) - 1))]);
 }
コード例 #2
0
ファイル: OptionalTest.php プロジェクト: hguenot/phpstream
 public function testOptionalEquals()
 {
     $opt = Optional::absent();
     $this->assertTrue($opt->equals($opt));
     $this->assertTrue($opt->equals(Optional::absent()));
     $this->assertFalse($opt->equals(Optional::of([0, 1, 2])));
     $this->assertFalse($opt->equals([0, 1, 2]));
     $opt = Optional::of([0, 1, 2]);
     $this->assertTrue($opt->equals($opt));
     $this->assertTrue($opt->equals(Optional::of([0, 1, 2])));
     $this->assertFalse($opt->equals(Optional::absent()));
     $this->assertFalse($opt->equals(Optional::of([2, 3, 4])));
     $this->assertFalse($opt->equals([0, 1, 2]));
 }
コード例 #3
0
ファイル: MinCollector.php プロジェクト: hguenot/phpstream
 /**
  * Removing the collected element.
  */
 public function reset()
 {
     $this->current = Optional::absent();
 }
コード例 #4
0
ファイル: FirstCollector.php プロジェクト: hguenot/phpstream
 /** 
  * Remove the collected element.
  */
 public function reset()
 {
     $this->optional = Optional::absent();
 }
コード例 #5
0
ファイル: Stream.php プロジェクト: hguenot/phpstream
 /**
  * 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;
 }