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)); }
/** * Create all statements around url transformation * * @param Operation $operation * @param Expr\Variable $queryParamVariable * * @return array */ protected function createUrlStatements(Operation $operation, $queryParamVariable) { $urlVariable = new Expr\Variable('url'); // url = /path $statements = [new Expr\Assign($urlVariable, new Scalar\String_($operation->getPath()))]; if ($operation->getOperation()->getParameters()) { foreach ($operation->getOperation()->getParameters() as $parameter) { if ($parameter instanceof Reference) { $parameter = $this->getResolver()->resolve($parameter); } if ($parameter instanceof PathParameterSubSchema) { // $url = str_replace('{param}', $param, $url) $statements[] = new Expr\Assign($urlVariable, new Expr\FuncCall(new Name('str_replace'), [new Arg(new Scalar\String_('{' . $parameter->getName() . '}')), new Arg(new Expr\FuncCall(new Name('urlencode'), [new Arg(new Expr\Variable(Inflector::camelize($parameter->getName())))])), new Arg($urlVariable)])); } } } // url = url . ? . $queryParam->buildQueryString $statements[] = new Expr\Assign($urlVariable, new Expr\BinaryOp\Concat($urlVariable, new Expr\BinaryOp\Concat(new Scalar\String_('?'), new Expr\MethodCall($queryParamVariable, 'buildQueryString', [new Arg(new Expr\Variable('parameters'))])))); return [$statements, $urlVariable]; }