/** * 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(); }
/** * @param array|Iterable $searchKeyPath * * @return Maybe */ public function lookupIn($searchKeyPath) { $path = ArrayList::of($searchKeyPath); if ($path->count() === 0) { return Maybe::nothing(); } if ($path->count() === 1) { return $this->lookup($path->head()); } $value = $this->lookup($path->head()); if ($value->isNothing()) { return $value; } $innerValue = Maybe::fromJust($value); if ($innerValue instanceof Iterable) { return $innerValue->lookupIn($path->tail()); } return Maybe::nothing(); }
/** * @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()); }