/** * 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))]); }
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])); }
/** * Removing the collected element. */ public function reset() { $this->current = Optional::absent(); }
/** * Remove the collected element. */ public function reset() { $this->optional = Optional::absent(); }
/** * 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; }