/**
  * Attempt to encode a Spec into a JSON schema.
  *
  * @param Spec $spec
  * @param string $title
  *
  * @throws CoreException
  * @return string
  */
 public function encode(Spec $spec, $title = 'root')
 {
     $schema = ['$schema' => 'http://json-schema.org/draft-04/schema#', 'title' => $title, 'type' => CompoundTypes::COMPOUND_OBJECT, 'required' => $spec->getRequired()];
     $properties = [];
     foreach ($spec->getConstraints() as $key => $constraints) {
         $properties[$key] = $this->encodeConstraints($spec, $constraints, $key);
     }
     foreach ($spec->getDefaults() as $key => $default) {
         if (!Arr::has($properties, $key)) {
             $properties[$key] = [];
         }
         $properties[$key]['default'] = $default;
     }
     $schema['properties'] = $properties;
     return json_encode($schema, JSON_PRETTY_PRINT);
 }
Esempio n. 2
0
 /**
  * Get the type of each field.
  *
  * @return array
  */
 public function getTypes()
 {
     if ($this->spec instanceof Spec) {
         return Std::map(function ($field) {
             if ($field instanceof AbstractConstraint) {
                 return $field->toString();
             }
             return implode(' ^ ', Std::map(function ($innerField) {
                 if ($innerField instanceof Spec) {
                     return '{spec}';
                 } elseif ($innerField instanceof AbstractConstraint) {
                     return $innerField->toString();
                 }
                 return '{???}';
             }, $field));
         }, $this->spec->getConstraints());
     }
     return [];
 }
 /**
  * @param Spec $spec
  * @param string $field
  *
  * @return TableRow
  */
 protected function renderConstraint(Spec $spec, $field)
 {
     $constraints = $spec->getConstraints();
     $defaults = $spec->getDefaults();
     $default = '';
     if (array_key_exists($field, $defaults)) {
         $default = $defaults[$field];
     }
     $constraint = $constraints[$field];
     if ($constraint instanceof AbstractConstraint) {
         return new TableRow([], [new TableCell([], $field), new TableCell([], $constraint->toString()), new TableCell([], (string) $default)]);
     }
     return new TableRow([], [new TableCell([], $field), new TableCell([], Std::map(function (AbstractConstraint $constraint) {
         return $constraint->toString() . ', ';
     }, $constraint)), new TableCell([], (string) $default)]);
 }
Esempio n. 4
0
 public function testAccessors()
 {
     $strings = Boa::string();
     $spec = new Spec(['name' => $strings], ['name' => 'Bobby'], ['name']);
     $this->assertEqualsMatrix([[['name' => $strings], $spec->getConstraints()], [['name' => 'Bobby'], $spec->getDefaults()], [['name'], $spec->getRequired()]]);
 }
Esempio n. 5
0
 /**
  * Get a RAML parameter definition from a Spec field.
  *
  * @param Spec $spec
  * @param string $field
  *
  * @return array
  */
 protected function specFieldToParameter(Spec $spec, $field)
 {
     $constraints = $spec->getConstraints();
     $defaults = $spec->getDefaults();
     $required = $spec->getRequired();
     $parameter = [];
     if (Arr::has($constraints, $field)) {
         $input = $constraints[$field];
         if (is_array($input) || $input instanceof Spec) {
             if ($input instanceof Spec) {
                 $input = [$input];
             }
             foreach ($input as $constraint) {
                 if ($constraint instanceof PrimitiveTypeConstraint) {
                     switch ($constraint->toString()) {
                         case ScalarTypes::SCALAR_STRING:
                             $parameter['type'] = 'string';
                             break;
                         case ScalarTypes::SCALAR_FLOAT:
                         case ScalarTypes::SCALAR_INTEGER:
                             $parameter['type'] = 'number';
                             break;
                         case ScalarTypes::SCALAR_BOOLEAN:
                             $parameter['type'] = 'boolean';
                             break;
                     }
                 }
             }
         }
     }
     if (Arr::has($defaults, $field)) {
         $parameter['default'] = $defaults[$field];
     }
     $parameter['required'] = in_array($field, $required);
     return $parameter;
 }