/** * The fromMaybe function takes a default value and and Maybe value. * If the Maybe is Nothing, it returns the default values; otherwise, * it returns the value contained in the Maybe. * * @param mixed $default * @param Maybe $maybe * * @return mixed */ public static function fromMaybe($default, Maybe $maybe) { if ($maybe->isNothing()) { return $default; } return $maybe->value; }
/** * 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)); }
public function testInsert() { $instance = new ArrayAccessMap(new ArrayObject(['great' => 'job'])); $instance2 = $instance->insert('omg', 'wow'); $this->assertNotSame($instance, $instance2); $this->assertEquals('wow', Maybe::fromJust($instance2->lookup('omg'))); }
/** * 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(); }
public function testSetFieldDescription() { $spec = new FormSpec(); $this->assertTrue($spec->getFieldDescription('wow')->isNothing()); $final = $spec->withFieldDescription('wow', 'WOWs'); $this->assertNotSame($spec, $final); $this->assertTrue($final->getFieldDescription('wow')->isJust()); $this->assertEquals('WOWs', Maybe::fromJust($final->getFieldDescription('wow'))); $this->assertEquals(['wow' => 'WOWs'], $final->getDescriptions()->toArray()); }
/** * Get an environment variable or return the default if it is not defined. * * This avoid any post-processing, such as automatic casting. * * @param string $key * @param null|mixed|callable $default * * @return mixed|string */ public static function getRaw($key, $default = null) { $env = ArrayMap::of($_ENV); $server = ArrayMap::of($_SERVER); if ($env->member($key)) { return Maybe::fromJust($env->lookup($key)); } elseif ($server->member($key)) { return Maybe::fromJust($server->lookup($key)); } $value = getenv($key); if ($value === false) { return Std::thunk($default); } return $value; }
/** * Run the flick on input. * * @param string|int $input * * @throws UnknownKeyException * @return mixed */ public function go($input) { Arguments::define(Boa::readMap())->define($input); $map = ComplexFactory::toReadMap($this->functions); if ($map->member($input)) { /** @var callable $function */ $function = Maybe::fromJust($map->lookup($input)); return $function(); } elseif ($map->member($this->default)) { /** @var callable $function */ $function = Maybe::fromJust($map->lookup($this->default)); return $function(); } throw new UnknownKeyException(); }
/** * Add one or more constraints to a field. * * @param string $field * @param AbstractConstraint|AbstractConstraint[] $constraint * * @throws MismatchedArgumentTypesException * @return $this */ public function let($field, $constraint) { Arguments::define(Boa::string(), Boa::either(Boa::instance(AbstractConstraint::class), Boa::arrOf(Boa::instance(AbstractConstraint::class))))->check($field, $constraint); if (!$this->constraints->member($field)) { $this->constraints = $this->constraints->insert($field, ArrayList::zero()); } if (!is_array($constraint)) { $constraint = [$constraint]; } $this->constraints = $this->constraints->insert($field, Maybe::fromMaybe(ArrayList::zero(), $this->constraints->lookup($field))->append(ArrayList::of($constraint))); return $this; }
/** * Render the object into a string. * * @return mixed */ public function render() { $addColon = function ($label) { return Maybe::just(vsprintf('%s:', [$label])); }; return (new Form($this->attributes, $this->spec->getAnnotations()->map(function ($_, $key) use($addColon) { return new Row(['class' => 'form-group'], [new Node('label', ['class' => 'col-sm-2 form-control-label'], Maybe::fromMaybe('', $this->spec->getFieldLabel($key)->bind($addColon))), new Div(['class' => 'col-sm-8'], $this->renderFullField($key))]); })->append(ArrayMap::of([new Row(['class' => 'form-group'], [new Div(['class' => 'col-sm-offset-2 col-sm-10'], [new Div(['class' => 'btn-group'], [new Button(['type' => 'reset', 'class' => 'btn btn-secondary'], 'Reset'), new Button(['type' => 'submit', 'class' => 'btn btn-primary'], 'Submit')])])])]))))->render(); }
/** * @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; }
/** * Get the type annotation for a field. * * @param string $fieldName * * @return AbstractTypeConstraint */ public function getFieldType($fieldName) { return Maybe::fromMaybe(Boa::any(), $this->getFieldAnnotation($fieldName, static::ANNOTATION_TYPE)); }
/** * @param string $fieldName * * @return bool */ public function getFieldRequired($fieldName) { return Maybe::fromMaybe(false, $this->getFieldAnnotation($fieldName, static::ANNOTATION_REQUIRED)); }
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()); }
/** * @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]); }