protected function generateType(PropertyInterface $type) { if (isset($this->_types[$type->getId()])) { return; } $this->_types[$type->getId()] = $type->getName(); if ($type instanceof Property\ComplexType) { $properties = $type->getProperties(); if (empty($properties)) { return; } $response = '<div id="psx-type-' . $type->getId() . '" class="psx-complex-type">'; $response .= '<h1>' . $type->getName() . '</h1>'; $response .= '<div class="psx-type-description">' . $type->getDescription() . '</div>'; $response .= '<table class="table psx-type-properties">'; $response .= '<colgroup>'; $response .= '<col width="20%" />'; $response .= '<col width="20%" />'; $response .= '<col width="40%" />'; $response .= '<col width="20%" />'; $response .= '</colgroup>'; $response .= '<thead>'; $response .= '<tr>'; $response .= '<th>Property</th>'; $response .= '<th>Type</th>'; $response .= '<th>Description</th>'; $response .= '<th>Constraints</th>'; $response .= '</tr>'; $response .= '</thead>'; $response .= '<tbody>'; foreach ($properties as $property) { list($type, $constraints) = $this->getValueDescription($property); $description = ''; if (!$property instanceof Property\ComplexType) { $description .= $property->getDescription(); } $response .= '<tr>'; $response .= '<td><span class="psx-property-name ' . ($property->isRequired() ? 'psx-property-required' : 'psx-property-optional') . '">' . $property->getName() . '</span></td>'; $response .= '<td>' . $type . '</td>'; $response .= '<td><span class="psx-property-description">' . $description . '</span></td>'; $response .= '<td>' . $constraints . '</td>'; $response .= '</tr>'; } $response .= '</tbody>'; $response .= '</table>'; $response .= '</div>'; foreach ($properties as $property) { if ($property instanceof Property\CompositeTypeAbstract) { foreach ($property as $childProperty) { $response .= $this->generateType($childProperty); } } } return $response; } return ''; }
protected function generateType(PropertyInterface $type) { if ($type instanceof Property\ComplexType) { $properties = $type->getProperties(); $props = array(); $required = array(); foreach ($properties as $property) { $props[$property->getName()] = $this->generateType($property); if ($property->isRequired()) { $required[] = $property->getName(); } } $result = array('type' => 'object', 'properties' => $props); $description = $type->getDescription(); if (!empty($description)) { $result['description'] = $description; } if (!empty($required)) { $result['required'] = $required; } $result['additionalProperties'] = false; $key = 'ref' . $type->getId(); $this->definitions[$key] = $result; return ['$ref' => '#/definitions/' . $key]; } elseif ($type instanceof Property\ArrayType) { $result = array('type' => 'array', 'items' => $this->generateType($type->getPrototype())); $description = $type->getDescription(); if (!empty($description)) { $result['description'] = $description; } $minLength = $type->getMinLength(); if ($minLength) { $result['minItems'] = $minLength; } $maxLength = $type->getMaxLength(); if ($maxLength) { $result['maxItems'] = $maxLength; } return $result; } elseif ($type instanceof Property\ChoiceType) { $properties = $type->getProperties(); $props = array(); foreach ($properties as $property) { $props[] = $this->generateType($property); } $result = array('oneOf' => $props); $description = $type->getDescription(); if (!empty($description)) { $result['description'] = $description; } return $result; } else { $result = array(); $result['type'] = $this->getPropertyTypeName($type); $description = $type->getDescription(); if (!empty($description)) { $result['description'] = $description; } if ($type instanceof Property\StringType) { $minLength = $type->getMinLength(); if ($minLength) { $result['minLength'] = $minLength; } $maxLength = $type->getMaxLength(); if ($maxLength) { $result['maxLength'] = $maxLength; } $pattern = $type->getPattern(); if ($pattern) { $result['pattern'] = $pattern; } } elseif ($type instanceof Property\DecimalType) { $min = $type->getMin(); if ($min) { $result['minimum'] = $min; } $max = $type->getMax(); if ($max) { $result['maximum'] = $max; } } $enumeration = $type->getEnumeration(); if ($enumeration) { $result['enum'] = $enumeration; } return $result; } }
protected function setParameterType(PropertyInterface $parameter, &$raml, $indent) { $indent = str_repeat(' ', $indent); switch (true) { case $parameter instanceof Property\IntegerType: $raml .= $indent . 'type: integer' . "\n"; break; case $parameter instanceof Property\FloatType: $raml .= $indent . 'type: number' . "\n"; break; case $parameter instanceof Property\BooleanType: $raml .= $indent . 'type: boolean' . "\n"; break; case $parameter instanceof Property\DateType: case $parameter instanceof Property\DateTimeType: $raml .= $indent . 'type: date' . "\n"; break; default: $raml .= $indent . 'type: string' . "\n"; break; } $description = $parameter->getDescription(); if (!empty($description)) { $raml .= $indent . 'description: ' . Inline::dump($parameter->getDescription()) . "\n"; } $raml .= $indent . 'required: ' . ($parameter->isRequired() ? 'true' : 'false') . "\n"; if ($parameter instanceof Property\DecimalType) { $min = $parameter->getMin(); $max = $parameter->getMax(); if ($min !== null) { $raml .= $indent . 'minimum: ' . $min . "\n"; } if ($max !== null) { $raml .= $indent . 'maximum: ' . $max . "\n"; } } elseif ($parameter instanceof Property\StringType) { $minLength = $parameter->getMinLength(); $maxLength = $parameter->getMaxLength(); $enumeration = $parameter->getEnumeration(); $pattern = $parameter->getPattern(); if ($minLength !== null) { $raml .= $indent . 'minLength: ' . $minLength . "\n"; } if ($maxLength !== null) { $raml .= $indent . 'maxLength: ' . $maxLength . "\n"; } if (!empty($enumeration)) { $raml .= $indent . 'enum: ' . Inline::dump($enumeration) . "\n"; } if (!empty($pattern)) { $raml .= $indent . 'pattern: ' . Inline::dump($pattern) . "\n"; } } }
protected function setParameterType(PropertyInterface $parameter, Parameter $param) { switch (true) { case $parameter instanceof Property\IntegerType: $param->setType('integer'); break; case $parameter instanceof Property\FloatType: $param->setType('number'); break; case $parameter instanceof Property\BooleanType: $param->setType('boolean'); break; case $parameter instanceof Property\DateType: $param->setType('string'); $param->setFormat('date'); break; case $parameter instanceof Property\DateTimeType: $param->setType('string'); $param->setFormat('date-time'); break; default: $param->setType('string'); break; } $param->setDescription($parameter->getDescription()); $param->setRequired($parameter->isRequired()); if ($parameter instanceof Property\DecimalType) { $param->setMinimum($parameter->getMin()); $param->setMaximum($parameter->getMax()); } elseif ($parameter instanceof Property\StringType) { $param->setMinimum($parameter->getMinLength()); $param->setMaximum($parameter->getMaxLength()); $param->setEnum($parameter->getEnumeration()); } }