public function generateFunctionName(Operation $operation) { $prefix = strtolower($operation->getMethod()); $shouldSingularize = true; $responses = $operation->getOperation()->getResponses(); if ($responses instanceof \ArrayObject && isset($responses[200])) { $response = $responses[200]; if ($response instanceof Response && $response->getSchema() instanceof Schema && $response->getSchema()->getType() === 'array') { $shouldSingularize = false; } } preg_match_all('/(?P<separator>[^a-zA-Z0-9_{}])+(?P<part>[a-zA-Z0-9_{}]*)/', $operation->getPath(), $matches); $methodNameParts = []; $lastNonParameterPartIndex = 0; foreach ($matches[0] as $index => $match) { if ($matches['separator'][$index] === '.') { continue; } $part = $matches['part'][$index]; if (preg_match_all('/{(?P<parameter>[^{}]+)}/', $part, $parameterMatches)) { foreach ($parameterMatches[0] as $parameterIndex => $parameterMatch) { $withoutSnakes = preg_replace_callback('/(^|_|\\.)+(.)/', function ($match) { return ('.' === $match[1] ? '_' : '') . strtoupper($match[2]); }, $parameterMatches['parameter'][$parameterIndex]); $methodNameParts[] = 'By' . ucfirst($withoutSnakes); } } else { $methodNameParts[] = ucfirst($part); $lastNonParameterPartIndex = count($methodNameParts) - 1; } } if ($shouldSingularize && count($methodNameParts) > 0) { $methodNameParts[$lastNonParameterPartIndex] = Inflector::singularize($methodNameParts[$lastNonParameterPartIndex]); } return $prefix . ucfirst(implode('', $methodNameParts)); }
public function generateFunctionName(Operation $operation) { return Inflector::camelize($operation->getOperation()->getOperationId()); }
/** * Create headers statements * * @param Operation $operation * @param Expr\Variable $queryParamVariable * * @return array */ protected function createHeaderStatements(Operation $operation, $queryParamVariable) { $headerVariable = new Expr\Variable('headers'); $headers = [new Expr\ArrayItem(new Scalar\String_($operation->getHost()), new Scalar\String_('Host'))]; $produces = $operation->getOperation()->getProduces(); if ($produces && in_array("application/json", $produces)) { $headers[] = new Expr\ArrayItem(new Expr\Array_([new Expr\ArrayItem(new Scalar\String_("application/json"))]), new Scalar\String_('Accept')); } $consumes = $operation->getOperation()->getProduces(); if ($operation->getOperation()->getParameters() && $consumes) { $bodyParameters = array_filter($operation->getOperation()->getParameters(), function ($parameter) { return $parameter instanceof BodyParameter; }); if (count($bodyParameters) > 0 && in_array("application/json", $consumes)) { $headers[] = new Expr\ArrayItem(new Scalar\String_("application/json"), new Scalar\String_('Content-Type')); } } return [[new Expr\Assign($headerVariable, new Expr\FuncCall(new Name('array_merge'), [new Arg(new Expr\Array_($headers)), new Arg(new Expr\MethodCall($queryParamVariable, 'buildHeaders', [new Arg(new Expr\Variable('parameters'))]))]))], $headerVariable]; }
/** * Create body statements * * @param Operation $operation * @param Expr\Variable $queryParamVariable * @param Context $context * * @return array */ protected function createBodyStatements(Operation $operation, $queryParamVariable, Context $context) { $bodyParameter = null; $bodyVariable = new Expr\Variable('body'); if ($operation->getOperation()->getParameters()) { foreach ($operation->getOperation()->getParameters() as $parameter) { if ($parameter instanceof BodyParameter) { $bodyParameter = $parameter; } } } if (null === $bodyParameter) { // $body = $queryParam->buildFormDataString($parameters); return [[new Expr\Assign($bodyVariable, new Expr\MethodCall($queryParamVariable, 'buildFormDataString', [new Arg(new Expr\Variable('parameters'))]))], $bodyVariable]; } // $body = $parameter if (!$bodyParameter->getSchema() instanceof Reference) { return [[new Expr\Assign($bodyVariable, new Expr\Variable(Inflector::camelize($bodyParameter->getName())))], $bodyVariable]; } // $body = $this->serializer->serialize($parameter); return [[new Expr\Assign($bodyVariable, new Expr\MethodCall(new Expr\PropertyFetch(new Expr\Variable('this'), 'serializer'), 'serialize', [new Arg(new Expr\Variable(Inflector::camelize($bodyParameter->getName()))), new Arg(new Scalar\String_('json'))]))], $bodyVariable]; }