private function extractUrl()

in util/Endpoint.php [239:322]


    private function extractUrl(array $paths): string
    {
        $skeleton = file_get_contents(self::REQUIRED_PART_TEMPLATE);
        $checkPart = '';
        $params = '';
        $deprecated = '';

        $tab8 = str_repeat(' ', 8);
        $tab12 = str_repeat(' ', 12);

        if (!empty($this->parts)) {
            foreach ($this->parts as $part => $value) {
                if (in_array($part, $this->requiredParts)) {
                    $checkPart .= str_replace(
                        ':endpoint',
                        $this->name,
                        str_replace(':part', $part, $skeleton)
                    );
                    $this->addNamespace('OpenSearch\Common\Exceptions\RuntimeException');
                } else {
                    $params .= sprintf("%s\$%s = \$this->%s ?? null;\n", $tab8, $part, $part);
                }
                if ($part === 'type') {
                    $deprecated .= str_replace(
                        ':msg',
                        'Specifying types in urls has been deprecated',
                        str_replace(':part', $part, file_get_contents(self::DEPRECATED_PART))
                    );
                } elseif (isset($value['deprecated']) && $value['deprecated']) {
                    $deprecated .= str_replace(
                        ':msg',
                        $this->getDeprecatedMessage($part),
                        str_replace(':part', $part, file_get_contents(self::DEPRECATED_PART))
                    );
                }
            }
        }
        $else = '';
        $urls = '';
        // Extract the paths to manage (removing deprecated path, duplicate, etc)
        $pathsToManage = $this->extractPaths($paths);

        $lastUrlReturn = false;
        foreach ($pathsToManage as $path) {
            $parts = $this->getPartsFromUrl($path);
            if (empty($parts)) {
                $else = sprintf("\n%sreturn \"%s\";", $tab8, $path);
                $lastUrlReturn = true;
                continue;
            }
            $check = '';
            if (!in_array($parts[0], $this->requiredParts)) {
                $check = sprintf("isset(\$%s)", $parts[0]);
            }
            $url = str_replace('{' . $parts[0] .'}', '$' . $parts[0], $path);
            for ($i=1; $i<count($parts); $i++) {
                $url = str_replace('{' . $parts[$i] .'}', '$' . $parts[$i], $url);
                if (in_array($parts[$i], $this->requiredParts)) {
                    continue;
                }
                $check .= sprintf("%sisset(\$%s)", empty($check) ? '' : ' && ', $parts[$i]);
            }
            // Fix for missing / at the beginning of URL
            // @see https://github.com/elastic/elasticsearch-php/pull/970
            if ($url[0] !== '/') {
                $url = '/' . $url;
            }
            if (empty($check)) {
                $urls .= sprintf("\n%sreturn \"%s\";", $tab8, $url);
                $lastUrlReturn = true;
            } else {
                $urls .= sprintf("\n%sif (%s) {\n%sreturn \"%s\";\n%s}", $tab8, $check, $tab12, $url, $tab8);
            }
        }
        if (!$lastUrlReturn) {
            $urls .= sprintf(
                "\n%sthrow new RuntimeException('Missing parameter for the endpoint %s');",
                $tab8,
                $this->apiName
            );
            $this->addNamespace('OpenSearch\Common\Exceptions\RuntimeException');
        }
        return $checkPart . $params . $deprecated . $urls . $else;
    }