/** * 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))]); }
/** * Collects item resulting of the Stream Process and call the reduce method with previous reduced value and * current item. * * @param type $key Key value in the initial array (<em>array index</em>) * @param type $value Value after processing */ public function collect($key, $value) { if ($this->current->isEmpty()) { $this->current = Optional::of($value); } else { $this->current = Optional::of($this->func !== null ? $this->func->apply($this->current->get(), $value) : call_user_func($this->callable, $this->current->get(), $value)); } }
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])); }
/** * Collects the min element at any time regarding the comparator method. * * @param type $key Key value in the initial array (<em>array index</em>) * @param type $value Value after processing */ public function collect($key, $value) { if ($this->current->isEmpty()) { $this->current = Optional::of($value); } else { if ($this->comparator !== null) { if ($this->comparator->compare($value, $this->current->get()) < 0) { $this->current = Optional::of($value); } } else { if (call_user_func($this->callable, $value, $this->current->get()) < 0) { $this->current = Optional::of($value); } } } }
/** * Collects the first element of the stream processing. * * @param type $key Key value in the initial array (<em>array index</em>) * @param type $value Value after processing */ public function collect($key, $value) { if ($this->optional->isEmpty()) { $this->optional = Optional::of($value); } }