diff --git a/modules/openapi-generator/src/main/resources/php/Configuration.mustache b/modules/openapi-generator/src/main/resources/php/Configuration.mustache index 44c80ecb7c85..32de830191c5 100644 --- a/modules/openapi-generator/src/main/resources/php/Configuration.mustache +++ b/modules/openapi-generator/src/main/resources/php/Configuration.mustache @@ -419,4 +419,82 @@ class Configuration return $keyWithPrefix; } + + /** + * Returns an array of host settings + * + * @return an array of host settings + */ + public function getHostSettings() + { + return array( + {{#servers}} + array( + "url" => "{{{url}}}", + "description" => "{{{description}}}{{^description}}No description provided{{/description}}", + {{#variables}} + {{#-first}} + "variables" => array( + {{/-first}} + "{{{name}}}" => array( + "description" => "{{{description}}}{{^description}}No description provided{{/description}}", + "default_value" => "{{{defaultValue}}}", + {{#enumValues}} + {{#-first}} + "enum_values" => array( + {{/-first}} + "{{{.}}}"{{^-last}},{{/-last}} + {{#-last}} + ) + {{/-last}} + {{/enumValues}} + ){{^-last}},{{/-last}} + {{#-last}} + ) + {{/-last}} + {{/variables}} + ){{^-last}},{{/-last}} + {{/servers}} + ); + } + + /** + * 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; + } } diff --git a/samples/client/petstore/php/OpenAPIClient-php/lib/Configuration.php b/samples/client/petstore/php/OpenAPIClient-php/lib/Configuration.php index cc602482c0eb..641f72e76636 100644 --- a/samples/client/petstore/php/OpenAPIClient-php/lib/Configuration.php +++ b/samples/client/petstore/php/OpenAPIClient-php/lib/Configuration.php @@ -426,4 +426,59 @@ public function getApiKeyWithPrefix($apiKeyIdentifier) 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; + } } diff --git a/samples/openapi3/client/petstore/php/OpenAPIClient-php/lib/Configuration.php b/samples/openapi3/client/petstore/php/OpenAPIClient-php/lib/Configuration.php index cc602482c0eb..42a68f7a3e0c 100644 --- a/samples/openapi3/client/petstore/php/OpenAPIClient-php/lib/Configuration.php +++ b/samples/openapi3/client/petstore/php/OpenAPIClient-php/lib/Configuration.php @@ -426,4 +426,92 @@ public function getApiKeyWithPrefix($apiKeyIdentifier) return $keyWithPrefix; } + + /** + * Returns an array of host settings + * + * @return an array of host settings + */ + public function getHostSettings() + { + return array( + array( + "url" => "http://{server}.swagger.io:{port}/v2", + "description" => "petstore server", + "variables" => array( + "server" => array( + "description" => "No description provided", + "default_value" => "petstore", + "enum_values" => array( + "petstore", + "qa-petstore", + "dev-petstore" + ) + ), + "port" => array( + "description" => "No description provided", + "default_value" => "80", + "enum_values" => array( + "80", + "8080" + ) + ) + ) + ), + array( + "url" => "https://localhost:8080/{version}", + "description" => "The local server", + "variables" => array( + "version" => array( + "description" => "No description provided", + "default_value" => "v2", + "enum_values" => array( + "v1", + "v2" + ) + ) + ) + ) + ); + } + + /** + * 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; + } } diff --git a/samples/openapi3/client/petstore/php/OpenAPIClient-php/tests/ConfigurationTest.php b/samples/openapi3/client/petstore/php/OpenAPIClient-php/tests/ConfigurationTest.php new file mode 100644 index 000000000000..983b871fd40a --- /dev/null +++ b/samples/openapi3/client/petstore/php/OpenAPIClient-php/tests/ConfigurationTest.php @@ -0,0 +1,56 @@ +getHostSettings(); + + $this->assertCount(2, $servers); + $this->assertSame("http://{server}.swagger.io:{port}/v2", $servers[0]["url"]); + $this->assertSame("petstore", $servers[0]["variables"]["server"]["default_value"]); + $this->assertSame("80", $servers[0]["variables"]["port"]["default_value"]); + $this->assertSame(array("80", "8080"), $servers[0]["variables"]["port"]["enum_values"]); + } + + /** + * Test server settings + */ + public function testServerUrl() + { + $config = new Configuration(); + // default value + $url = $config->getHostFromSettings(0); + $this->assertSame("http://petstore.swagger.io:80/v2", $url); + + // using a variable + $url = $config->getHostFromSettings(0, array("server" => "dev-petstore")); + $this->assertSame("http://dev-petstore.swagger.io:80/v2", $url); + + // using 2 variables + $url = $config->getHostFromSettings(0, array("server" => "dev-petstore", "port" => "8080")); + $this->assertSame("http://dev-petstore.swagger.io:8080/v2", $url); + } + + /** + * Test host settings with invalid vaues + * @expectedException InvalidArgumentException + * @expectedExceptionMessage The variable `port` in the host URL has invalid value 8. Must be 80,8080 + */ + public function testHostUrlWithInvalidValues() + { + // using 2 variables with invalid values + $config = new Configuration(); + $url = $config->getHostFromSettings(0, array("server" => "dev-petstore", "port" => "8")); + $this->assertSame("http://dev-petstore.swagger.io:8080/v2", $url); + } +}