/** * Encode constraints into JSON schema types/constraints. * * @param Spec $spec * @param AbstractConstraint|AbstractConstraint[]|Spec $input * @param string $title * * @throws CoreException * @return array */ public function encodeConstraints(Spec $spec, $input, $title) { if ($input instanceof AbstractConstraint) { $schema = ['description' => $input->getDescription()]; if ($input instanceof PrimitiveTypeConstraint) { $schema['type'] = $input->toString(); } return $schema; } elseif (is_array($input)) { $schema = []; $descriptions = []; foreach ($input as $constraint) { $descriptions[] = $constraint->getDescription(); if ($constraint instanceof PrimitiveTypeConstraint) { $schema['type'] = $constraint->toString(); } } $schema['description'] = implode('. ', $descriptions); return $schema; } elseif ($input instanceof Spec) { return (new static())->encode($input, $title); } throw new CoreException('Unexpected constraint type.'); }
/** * @param AbstractConstraint $constraint * * @return bool */ public function addConstraint(AbstractConstraint $constraint) { return $this->constraints->add($constraint); }