Exemple #1
0
 protected function generateType(PropertyInterface $type, $data)
 {
     if ($type instanceof Property\ComplexType) {
         $fields = array();
         $properties = $type->getProperties();
         foreach ($properties as $name => $property) {
             if (isset($data[$name])) {
                 $fields[$name] = $this->generateType($property, $data[$name]);
             } elseif ($property->isRequired()) {
                 throw new RuntimeException('Missing sample data of required property ' . $name);
             }
         }
         return new Record($type->getName(), $fields);
     } elseif ($type instanceof Property\ArrayType) {
         if (is_array($data)) {
             $values = array();
             foreach ($data as $value) {
                 $values[] = $this->generateType($type->getPrototype(), $value);
             }
             return $values;
         } elseif ($type->isRequired()) {
             throw new RuntimeException('Missing sample data of required property ' . $type->getName());
         }
     } elseif ($type instanceof Property\BooleanType) {
         return (bool) $data;
     } elseif ($type instanceof Property\IntegerType) {
         return (int) $data;
     } elseif ($type instanceof Property\FloatType) {
         return (double) $data;
     } else {
         return (string) $data;
     }
 }
Exemple #2
0
 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;
     }
 }
Exemple #3
0
 protected function getValueDescription(PropertyInterface $type)
 {
     if ($type instanceof Property\ComplexType) {
         $span = '<span class="psx-property-type psx-property-type-complex"><a href="#psx-type-' . $type->getId() . '">' . $type->getName() . '</a></span>';
         return [$span, null];
     } elseif ($type instanceof Property\ArrayType) {
         $constraints = array();
         $min = $type->getMinLength();
         if ($min !== null) {
             $constraints['minimum'] = '<span class="psx-constraint-minimum">' . $min . '</span>';
         }
         $max = $type->getMaxLength();
         if ($max !== null) {
             $constraints['maximum'] = '<span class="psx-constraint-maximum">' . $max . '</span>';
         }
         $constraint = $this->constraintToString($constraints);
         $property = $this->getValueDescription($type->getPrototype());
         $span = '<span class="psx-property-type psx-property-type-array">Array&lt;' . $property[0] . '&gt;</span>';
         return [$span, $constraint];
     } elseif ($type instanceof Property\ChoiceType) {
         $choice = array();
         $properties = $type->getProperties();
         foreach ($properties as $prop) {
             $property = $this->getValueDescription($prop);
             $choice[] = $property[0];
         }
         $span = '<span class="psx-property-type psx-property-type-choice">' . implode('|', $choice) . '</span>';
         return [$span, null];
     } elseif ($type instanceof PropertySimpleAbstract) {
         $typeName = ucfirst($type->getTypeName());
         $constraints = array();
         if ($type->getPattern() !== null) {
             $constraints['pattern'] = '<span class="psx-constraint-pattern">' . $type->getPattern() . '</span>';
         }
         if ($type->getEnumeration() !== null) {
             $enumeration = '<ul class="psx-property-enumeration">';
             foreach ($type->getEnumeration() as $enum) {
                 $enumeration .= '<li><span class="psx-constraint-enumeration-value">' . $enum . '</span></li>';
             }
             $enumeration .= '</ul>';
             $constraints['enumeration'] = '<span class="psx-constraint-enumeration">' . $enumeration . '</span>';
         }
         if ($type instanceof Property\DecimalType) {
             $min = $type->getMin();
             if ($min !== null) {
                 $constraints['minimum'] = '<span class="psx-constraint-minimum">' . $min . '</span>';
             }
             $max = $type->getMax();
             if ($max !== null) {
                 $constraints['maximum'] = '<span class="psx-constraint-maximum">' . $max . '</span>';
             }
         } elseif ($type instanceof Property\StringType) {
             $min = $type->getMinLength();
             if ($min !== null) {
                 $constraints['minimum'] = '<span class="psx-constraint-minimum">' . $min . '</span>';
             }
             $max = $type->getMaxLength();
             if ($max !== null) {
                 $constraints['maximum'] = '<span class="psx-constraint-maximum">' . $max . '</span>';
             }
         }
         $constraint = $this->constraintToString($constraints);
         $cssClass = 'psx-property-type-' . strtolower($typeName);
         if ($type instanceof Property\DateType) {
             $typeName = '<a href="http://tools.ietf.org/html/rfc3339#section-5.6" title="RFC3339">Date</a>';
         } elseif ($type instanceof Property\DateTimeType) {
             $typeName = '<a href="http://tools.ietf.org/html/rfc3339#section-5.6" title="RFC3339">DateTime</a>';
         } elseif ($type instanceof Property\TimeType) {
             $typeName = '<a href="http://tools.ietf.org/html/rfc3339#section-5.6" title="RFC3339">Time</a>';
         } elseif ($type instanceof Property\DurationType) {
             $typeName = '<span title="ISO 8601">Duration</span>';
         }
         $span = '<span class="psx-property-type ' . $cssClass . '">' . $typeName . '</span>';
         return [$span, $constraint];
     }
 }