public static final function elementAt(array $array, $offset) : OptionalInterface { foreach ($array as $key => $value) { if ($key === $offset) { return Optional::of($value); } } return Optional::absent(); }
public static final function elementAt(\Traversable $traversable, $position) : OptionalInterface { $i = 0; foreach ($traversable as $value) { if ($i == $position) { return Optional::of($value); } } return Optional::absent(); }
public function testOfEmpty() { $o = Optional::OfEmpty(0); $this->assertInstanceOf(Optional::class, $o); $this->assertInstanceOf(OptionalInterface::class, $o); $this->assertTrue($o->isAbsent()); $this->assertFalse($o->isPresent()); $o = Optional::OfEmpty([]); $this->assertInstanceOf(Optional::class, $o); $this->assertInstanceOf(OptionalInterface::class, $o); $this->assertTrue($o->isAbsent()); $this->assertFalse($o->isPresent()); }