/** * 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 string $fieldName * * @return bool */ public function getFieldRequired($fieldName) { return Maybe::fromMaybe(false, $this->getFieldAnnotation($fieldName, static::ANNOTATION_REQUIRED)); }
/** * 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)); }
public function testFromMaybe() { $just = Maybe::of('doge'); $nothing = Maybe::nothing(); $this->assertEqualsMatrix([['doge', Maybe::fromMaybe('default', $just)], ['default', Maybe::fromMaybe('default', $nothing)]]); }