public function generateFunctionName(Operation $operation) { $prefix = strtolower($operation->getMethod()); $parts = explode('/', $operation->getPath()); $parts = array_filter($parts, function ($part) { $part = trim($part); if (empty($part)) { return false; } if (preg_match('/^{(.+?)}$/', $part)) { return false; } return true; }); $parts = array_map(function ($part) { $part = trim($part); return ucfirst($part); }, $parts); return $prefix . implode('', $parts); }
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 headers statements * * @param Operation $operation * @param Expr\Variable $queryParamVariable * * @return array */ protected function createHeaderStatements(Operation $operation, $queryParamVariable) { $headerVariable = new Expr\Variable('headers'); return [[new Expr\Assign($headerVariable, new Expr\FuncCall(new Name('array_merge'), [new Arg(new Expr\Array_([new Expr\ArrayItem(new Scalar\String_($operation->getHost()), new Scalar\String_('Host'))])), new Arg(new Expr\MethodCall($queryParamVariable, 'buildHeaders', [new Arg(new Expr\Variable('parameters'))]))]))], $headerVariable]; }