Joli\Jane\OpenApi\Generator\OperationGenerator::generate PHP Méthode

generate() public méthode

Generate a method for an operation
public generate ( string $name, Joli\Jane\OpenApi\Operation\Operation $operation, Joli\Jane\Generator\Context\Context $context ) : ClassMethod
$name string
$operation Joli\Jane\OpenApi\Operation\Operation
$context Joli\Jane\Generator\Context\Context
Résultat PhpParser\Node\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))]]);
    }

Usage Example

 protected function generateClass($group, $operations, $namespace, Context $context, $suffix = 'Resource')
 {
     $factory = new BuilderFactory();
     $name = $group === 0 ? 'Default' : $group;
     $class = $factory->class(Inflector::classify($name . $suffix));
     $class->extend('Resource');
     foreach ($operations as $operation) {
         $class->addStmt($this->operationGenerator->generate($this->operationNaming->generateFunctionName($operation), $operation, $context));
     }
     return $factory->namespace($namespace . "\\Resource")->addStmt($factory->use('Joli\\Jane\\OpenApi\\Runtime\\Client\\QueryParam'))->addStmt($factory->use('Joli\\Jane\\OpenApi\\Runtime\\Client\\Resource'))->addStmt($class)->getNode();
 }