Add multiple servers support to PHP client (#1964)

* add multiple server support

* update php samples

* update wording

* make variables optional
This commit is contained in:
William Cheng
2019-01-24 11:32:03 +08:00
committed by GitHub
parent 69323aec48
commit 33b7547da3
4 changed files with 277 additions and 0 deletions

View File

@@ -426,4 +426,59 @@ class Configuration
return $keyWithPrefix;
}
/**
* Returns an array of host settings
*
* @return an array of host settings
*/
public function getHostSettings()
{
return array(
array(
"url" => "http://petstore.swagger.io:80/v2",
"description" => "No description provided",
)
);
}
/**
* Returns URL based on the index and variables
*
* @param index array index of the host settings
* @param variables hash of variable and the corresponding value (optional)
* @return URL based on host settings
*/
public function getHostFromSettings($index, $variables = null)
{
if (null === $variables) {
$variables = array();
}
$hosts = $this->getHostSettings();
// check array index out of bound
if ($index < 0 || $index > sizeof($hosts)) {
throw new \InvalidArgumentException("Invalid index $index when selecting the host. Must be less than ".sizeof($hosts));
}
$host = $hosts[$index];
$url = $host["url"];
// go through variable and assign a value
foreach ($host["variables"] as $name => $variable) {
if (array_key_exists($name, $variables)) { // check to see if it's in the variables provided by the user
if (in_array($variables[$name], $variable["enum_values"])) { // check to see if the value is in the enum
$url = str_replace("{".$name."}", $variables[$name], $url);
} else {
throw new \InvalidArgumentException("The variable `$name` in the host URL has invalid value ".$variables[$name].". Must be ".join(',', $variable["enum_values"]).".");
}
} else {
// use default value
$url = str_replace("{".$name."}", $variable["default_value"], $url);
}
}
return $url;
}
}