Example #1
0
 /**
  * {@inheritDoc}
  */
 public function guessType($object, $name, $classes)
 {
     $discriminants = [];
     $required = $object->getRequired() ?: [];
     foreach ($object->getProperties() as $key => $property) {
         if (!in_array($key, $required)) {
             continue;
         }
         if ($property instanceof Reference) {
             $property = $this->resolver->resolve($property);
         }
         if ($property->getEnum() !== null) {
             $isSimple = true;
             foreach ($property->getEnum() as $value) {
                 if (is_array($value) || is_object($value)) {
                     $isSimple = false;
                 }
             }
             if ($isSimple) {
                 $discriminants[$key] = $property->getEnum();
             }
         } else {
             $discriminants[$key] = null;
         }
     }
     return new ObjectType($object, $this->naming->getClassName($name), $discriminants);
 }
 /**
  * Generate a method for an operation
  *
  * @param string    $name
  * @param Operation $operation
  * @param Context   $context
  *
  * @return Stmt\ClassMethod
  */
 public function generate($name, Operation $operation, Context $context)
 {
     // Input
     list($queryParamDocumentation, $queryParamStatements, $queryParamVariable) = $this->createQueryParamStatements($operation);
     list($documentationParameters, $parameters) = $this->createParameters($operation, $queryParamDocumentation, $context);
     list($urlStatements, $urlVariable) = $this->createUrlStatements($operation, $queryParamVariable);
     list($bodyStatements, $bodyVariable) = $this->createBodyStatements($operation, $queryParamVariable, $context);
     list($headerStatements, $headerVariable) = $this->createHeaderStatements($operation, $queryParamVariable);
     $statements = array_merge($queryParamStatements, $urlStatements, $headerStatements, $bodyStatements, [new Expr\Assign(new Expr\Variable('request'), new Expr\MethodCall(new Expr\PropertyFetch(new Expr\Variable('this'), 'messageFactory'), 'createRequest', [new Arg(new Scalar\String_($operation->getMethod())), new Arg($urlVariable), new Arg($headerVariable), new Arg($bodyVariable)])), new Expr\Assign(new Expr\Variable('promise'), new Expr\MethodCall(new Expr\PropertyFetch(new Expr\Variable('this'), 'httpClient'), 'sendAsyncRequest', [new Arg(new Expr\Variable('request'))])), new Stmt\If_(new Expr\BinaryOp\Identical(new Expr\ConstFetch(new Name('self::FETCH_PROMISE')), new Expr\Variable('fetch')), ['stmts' => [new Stmt\Return_(new Expr\Variable('promise'))]]), new Expr\Assign(new Expr\Variable('response'), new Expr\MethodCall(new Expr\Variable('promise'), 'wait'))]);
     // Output
     $outputStatements = [];
     $outputTypes = ["\\Psr\\Http\\Message\\ResponseInterface"];
     foreach ($operation->getOperation()->getResponses() as $status => $response) {
         if ($response instanceof Reference) {
             $response = $this->resolver->resolve($response);
         }
         list($outputType, $ifStatus) = $this->createResponseDenormalizationStatement($status, $response->getSchema(), $context);
         if (null !== $outputType) {
             if (!in_array($outputType, $outputTypes)) {
                 $outputTypes[] = $outputType;
             }
             $outputStatements[] = $ifStatus;
         }
     }
     if (!empty($outputStatements)) {
         $statements[] = new Stmt\If_(new Expr\BinaryOp\Equal(new Expr\ConstFetch(new Name('self::FETCH_OBJECT')), new Expr\Variable('fetch')), ['stmts' => $outputStatements]);
     }
     // return $response
     $statements[] = new Stmt\Return_(new Expr\Variable('response'));
     $documentation = array_merge(['/**', sprintf(" * %s", $operation->getOperation()->getDescription()), ' *'], $documentationParameters, [' *', ' * @return ' . implode('|', $outputTypes), ' */']);
     return new Stmt\ClassMethod($name, ['type' => Stmt\Class_::MODIFIER_PUBLIC, 'params' => $parameters, 'stmts' => $statements], ['comments' => [new Comment\Doc(implode("\n", $documentation))]]);
 }
Example #3
0
 /**
  * {@inheritdoc}
  */
 public function guessType($object, $name, $classes)
 {
     $resolved = $this->resolver->resolve($object);
     if (array_key_exists(spl_object_hash($resolved), $classes)) {
         $name = $classes[spl_object_hash($resolved)]->getName();
     }
     return $this->chainGuesser->guessType($resolved, $name, $classes);
 }
Example #4
0
 /**
  * {@inheritdoc}
  */
 public function guessProperties($object, $name, $classes)
 {
     $properties = [];
     foreach ($object->getAllOf() as $allOfSchema) {
         if ($allOfSchema instanceof Reference) {
             $allOfSchema = $this->resolver->resolve($allOfSchema);
         }
         $properties = array_merge($properties, $this->chainGuesser->guessProperties($allOfSchema, $name, $classes));
     }
     return $properties;
 }
Example #5
0
 /**
  * {@inheritDoc}
  */
 public function guessClass($object, $name)
 {
     $classes = [];
     foreach ($object->getOneOf() as $oneOf) {
         $oneOfName = $name . 'Sub';
         $oneOfResolved = $oneOf;
         if ($oneOf instanceof Reference) {
             $oneOfName = array_pop(explode('/', $oneOf->getFragment()));
             $oneOfResolved = $this->resolver->resolve($oneOf);
         }
         $merged = $this->jsonSchemaMerger->merge($object, $oneOfResolved);
         $classes = array_merge($classes, $this->chainGuesser->guessClass($merged, $oneOfName));
         if ($oneOf instanceof Reference) {
             $oneOf->setResolved($merged);
         }
     }
     return $classes;
 }
Example #6
0
 /**
  * {@inheritDoc}
  */
 public function guessType($object, $name, $classes)
 {
     $type = null;
     foreach ($object->getAllOf() as $allOf) {
         $allOfSchema = $allOf;
         if ($allOfSchema instanceof Reference) {
             $allOfSchema = $this->resolver->resolve($allOf);
         }
         if (null !== $allOfSchema->getType()) {
             if (null !== $type) {
                 throw new \RuntimeException('an allOf instruction with 2 or more types is strictly impossible, check your schema');
             }
             $type = $this->chainGuesser->guessType($allOf, $name, $classes);
         }
     }
     if ($type === null) {
         return new Type($object, 'mixed');
     }
     return $type;
 }
 /**
  * @param         $status
  * @param         $schema
  * @param Context $context
  *
  * @return null|Stmt\If_
  */
 protected function createResponseDenormalizationStatement($status, $schema, Context $context)
 {
     $resolvedSchema = null;
     $array = false;
     if ($schema instanceof Reference) {
         $resolvedSchema = $this->resolver->resolve($schema);
     }
     if ($schema instanceof Schema && $schema->getType() == "array" && $schema->getItems() instanceof Reference) {
         $resolvedSchema = $this->resolver->resolve($schema->getItems());
         $array = true;
     }
     if ($resolvedSchema === null) {
         return [null, null];
     }
     $class = $context->getObjectClassMap()[spl_object_hash($resolvedSchema)];
     $class = $context->getNamespace() . "\\Model\\" . $class->getName();
     if ($array) {
         $class .= "[]";
     }
     return ["\\" . $class, new Stmt\If_(new Expr\BinaryOp\Equal(new Scalar\String_($status), new Expr\MethodCall(new Expr\Variable('response'), 'getStatusCode')), ['stmts' => [new Stmt\Return_(new Expr\MethodCall(new Expr\PropertyFetch(new Expr\Variable('this'), 'serializer'), 'deserialize', [new Arg(new Expr\MethodCall(new Expr\MethodCall(new Expr\Variable('response'), 'getBody'), 'getContents')), new Arg(new Scalar\String_($class)), new Arg(new Scalar\String_('json'))]))]])];
 }
 /**
  * @param BodyParameter $parameter
  * @param Context $context
  *
  * @return array
  */
 protected function getClass(BodyParameter $parameter, Context $context)
 {
     $resolvedSchema = null;
     $array = false;
     $schema = $parameter->getSchema();
     if ($schema instanceof Reference) {
         $resolvedSchema = $this->resolver->resolve($schema);
     }
     if ($schema instanceof Schema && $schema->getType() == "array" && $schema->getItems() instanceof Reference) {
         $resolvedSchema = $this->resolver->resolve($schema->getItems());
         $array = true;
     }
     if ($resolvedSchema === null) {
         return [$schema->getType(), null];
     }
     $class = $context->getObjectClassMap()[spl_object_hash($resolvedSchema)];
     $class = "\\" . $context->getNamespace() . "\\Model\\" . $class->getName();
     if ($array) {
         $class .= "[]";
     }
     return [$class, $array];
 }