/** * Get the value of the provided key. * * @param string $key * * @return Maybe */ public function lookup($key) { if (!$this->member($key)) { return Maybe::nothing(); } return Maybe::just($this->value->offsetGet($key)); }
/** * Get the value of the provided key. * * @param string $key * * @return Maybe * @throws CoreException */ public function lookup($key) { foreach ($this->value as $innerKey => $value) { if ($key === $innerKey) { return Maybe::just($value); } } return Maybe::nothing(); }
/** * Get the value of the provided key. * * @param string $key * * @return Maybe * @throws CoreException */ public function lookup($key) { Arguments::define($this->getKeyType())->check($key); if (!$this->member($key)) { return Maybe::nothing(); } return Maybe::just($this->value[$key]); }
/** * @param callable $predicate * * @return Maybe */ public function find(callable $predicate) { $result = Maybe::nothing(); $this->each(function ($value, $key) use($predicate, &$result) { if ($predicate($value, $key, $this)) { $result = Maybe::just($value); return false; } return true; }); return $result; }
/** * @param mixed $key * * @return Maybe */ public function lookup($key) { if ($this->member($key) === false) { return Maybe::nothing(); } $copy = array_merge($this->value); return Maybe::just($copy[$key]); }
public function testFmap() { $just = Maybe::just('DOGE'); $nothing = Maybe::nothing(); $lowerCase = function ($string) { return Maybe::just(strtolower($string)); }; $result1 = $just->fmap($lowerCase); $result2 = $nothing->fmap($lowerCase); $this->assertTrue($result1->isJust()); $this->assertTrue($result2->isNothing()); }