/** * Get string representation of this constraint. * * @return mixed */ public function toString() { $one = $this->one->toString(); $other = $this->other->toString(); if ($this->one instanceof AbstractTypeConstraint) { $one = $this->one->isUnion() ? sprintf('(%s)', $one) : $one; } if ($this->other instanceof AbstractTypeConstraint) { $other = $this->other->isUnion() ? sprintf('(%s)', $other) : $other; } return sprintf('%s|%s', $one, $other); }
/** * 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.'); }