github.com/phrase/openapi@v0.0.0-20240514140800-49e8a106740e/openapi-generator/templates/php/Configuration.mustache (about)

     1  <?php
     2  /**
     3   * Configuration
     4   * PHP version 5
     5   *
     6   * @category Class
     7   * @package  {{invokerPackage}}
     8   * @author   OpenAPI Generator team
     9   * @link     https://openapi-generator.tech
    10   */
    11  
    12  {{>partial_header}}
    13  /**
    14   * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
    15   * https://openapi-generator.tech
    16   * Do not edit the class manually.
    17   */
    18  
    19  namespace {{invokerPackage}};
    20  
    21  /**
    22   * Configuration Class Doc Comment
    23   * PHP version 5
    24   *
    25   * @category Class
    26   * @package  {{invokerPackage}}
    27   * @author   OpenAPI Generator team
    28   * @link     https://openapi-generator.tech
    29   */
    30  class Configuration
    31  {
    32      private static $defaultConfiguration;
    33  
    34      /**
    35       * Associate array to store API key(s)
    36       *
    37       * @var string[]
    38       */
    39      protected $apiKeys = [];
    40  
    41      /**
    42       * Associate array to store API prefix (e.g. Bearer)
    43       *
    44       * @var string[]
    45       */
    46      protected $apiKeyPrefixes = [];
    47  
    48      /**
    49       * Access token for OAuth/Bearer authentication
    50       *
    51       * @var string
    52       */
    53      protected $accessToken = '';
    54  
    55      /**
    56       * Username for HTTP basic authentication
    57       *
    58       * @var string
    59       */
    60      protected $username = '';
    61  
    62      /**
    63       * Password for HTTP basic authentication
    64       *
    65       * @var string
    66       */
    67      protected $password = '';
    68  
    69      /**
    70       * The host
    71       *
    72       * @var string
    73       */
    74      protected $host = '{{basePath}}';
    75  
    76      /**
    77       * User agent of the HTTP request, set to "OpenAPI-Generator/{version}/PHP" by default
    78       *
    79       * @var string
    80       */
    81      protected $userAgent = '{{#httpUserAgent}}{{{.}}}{{/httpUserAgent}}{{^httpUserAgent}}OpenAPI-Generator/{{#artifactVersion}}{{{.}}}{{/artifactVersion}}{{^artifactVersion}}1.0.0{{/artifactVersion}}/PHP{{/httpUserAgent}}';
    82  
    83      /**
    84       * Debug switch (default set to false)
    85       *
    86       * @var bool
    87       */
    88      protected $debug = false;
    89  
    90      /**
    91       * Debug file location (log to STDOUT by default)
    92       *
    93       * @var string
    94       */
    95      protected $debugFile = 'php://output';
    96  
    97      /**
    98       * Debug file location (log to STDOUT by default)
    99       *
   100       * @var string
   101       */
   102      protected $tempFolderPath;
   103  
   104      /**
   105       * Constructor
   106       */
   107      public function __construct()
   108      {
   109          $this->tempFolderPath = sys_get_temp_dir();
   110      }
   111  
   112      /**
   113       * Sets API key
   114       *
   115       * @param string $apiKeyIdentifier API key identifier (authentication scheme)
   116       * @param string $key              API key or token
   117       *
   118       * @return $this
   119       */
   120      public function setApiKey($apiKeyIdentifier, $key)
   121      {
   122          $this->apiKeys[$apiKeyIdentifier] = $key;
   123          return $this;
   124      }
   125  
   126      /**
   127       * Gets API key
   128       *
   129       * @param string $apiKeyIdentifier API key identifier (authentication scheme)
   130       *
   131       * @return string API key or token
   132       */
   133      public function getApiKey($apiKeyIdentifier)
   134      {
   135          return isset($this->apiKeys[$apiKeyIdentifier]) ? $this->apiKeys[$apiKeyIdentifier] : null;
   136      }
   137  
   138      /**
   139       * Sets the prefix for API key (e.g. Bearer)
   140       *
   141       * @param string $apiKeyIdentifier API key identifier (authentication scheme)
   142       * @param string $prefix           API key prefix, e.g. Bearer
   143       *
   144       * @return $this
   145       */
   146      public function setApiKeyPrefix($apiKeyIdentifier, $prefix)
   147      {
   148          $this->apiKeyPrefixes[$apiKeyIdentifier] = $prefix;
   149          return $this;
   150      }
   151  
   152      /**
   153       * Gets API key prefix
   154       *
   155       * @param string $apiKeyIdentifier API key identifier (authentication scheme)
   156       *
   157       * @return string
   158       */
   159      public function getApiKeyPrefix($apiKeyIdentifier)
   160      {
   161          return isset($this->apiKeyPrefixes[$apiKeyIdentifier]) ? $this->apiKeyPrefixes[$apiKeyIdentifier] : null;
   162      }
   163  
   164      /**
   165       * Sets the access token for OAuth
   166       *
   167       * @param string $accessToken Token for OAuth
   168       *
   169       * @return $this
   170       */
   171      public function setAccessToken($accessToken)
   172      {
   173          $this->accessToken = $accessToken;
   174          return $this;
   175      }
   176  
   177      /**
   178       * Gets the access token for OAuth
   179       *
   180       * @return string Access token for OAuth
   181       */
   182      public function getAccessToken()
   183      {
   184          return $this->accessToken;
   185      }
   186  
   187      /**
   188       * Sets the username for HTTP basic authentication
   189       *
   190       * @param string $username Username for HTTP basic authentication
   191       *
   192       * @return $this
   193       */
   194      public function setUsername($username)
   195      {
   196          $this->username = $username;
   197          return $this;
   198      }
   199  
   200      /**
   201       * Gets the username for HTTP basic authentication
   202       *
   203       * @return string Username for HTTP basic authentication
   204       */
   205      public function getUsername()
   206      {
   207          return $this->username;
   208      }
   209  
   210      /**
   211       * Sets the password for HTTP basic authentication
   212       *
   213       * @param string $password Password for HTTP basic authentication
   214       *
   215       * @return $this
   216       */
   217      public function setPassword($password)
   218      {
   219          $this->password = $password;
   220          return $this;
   221      }
   222  
   223      /**
   224       * Gets the password for HTTP basic authentication
   225       *
   226       * @return string Password for HTTP basic authentication
   227       */
   228      public function getPassword()
   229      {
   230          return $this->password;
   231      }
   232  
   233      /**
   234       * Sets the host
   235       *
   236       * @param string $host Host
   237       *
   238       * @return $this
   239       */
   240      public function setHost($host)
   241      {
   242          $this->host = $host;
   243          return $this;
   244      }
   245  
   246      /**
   247       * Gets the host
   248       *
   249       * @return string Host
   250       */
   251      public function getHost()
   252      {
   253          return $this->host;
   254      }
   255  
   256      /**
   257       * Sets the user agent of the api client
   258       *
   259       * @param string $userAgent the user agent of the api client
   260       *
   261       * @throws \InvalidArgumentException
   262       * @return $this
   263       */
   264      public function setUserAgent($userAgent)
   265      {
   266          if (!is_string($userAgent)) {
   267              throw new \InvalidArgumentException('User-agent must be a string.');
   268          }
   269  
   270          $this->userAgent = $userAgent;
   271          return $this;
   272      }
   273  
   274      /**
   275       * Gets the user agent of the api client
   276       *
   277       * @return string user agent
   278       */
   279      public function getUserAgent()
   280      {
   281          return $this->userAgent;
   282      }
   283  
   284      /**
   285       * Sets debug flag
   286       *
   287       * @param bool $debug Debug flag
   288       *
   289       * @return $this
   290       */
   291      public function setDebug($debug)
   292      {
   293          $this->debug = $debug;
   294          return $this;
   295      }
   296  
   297      /**
   298       * Gets the debug flag
   299       *
   300       * @return bool
   301       */
   302      public function getDebug()
   303      {
   304          return $this->debug;
   305      }
   306  
   307      /**
   308       * Sets the debug file
   309       *
   310       * @param string $debugFile Debug file
   311       *
   312       * @return $this
   313       */
   314      public function setDebugFile($debugFile)
   315      {
   316          $this->debugFile = $debugFile;
   317          return $this;
   318      }
   319  
   320      /**
   321       * Gets the debug file
   322       *
   323       * @return string
   324       */
   325      public function getDebugFile()
   326      {
   327          return $this->debugFile;
   328      }
   329  
   330      /**
   331       * Sets the temp folder path
   332       *
   333       * @param string $tempFolderPath Temp folder path
   334       *
   335       * @return $this
   336       */
   337      public function setTempFolderPath($tempFolderPath)
   338      {
   339          $this->tempFolderPath = $tempFolderPath;
   340          return $this;
   341      }
   342  
   343      /**
   344       * Gets the temp folder path
   345       *
   346       * @return string Temp folder path
   347       */
   348      public function getTempFolderPath()
   349      {
   350          return $this->tempFolderPath;
   351      }
   352  
   353      /**
   354       * Gets the default configuration instance
   355       *
   356       * @return Configuration
   357       */
   358      public static function getDefaultConfiguration()
   359      {
   360          if (self::$defaultConfiguration === null) {
   361              self::$defaultConfiguration = new Configuration();
   362          }
   363  
   364          return self::$defaultConfiguration;
   365      }
   366  
   367      /**
   368       * Sets the detault configuration instance
   369       *
   370       * @param Configuration $config An instance of the Configuration Object
   371       *
   372       * @return void
   373       */
   374      public static function setDefaultConfiguration(Configuration $config)
   375      {
   376          self::$defaultConfiguration = $config;
   377      }
   378  
   379      /**
   380       * Gets the essential information for debugging
   381       *
   382       * @return string The report for debugging
   383       */
   384      public static function toDebugReport()
   385      {
   386          $report  = 'PHP SDK ({{invokerPackage}}) Debug Report:' . PHP_EOL;
   387          $report .= '    OS: ' . php_uname() . PHP_EOL;
   388          $report .= '    PHP Version: ' . PHP_VERSION . PHP_EOL;
   389          $report .= '    The version of the OpenAPI document: {{version}}' . PHP_EOL;
   390  	    {{#artifactVersion}}
   391          $report .= '    SDK Package Version: {{artifactVersion}}' . PHP_EOL;
   392      	{{/artifactVersion}}
   393          $report .= '    Temp Folder Path: ' . self::getDefaultConfiguration()->getTempFolderPath() . PHP_EOL;
   394  
   395          return $report;
   396      }
   397  
   398      /**
   399       * Get API key (with prefix if set)
   400       *
   401       * @param  string $apiKeyIdentifier name of apikey
   402       *
   403       * @return string API key with the prefix
   404       */
   405      public function getApiKeyWithPrefix($apiKeyIdentifier)
   406      {
   407          $prefix = $this->getApiKeyPrefix($apiKeyIdentifier);
   408          $apiKey = $this->getApiKey($apiKeyIdentifier);
   409  
   410          if ($apiKey === null) {
   411              return null;
   412          }
   413  
   414          if ($prefix === null) {
   415              $keyWithPrefix = $apiKey;
   416          } else {
   417              $keyWithPrefix = $prefix . ' ' . $apiKey;
   418          }
   419  
   420          return $keyWithPrefix;
   421      }
   422  
   423      /**
   424       * Returns an array of host settings
   425       *
   426       * @return an array of host settings
   427       */
   428      public function getHostSettings()
   429      {
   430          return array(
   431          {{#servers}}
   432            array(
   433              "url" => "{{{url}}}",
   434              "description" => "{{{description}}}{{^description}}No description provided{{/description}}",
   435              {{#variables}}
   436              {{#-first}}
   437              "variables" => array(
   438              {{/-first}}
   439                "{{{name}}}" => array(
   440                    "description" => "{{{description}}}{{^description}}No description provided{{/description}}",
   441                    "default_value" => "{{{defaultValue}}}",
   442                    {{#enumValues}}
   443                    {{#-first}}
   444                    "enum_values" => array(
   445                    {{/-first}}
   446                      "{{{.}}}"{{^-last}},{{/-last}}
   447                    {{#-last}}
   448                    )
   449                    {{/-last}}
   450                    {{/enumValues}}
   451                  ){{^-last}},{{/-last}}
   452                {{#-last}}
   453                )
   454                {{/-last}}
   455              {{/variables}}
   456            ){{^-last}},{{/-last}}
   457          {{/servers}}
   458          );
   459      }
   460  
   461      /**
   462       * Returns URL based on the index and variables
   463       *
   464       * @param index array index of the host settings
   465       * @param variables hash of variable and the corresponding value (optional)
   466       * @return URL based on host settings
   467       */
   468      public function getHostFromSettings($index, $variables = null)
   469      {
   470          if (null === $variables) {
   471              $variables = array();
   472          }
   473  
   474          $hosts = $this->getHostSettings();
   475  
   476          // check array index out of bound
   477          if ($index < 0 || $index >= sizeof($hosts)) {
   478              throw new \InvalidArgumentException("Invalid index $index when selecting the host. Must be less than ".sizeof($hosts));
   479          }
   480  
   481          $host = $hosts[$index];
   482          $url = $host["url"];
   483  
   484          // go through variable and assign a value
   485          foreach ($host["variables"] as $name => $variable) {
   486              if (array_key_exists($name, $variables)) { // check to see if it's in the variables provided by the user
   487                  if (in_array($variables[$name], $variable["enum_values"])) { // check to see if the value is in the enum
   488                      $url = str_replace("{".$name."}", $variables[$name], $url);
   489                  } else {
   490                      throw new \InvalidArgumentException("The variable `$name` in the host URL has invalid value ".$variables[$name].". Must be ".join(',', $variable["enum_values"]).".");
   491                  }
   492              } else {
   493                  // use default value
   494                  $url = str_replace("{".$name."}", $variable["default_value"], $url);
   495              }
   496          }
   497  
   498          return $url;
   499      }
   500  }