<?php
/**
 * {{classname}}
 * PHP version 5
 *
 * @category Class
 * @package  {{invokerPackage}}
 * @author   Swagger Codegen team
 * @link     https://github.com/swagger-api/swagger-codegen
 */

{{>partial_header}}
/**
 * NOTE: This class is auto generated by the swagger code generator program.
 * https://github.com/swagger-api/swagger-codegen
 * Do not edit the class manually.
 */

namespace {{apiPackage}};

use \{{invokerPackage}}\ApiClient;
use \{{invokerPackage}}\ApiException;
use \{{invokerPackage}}\Configuration;
use \{{invokerPackage}}\ObjectSerializer;

/**
 * {{classname}} Class Doc Comment
 *
 * @category Class
 * @package  {{invokerPackage}}
 * @author   Swagger Codegen team
 * @link     https://github.com/swagger-api/swagger-codegen
 */
{{#operations}}class {{classname}}
{
    /**
     * API Client
     *
     * @var \{{invokerPackage}}\ApiClient instance of the ApiClient
     */
    protected $apiClient;

    /**
     * Constructor
     *
     * @param \{{invokerPackage}}\ApiClient|null $apiClient The api client to use
     */
    public function __construct(\{{invokerPackage}}\ApiClient $apiClient = null)
    {
        if ($apiClient === null) {
            $apiClient = new ApiClient();
        }

        $this->apiClient = $apiClient;
    }

    /**
     * Get API client
     *
     * @return \{{invokerPackage}}\ApiClient get the API client
     */
    public function getApiClient()
    {
        return $this->apiClient;
    }

    /**
     * Set the API client
     *
     * @param \{{invokerPackage}}\ApiClient $apiClient set the API client
     *
     * @return {{classname}}
     */
    public function setApiClient(\{{invokerPackage}}\ApiClient $apiClient)
    {
        $this->apiClient = $apiClient;
        return $this;
    }
    {{#operation}}

    /**
     * Operation {{{operationId}}}
     *
     * {{{summary}}}
     *
{{#description}}
     * {{.}}
     *
{{/description}}
{{#allParams}}
     * @param {{dataType}} ${{paramName}} {{description}} {{#required}}(required){{/required}}{{^required}}(optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}}
{{/allParams}}
     * @throws \{{invokerPackage}}\ApiException on non-2xx response
     * @return {{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}}
     */
    public function {{operationId}}({{#allParams}}${{paramName}}{{^required}} = null{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}})
    {
        list($response) = $this->{{operationId}}WithHttpInfo({{#allParams}}${{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}});
        return $response;
    }

    /**
     * Operation {{{operationId}}}WithHttpInfo
     *
     * {{{summary}}}
     *
{{#description}}
     * {{.}}
     *
{{/description}}
{{#allParams}}
     * @param {{dataType}} ${{paramName}} {{description}} {{#required}}(required){{/required}}{{^required}}(optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}}
{{/allParams}}
     * @throws \{{invokerPackage}}\ApiException on non-2xx response
     * @return array of {{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}null{{/returnType}}, HTTP status code, HTTP response headers (array of strings)
     */
    public function {{operationId}}WithHttpInfo({{#allParams}}${{paramName}}{{^required}} = null{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}})
    {
        {{#allParams}}
        {{#required}}
        // verify the required parameter '{{paramName}}' is set
        if (${{paramName}} === null) {
            throw new \InvalidArgumentException('Missing the required parameter ${{paramName}} when calling {{operationId}}');
        }
        {{/required}}
        {{#hasValidation}}
        {{#maxLength}}
        if ({{^required}}!is_null(${{paramName}}) && {{/required}}(strlen(${{paramName}}) > {{maxLength}})) {
            throw new \InvalidArgumentException('invalid length for "${{paramName}}" when calling {{classname}}.{{operationId}}, must be smaller than or equal to {{maxLength}}.');
        }
        {{/maxLength}}
        {{#minLength}}
        if ({{^required}}!is_null(${{paramName}}) && {{/required}}(strlen(${{paramName}}) < {{minLength}})) {
            throw new \InvalidArgumentException('invalid length for "${{paramName}}" when calling {{classname}}.{{operationId}}, must be bigger than or equal to {{minLength}}.');
        }
        {{/minLength}}
        {{#maximum}}
        if ({{^required}}!is_null(${{paramName}}) && {{/required}}(${{paramName}} >{{#exclusiveMaximum}}={{/exclusiveMaximum}} {{maximum}})) {
            throw new \InvalidArgumentException('invalid value for "${{paramName}}" when calling {{classname}}.{{operationId}}, must be smaller than {{^exclusiveMaximum}}or equal to {{/exclusiveMaximum}}{{maximum}}.');
        }
        {{/maximum}}
        {{#minimum}}
        if ({{^required}}!is_null(${{paramName}}) && {{/required}}(${{paramName}} <{{#exclusiveMinimum}}={{/exclusiveMinimum}} {{minimum}})) {
            throw new \InvalidArgumentException('invalid value for "${{paramName}}" when calling {{classname}}.{{operationId}}, must be bigger than {{^exclusiveMinimum}}or equal to {{/exclusiveMinimum}}{{minimum}}.');
        }
        {{/minimum}}
        {{#pattern}}
        if ({{^required}}!is_null(${{paramName}}) && {{/required}}!preg_match("{{{pattern}}}", ${{paramName}})) {
            throw new \InvalidArgumentException("invalid value for \"{{paramName}}\" when calling {{classname}}.{{operationId}}, must conform to the pattern {{{pattern}}}.");
        }
        {{/pattern}}
        {{#maxItems}}
        if ({{^required}}!is_null(${{paramName}}) && {{/required}}(count(${{paramName}}) > {{maxItems}})) {
            throw new \InvalidArgumentException('invalid value for "${{paramName}}" when calling {{classname}}.{{operationId}}, number of items must be less than or equal to {{maxItems}}.');
        }
        {{/maxItems}}
        {{#minItems}}
        if ({{^required}}!is_null(${{paramName}}) && {{/required}}(count(${{paramName}}) < {{minItems}})) {
            throw new \InvalidArgumentException('invalid value for "${{paramName}}" when calling {{classname}}.{{operationId}}, number of items must be greater than or equal to {{minItems}}.');
        }
        {{/minItems}}

        {{/hasValidation}}
        {{/allParams}}
        // parse inputs
        $resourcePath = "{{path}}";
        $httpBody = '';
        $queryParams = [];
        $headerParams = [];
        $formParams = [];
        $_header_accept = $this->apiClient->selectHeaderAccept([{{#produces}}'{{{mediaType}}}'{{#hasMore}}, {{/hasMore}}{{/produces}}]);
        if (!is_null($_header_accept)) {
            $headerParams['Accept'] = $_header_accept;
        }
        $headerParams['Content-Type'] = $this->apiClient->selectHeaderContentType([{{#consumes}}'{{{mediaType}}}'{{#hasMore}}, {{/hasMore}}{{/consumes}}]);

        {{#queryParams}}
        // query params
        {{#collectionFormat}}
        if (is_array(${{paramName}})) {
            ${{paramName}} = $this->apiClient->getSerializer()->serializeCollection(${{paramName}}, '{{collectionFormat}}', true);
        }
        {{/collectionFormat}}
        if (${{paramName}} !== null) {
            $queryParams['{{baseName}}'] = $this->apiClient->getSerializer()->toQueryValue(${{paramName}});
        }
        {{/queryParams}}
        {{#headerParams}}
        // header params
        {{#collectionFormat}}
        if (is_array(${{paramName}})) {
            ${{paramName}} = $this->apiClient->getSerializer()->serializeCollection(${{paramName}}, '{{collectionFormat}}');
        }
        {{/collectionFormat}}
        if (${{paramName}} !== null) {
            $headerParams['{{baseName}}'] = $this->apiClient->getSerializer()->toHeaderValue(${{paramName}});
        }
        {{/headerParams}}
        {{#pathParams}}
        // path params
        {{#collectionFormat}}
        if (is_array(${{paramName}})) {
            ${{paramName}} = $this->apiClient->getSerializer()->serializeCollection(${{paramName}}, '{{collectionFormat}}');
        }
        {{/collectionFormat}}
        if (${{paramName}} !== null) {
            $resourcePath = str_replace(
                "{" . "{{baseName}}" . "}",
                $this->apiClient->getSerializer()->toPathValue(${{paramName}}),
                $resourcePath
            );
        }
        {{/pathParams}}
        // default format to json
        $resourcePath = str_replace("{format}", "json", $resourcePath);

        {{#formParams}}
        // form params
        if (${{paramName}} !== null) {
            {{#isFile}}
            // PHP 5.5 introduced a CurlFile object that deprecates the old @filename syntax
            // See: https://wiki.php.net/rfc/curl-file-upload
            if (function_exists('curl_file_create')) {
                $formParams['{{baseName}}'] = curl_file_create($this->apiClient->getSerializer()->toFormValue(${{paramName}}));
            } else {
                $formParams['{{baseName}}'] = '@' . $this->apiClient->getSerializer()->toFormValue(${{paramName}});
            }
            {{/isFile}}
            {{^isFile}}
            $formParams['{{baseName}}'] = $this->apiClient->getSerializer()->toFormValue(${{paramName}});
            {{/isFile}}
        }
        {{/formParams}}
        {{#bodyParams}}// body params
        $_tempBody = null;
        if (isset(${{paramName}})) {
            $_tempBody = ${{paramName}};
        }
        {{/bodyParams}}

        // for model (json/xml)
        if (isset($_tempBody)) {
            $httpBody = $_tempBody; // $_tempBody is the method argument, if present
        } elseif (count($formParams) > 0) {
            $httpBody = $formParams; // for HTTP post (form)
        }
        {{#authMethods}}
        {{#isApiKey}}
        // this endpoint requires API key authentication
        $apiKey = $this->apiClient->getApiKeyWithPrefix('{{keyParamName}}');
        if (strlen($apiKey) !== 0) {
            {{#isKeyInHeader}}$headerParams['{{keyParamName}}'] = $apiKey;{{/isKeyInHeader}}{{#isKeyInQuery}}$queryParams['{{keyParamName}}'] = $apiKey;{{/isKeyInQuery}}
        }
        {{/isApiKey}}
        {{#isBasic}}
        // this endpoint requires HTTP basic authentication
        if (strlen($this->apiClient->getConfig()->getUsername()) !== 0 or strlen($this->apiClient->getConfig()->getPassword()) !== 0) {
            $headerParams['Authorization'] = 'Basic ' . base64_encode($this->apiClient->getConfig()->getUsername() . ":" . $this->apiClient->getConfig()->getPassword());
        }
        {{/isBasic}}
        {{#isOAuth}}
        // this endpoint requires OAuth (access token)
        if (strlen($this->apiClient->getConfig()->getAccessToken()) !== 0) {
            $headerParams['Authorization'] = 'Bearer ' . $this->apiClient->getConfig()->getAccessToken();
        }
        {{/isOAuth}}
        {{/authMethods}}
        // make the API Call
        try {
            list($response, $statusCode, $httpHeader) = $this->apiClient->callApi(
                $resourcePath,
                '{{httpMethod}}',
                $queryParams,
                $httpBody,
                $headerParams,
                {{#returnType}}
                '{{returnType}}',
                {{/returnType}}
                {{^returnType}}
                null,
                {{/returnType}}
                '{{path}}'
            );

            {{#returnType}}
            return [$this->apiClient->getSerializer()->deserialize($response, '{{returnType}}', $httpHeader), $statusCode, $httpHeader];
            {{/returnType}}
            {{^returnType}}
            return [null, $statusCode, $httpHeader];
            {{/returnType}}
        } catch (ApiException $e) {
            switch ($e->getCode()) {
            {{#responses}}
              {{#dataType}}
                {{^isWildcard}}case {{code}}:{{/isWildcard}}{{#isWildcard}}default:{{/isWildcard}}
                    $data = $this->apiClient->getSerializer()->deserialize($e->getResponseBody(), '{{dataType}}', $e->getResponseHeaders());
                    $e->setResponseObject($data);
                    break;
              {{/dataType}}
            {{/responses}}
            }

            throw $e;
        }
    }
    {{/operation}}
}
{{/operations}}
