617 lines
36 KiB
PHP
617 lines
36 KiB
PHP
<?php
|
|
/**
|
|
* ObjectSerializer
|
|
*
|
|
* PHP version 7.4
|
|
*
|
|
* @category Class
|
|
* @package OpenAPI\Client
|
|
* @author OpenAPI Generator team
|
|
* @link https://openapi-generator.tech
|
|
*/
|
|
|
|
/**
|
|
* Instance API
|
|
*
|
|
* Scaleway Instances are virtual machines in the cloud. Different [Instance types](https://www.scaleway.com/en/docs/compute/instances/reference-content/choosing-instance-type/) offer different technical specifications in terms of vCPU, RAM, bandwidth and storage. Once you have created your Instance and installed your image of choice (e.g. an operating system), you can [connect to your Instance via SSH](https://www.scaleway.com/en/docs/compute/instances/how-to/connect-to-instance/) to use it as you wish. When you are done using the Instance, you can delete it from your account. (switchcolumn) <Message type=\"tip\"> To retrieve information about the different [images](#path-images) available to install on Scaleway Instances, check out our [Marketplace API](https://www.scaleway.com/en/developers/api/marketplace). </Message> (switchcolumn) ## Concepts Refer to our [dedicated concepts page](https://www.scaleway.com/en/docs/compute/instances/concepts/) to find definitions of all concepts and terminology related to Instances. (switchcolumn) (switchcolumn) ## Quickstart 1. Configure your environment variables <Message type=\"note\"> This is an optional step that seeks to simplify your usage of the Instances API. See [Availability Zones](#availability-zones) below for help choosing an Availability Zone. You can find your Project ID in the [Scaleway console](https://console.scaleway.com/project/settings). </Message> ```bash export SCW_SECRET_KEY=\"<API secret key>\" export SCW_DEFAULT_ZONE=\"<Scaleway Availability Zone>\" export SCW_PROJECT_ID=\"<Scaleway Project ID>\" ``` 2. **Create an Instance**: Run the following command to create an Instance. You can customize the details in the payload (name, description, type, tags etc) to your needs: use the information below to adjust the payload as necessary. ```bash curl -X POST \\ -H \"X-Auth-Token: $SCW_SECRET_KEY\" \\ -H \"Content-Type: application/json\" \\ \"https://api.scaleway.com/instance/v1/zones/$SCW_DEFAULT_ZONE/servers\" \\ -d '{ \"name\": \"my-new-instance\", \"project\": \"'\"$SCW_PROJECT_ID\"'\", \"commercial_type\": \"GP1-S\", \"image\": \"ubuntu_noble\", \"enable_ipv6\": true, \"volumes\": { \"0\":{ \"size\": 300000000000, \"volume_type\": \"l_ssd\" } } }' ``` | Parameter | Description | Valid values | | --------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | `name` | A name of your choice for the Instance (string) | Any string containing only alphanumeric characters, dots, spaces and dashes, e.g. `\"my-new-instance\"`. | | `project` | The Project in which the Instance should be created (string) | Any valid Scaleway Project ID (see above), e.g. `\"b4bd99e0-b389-11ed-afa1-0242ac120002\"` | | `commercial-type` | The commercial Instance type to create (string) | Any valid ID of a Scaleway commercial Instance type, e.g. `\"GP1-S\"`, `\"PRO2-M\"`. Use the [List Instance Types](#path-instance-types-list-instance-types) endpoint to get a list of all valid Instance types and their IDs. | | `image` | The image to install on the Instance, e.g. a particular OS (string) | Any Scaleway image label, e.g. `\"ubuntu_noble\"`, or any valid Scaleway image ID, e.g. `\"6fc0ade6-d6a3-4fb9-87ab-2444ac71e5c0\"` which is the ID for the `Ubuntu 24.04 Noble Numbat` image. Use the [List Instance Images](#path-images-list-instance-images) endpoint to get a list of all available images with their IDs and labels, or check out the [Scaleway Marketplace API](https://www.scaleway.com/en/developers/api/marketplace). | | `enable_ipv6` | Whether to enable IPv6 on the Instance (boolean) | `true` or `false` | | `volumes` | An object that specifies the storage volumes to attach to the Instance. For more information, see **Creating an Instance: the volumes object** in the [Technical information](#technical-information) section of this quickstart. | A (dictionary) object with a minimum of one key (`\"0\"`) whose value is another object containing the parameters `\"name\"` (a name for the volume), `\"size\"` (the size for the volume, in bytes), and `\"volume_type\"` (`\"l_ssd\"` or `\"b_ssd\"`). Additional keys for additional volumes should increment by 1 each time (the second volume would have a key of `1`.) Further parameters are available, and it is possible to attach existing volumes rather than creating a new one, or create a volume from a snapshot. | 3. **List your Instances**: run the following command to get a list of all the Instances in your account, with their details: ```bash curl -X GET \\ -H \"Content-Type: application/json\" \\ -H \"X-Auth-Token: $SCW_SECRET_KEY\" \\ \"https://api.scaleway.com/instance/v1/zones/$SCW_DEFAULT_ZONE/servers/\" ``` 4. **Delete an Instance**: run the following command to delete an Instance, specified by its Instance ID: ```bash curl -X DELETE \\ -H \"X-Auth-Token: $SCW_SECRET_KEY\" \\ -H \"Content-Type: application/json\" \\ \"https://api.scaleway.com/instance/v1/zones/$SCW_DEFAULT_ZONE/servers/<Instance-ID>\" ``` The expected successful response is empty. (switchcolumn) <Message type=\"requirement\"> - You have a [Scaleway account](https://console.scaleway.com/) - You have created an [API key](https://www.scaleway.com/en/docs/identity-and-access-management/iam/how-to/create-api-keys/) and that the API key has sufficient [IAM permissions](https://www.scaleway.com/en/docs/identity-and-access-management/iam/reference-content/permission-sets/) to perform the actions described on this page - You have [installed `curl`](https://curl.se/download.html) </Message> (switchcolumn) ## Technical information ### Availability Zones Instances can be deployed in the following Availability Zones: | Name | API ID | |-----------|-----------------------| | Paris | `fr-par-1` `fr-par-2` `fr-par-3` | | Amsterdam | `nl-ams-1` `nl-ams-2` | | Warsaw | `pl-waw-1` `pl-waw-2` | (switchcolumn) (switchcolumn) ### Pagination Most listing requests receive a paginated response. Requests against paginated endpoints accept two `query` arguments: - `page`, a positive integer to choose which page to return. - `per_page`, an positive integer lower or equal to 100 to select the number of items to return per page. The default value is `50`. Paginated endpoints usually also accept filters to search and sort results.These filters are documented along each endpoint documentation. The `X-Total-Count` header contains the total number of items returned. (switchcolumn) (switchcolumn) ### Creating an Instance: the volumes object When [creating an Instance](#path-instances-create-an-instance) using the Scaleway API, the `volumes` object is **not strictly required**. However, the defaults vary depending on certain conditions: 1. If an image label is used: - The default will be an `sbs_volume` volume. - The size of this volume will be the OS size (typically 10GB in most cases). 2. If an image ID from the marketplace is used (or any image based on a unified snapshot): - If the Instance supports local storage: - The default will be an `l_ssd` volume. - The size of this volume will be the instance's maximum local storage capacity. - If the Instance does not support local storage: - The default will be a `b_ssd` volume. - The size of this volume will be the OS size. If you want to customize the storage configuration or add additional volumes, you will need to include the volumes object in your API request. This object should contain at least one (dictionary) object with a minimum of one key (`\"0\"`) whose value is another object containing the parameters `\"name\"` (a name for the volume), `\"size\"` (the size for the volume, in bytes), and `\"volume_type\"` (`\"sbs_volume\"`, `\"l_ssd\"` or `\"b_ssd\"`). Additional keys for additional volumes should increment by 1 each time (the second volume would have a key of `\"1\"`.) Note that volume `size` must respect the volume constraints of the Instance's `commercial_type`: for each type of Instance, a minimum amount of storage is required, and there is also a maximum that cannot be exceeded. All Instance types support Block Storage (`sbs_volume`, `b_ssd`), some also support local storage (`l_ssd`). Read more about these constraints in the [List Instance types](#path-instance-types-list-instance-types) documentation, specifically the `volume_constraints` parameter for each type listed in the response You can use the `volumes` object in different ways. The table below shows which parameters are required for each of the following use cases: | Use case | Required params | Optional params | Notes | |-------------------------|-----------------------|---------------------|----------------------------------------| | Create a volume (`l_ssd`, `b_ssd`, `sbs_volume`) from a snapshot of an image | | `volume_type`, `size`, `boot` | If the `size` parameter is not set, the size of the volume will equal the size of the corresponding snapshot of the image. The image snapshot type should be compatible with the `volume_type`. | | Create a volume (`l_ssd`, `b_ssd`) from a snapshot | `base_snapshot`, `name`, `volume_type` | `boot` | | | Create a volume of type `sbs_volume` from a snapshot | `base_snapshot`, `name`, `volume_type` | `size`, `boot` | | | Create an empty volume | `name`, `volume_type`, `size` | `boot` | | | Attach an existing volume (`l_ssd`, `b_ssd`) | `id` | `boot` | | | Attach an existing volume of type `sbs_volume` | `id`, `volume_type` | `boot` | | (switchcolumn) <Message type=\"note\"> This information is designed to help you correctly configure the `volumes` object when using the [Create an Instance](#path-instances-create-an-instance) or [Update an Instance](#path-instances-update-an-instance) methods. </Message> (switchcolumn) ## Going further For more help using Scaleway Instances, check out the following resources: - Our [main documentation](https://www.scaleway.com/en/docs/compute/instances/) - The #instance channel on our [Slack Community](https://www.scaleway.com/en/docs/tutorials/scaleway-slack-community/) - Our [support ticketing system](https://www.scaleway.com/en/docs/console/my-account/how-to/open-a-support-ticket/).
|
|
*
|
|
* The version of the OpenAPI document: v1
|
|
* Generated by: https://openapi-generator.tech
|
|
* Generator version: 7.11.0
|
|
*/
|
|
|
|
/**
|
|
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
|
|
* https://openapi-generator.tech
|
|
* Do not edit the class manually.
|
|
*/
|
|
|
|
namespace OpenAPI\Client;
|
|
|
|
use GuzzleHttp\Psr7\Utils;
|
|
use OpenAPI\Client\Model\ModelInterface;
|
|
|
|
/**
|
|
* ObjectSerializer Class Doc Comment
|
|
*
|
|
* @category Class
|
|
* @package OpenAPI\Client
|
|
* @author OpenAPI Generator team
|
|
* @link https://openapi-generator.tech
|
|
*/
|
|
class ObjectSerializer
|
|
{
|
|
/** @var string */
|
|
private static $dateTimeFormat = \DateTime::ATOM;
|
|
|
|
/**
|
|
* Change the date format
|
|
*
|
|
* @param string $format the new date format to use
|
|
*/
|
|
public static function setDateTimeFormat($format)
|
|
{
|
|
self::$dateTimeFormat = $format;
|
|
}
|
|
|
|
/**
|
|
* Serialize data
|
|
*
|
|
* @param mixed $data the data to serialize
|
|
* @param string|null $type the OpenAPIToolsType of the data
|
|
* @param string|null $format the format of the OpenAPITools type of the data
|
|
*
|
|
* @return scalar|object|array|null serialized form of $data
|
|
*/
|
|
public static function sanitizeForSerialization($data, $type = null, $format = null)
|
|
{
|
|
if (is_scalar($data) || null === $data) {
|
|
return $data;
|
|
}
|
|
|
|
if ($data instanceof \DateTime) {
|
|
return ($format === 'date') ? $data->format('Y-m-d') : $data->format(self::$dateTimeFormat);
|
|
}
|
|
|
|
if (is_array($data)) {
|
|
foreach ($data as $property => $value) {
|
|
$data[$property] = self::sanitizeForSerialization($value);
|
|
}
|
|
return $data;
|
|
}
|
|
|
|
if (is_object($data)) {
|
|
$values = [];
|
|
if ($data instanceof ModelInterface) {
|
|
$formats = $data::openAPIFormats();
|
|
foreach ($data::openAPITypes() as $property => $openAPIType) {
|
|
$getter = $data::getters()[$property];
|
|
$value = $data->$getter();
|
|
if ($value !== null && !in_array($openAPIType, ['\DateTime', '\SplFileObject', 'array', 'bool', 'boolean', 'byte', 'float', 'int', 'integer', 'mixed', 'number', 'object', 'string', 'void'], true)) {
|
|
$callable = [$openAPIType, 'getAllowableEnumValues'];
|
|
if (is_callable($callable)) {
|
|
/** array $callable */
|
|
$allowedEnumTypes = $callable();
|
|
if (!in_array($value, $allowedEnumTypes, true)) {
|
|
$imploded = implode("', '", $allowedEnumTypes);
|
|
throw new \InvalidArgumentException("Invalid value for enum '$openAPIType', must be one of: '$imploded'");
|
|
}
|
|
}
|
|
}
|
|
if (($data::isNullable($property) && $data->isNullableSetToNull($property)) || $value !== null) {
|
|
$values[$data::attributeMap()[$property]] = self::sanitizeForSerialization($value, $openAPIType, $formats[$property]);
|
|
}
|
|
}
|
|
} else {
|
|
foreach($data as $property => $value) {
|
|
$values[$property] = self::sanitizeForSerialization($value);
|
|
}
|
|
}
|
|
return (object)$values;
|
|
} else {
|
|
return (string)$data;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Sanitize filename by removing path.
|
|
* e.g. ../../sun.gif becomes sun.gif
|
|
*
|
|
* @param string $filename filename to be sanitized
|
|
*
|
|
* @return string the sanitized filename
|
|
*/
|
|
public static function sanitizeFilename($filename)
|
|
{
|
|
if (preg_match("/.*[\/\\\\](.*)$/", $filename, $match)) {
|
|
return $match[1];
|
|
} else {
|
|
return $filename;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Shorter timestamp microseconds to 6 digits length.
|
|
*
|
|
* @param string $timestamp Original timestamp
|
|
*
|
|
* @return string the shorten timestamp
|
|
*/
|
|
public static function sanitizeTimestamp($timestamp)
|
|
{
|
|
if (!is_string($timestamp)) return $timestamp;
|
|
|
|
return preg_replace('/(:\d{2}.\d{6})\d*/', '$1', $timestamp);
|
|
}
|
|
|
|
/**
|
|
* Take value and turn it into a string suitable for inclusion in
|
|
* the path, by url-encoding.
|
|
*
|
|
* @param string $value a string which will be part of the path
|
|
*
|
|
* @return string the serialized object
|
|
*/
|
|
public static function toPathValue($value)
|
|
{
|
|
return rawurlencode(self::toString($value));
|
|
}
|
|
|
|
/**
|
|
* Checks if a value is empty, based on its OpenAPI type.
|
|
*
|
|
* @param mixed $value
|
|
* @param string $openApiType
|
|
*
|
|
* @return bool true if $value is empty
|
|
*/
|
|
private static function isEmptyValue($value, string $openApiType): bool
|
|
{
|
|
# If empty() returns false, it is not empty regardless of its type.
|
|
if (!empty($value)) {
|
|
return false;
|
|
}
|
|
|
|
# Null is always empty, as we cannot send a real "null" value in a query parameter.
|
|
if ($value === null) {
|
|
return true;
|
|
}
|
|
|
|
switch ($openApiType) {
|
|
# For numeric values, false and '' are considered empty.
|
|
# This comparison is safe for floating point values, since the previous call to empty() will
|
|
# filter out values that don't match 0.
|
|
case 'int':
|
|
case 'integer':
|
|
return $value !== 0;
|
|
|
|
case 'number':
|
|
case 'float':
|
|
return $value !== 0 && $value !== 0.0;
|
|
|
|
# For boolean values, '' is considered empty
|
|
case 'bool':
|
|
case 'boolean':
|
|
return !in_array($value, [false, 0], true);
|
|
|
|
# For string values, '' is considered empty.
|
|
case 'string':
|
|
return $value === '';
|
|
|
|
# For all the other types, any value at this point can be considered empty.
|
|
default:
|
|
return true;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Take query parameter properties and turn it into an array suitable for
|
|
* native http_build_query or GuzzleHttp\Psr7\Query::build.
|
|
*
|
|
* @param mixed $value Parameter value
|
|
* @param string $paramName Parameter name
|
|
* @param string $openApiType OpenAPIType eg. array or object
|
|
* @param string $style Parameter serialization style
|
|
* @param bool $explode Parameter explode option
|
|
* @param bool $required Whether query param is required or not
|
|
*
|
|
* @return array
|
|
*/
|
|
public static function toQueryValue(
|
|
$value,
|
|
string $paramName,
|
|
string $openApiType = 'string',
|
|
string $style = 'form',
|
|
bool $explode = true,
|
|
bool $required = true
|
|
): array {
|
|
|
|
# Check if we should omit this parameter from the query. This should only happen when:
|
|
# - Parameter is NOT required; AND
|
|
# - its value is set to a value that is equivalent to "empty", depending on its OpenAPI type. For
|
|
# example, 0 as "int" or "boolean" is NOT an empty value.
|
|
if (self::isEmptyValue($value, $openApiType)) {
|
|
if ($required) {
|
|
return ["{$paramName}" => ''];
|
|
} else {
|
|
return [];
|
|
}
|
|
}
|
|
|
|
# Handle DateTime objects in query
|
|
if($openApiType === "\\DateTime" && $value instanceof \DateTime) {
|
|
return ["{$paramName}" => $value->format(self::$dateTimeFormat)];
|
|
}
|
|
|
|
$query = [];
|
|
$value = (in_array($openApiType, ['object', 'array'], true)) ? (array)$value : $value;
|
|
|
|
// since \GuzzleHttp\Psr7\Query::build fails with nested arrays
|
|
// need to flatten array first
|
|
$flattenArray = function ($arr, $name, &$result = []) use (&$flattenArray, $style, $explode) {
|
|
if (!is_array($arr)) return $arr;
|
|
|
|
foreach ($arr as $k => $v) {
|
|
$prop = ($style === 'deepObject') ? $prop = "{$name}[{$k}]" : $k;
|
|
|
|
if (is_array($v)) {
|
|
$flattenArray($v, $prop, $result);
|
|
} else {
|
|
if ($style !== 'deepObject' && !$explode) {
|
|
// push key itself
|
|
$result[] = $prop;
|
|
}
|
|
$result[$prop] = $v;
|
|
}
|
|
}
|
|
return $result;
|
|
};
|
|
|
|
$value = $flattenArray($value, $paramName);
|
|
|
|
// https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#style-values
|
|
if ($openApiType === 'array' && $style === 'deepObject' && $explode) {
|
|
return $value;
|
|
}
|
|
|
|
if ($openApiType === 'object' && ($style === 'deepObject' || $explode)) {
|
|
return $value;
|
|
}
|
|
|
|
if ('boolean' === $openApiType && is_bool($value)) {
|
|
$value = self::convertBoolToQueryStringFormat($value);
|
|
}
|
|
|
|
// handle style in serializeCollection
|
|
$query[$paramName] = ($explode) ? $value : self::serializeCollection((array)$value, $style);
|
|
|
|
return $query;
|
|
}
|
|
|
|
/**
|
|
* Convert boolean value to format for query string.
|
|
*
|
|
* @param bool $value Boolean value
|
|
*
|
|
* @return int|string Boolean value in format
|
|
*/
|
|
public static function convertBoolToQueryStringFormat(bool $value)
|
|
{
|
|
if (Configuration::BOOLEAN_FORMAT_STRING == Configuration::getDefaultConfiguration()->getBooleanFormatForQueryString()) {
|
|
return $value ? 'true' : 'false';
|
|
}
|
|
|
|
return (int) $value;
|
|
}
|
|
|
|
/**
|
|
* Take value and turn it into a string suitable for inclusion in
|
|
* the header. If it's a string, pass through unchanged
|
|
* If it's a datetime object, format it in ISO8601
|
|
*
|
|
* @param string $value a string which will be part of the header
|
|
*
|
|
* @return string the header string
|
|
*/
|
|
public static function toHeaderValue($value)
|
|
{
|
|
$callable = [$value, 'toHeaderValue'];
|
|
if (is_callable($callable)) {
|
|
return $callable();
|
|
}
|
|
|
|
return self::toString($value);
|
|
}
|
|
|
|
/**
|
|
* Take value and turn it into a string suitable for inclusion in
|
|
* the http body (form parameter). If it's a string, pass through unchanged
|
|
* If it's a datetime object, format it in ISO8601
|
|
*
|
|
* @param string|\SplFileObject $value the value of the form parameter
|
|
*
|
|
* @return string the form string
|
|
*/
|
|
public static function toFormValue($value)
|
|
{
|
|
if ($value instanceof \SplFileObject) {
|
|
return $value->getRealPath();
|
|
} else {
|
|
return self::toString($value);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Take value and turn it into a string suitable for inclusion in
|
|
* the parameter. If it's a string, pass through unchanged
|
|
* If it's a datetime object, format it in ISO8601
|
|
* If it's a boolean, convert it to "true" or "false".
|
|
*
|
|
* @param float|int|bool|\DateTime $value the value of the parameter
|
|
*
|
|
* @return string the header string
|
|
*/
|
|
public static function toString($value)
|
|
{
|
|
if ($value instanceof \DateTime) { // datetime in ISO8601 format
|
|
return $value->format(self::$dateTimeFormat);
|
|
} elseif (is_bool($value)) {
|
|
return $value ? 'true' : 'false';
|
|
} else {
|
|
return (string) $value;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Serialize an array to a string.
|
|
*
|
|
* @param array $collection collection to serialize to a string
|
|
* @param string $style the format use for serialization (csv,
|
|
* ssv, tsv, pipes, multi)
|
|
* @param bool $allowCollectionFormatMulti allow collection format to be a multidimensional array
|
|
*
|
|
* @return string
|
|
*/
|
|
public static function serializeCollection(array $collection, $style, $allowCollectionFormatMulti = false)
|
|
{
|
|
if ($allowCollectionFormatMulti && ('multi' === $style)) {
|
|
// http_build_query() almost does the job for us. We just
|
|
// need to fix the result of multidimensional arrays.
|
|
return preg_replace('/%5B[0-9]+%5D=/', '=', http_build_query($collection, '', '&'));
|
|
}
|
|
switch ($style) {
|
|
case 'pipeDelimited':
|
|
case 'pipes':
|
|
return implode('|', $collection);
|
|
|
|
case 'tsv':
|
|
return implode("\t", $collection);
|
|
|
|
case 'spaceDelimited':
|
|
case 'ssv':
|
|
return implode(' ', $collection);
|
|
|
|
case 'simple':
|
|
case 'csv':
|
|
// Deliberate fall through. CSV is default format.
|
|
default:
|
|
return implode(',', $collection);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Deserialize a JSON string into an object
|
|
*
|
|
* @param mixed $data object or primitive to be deserialized
|
|
* @param string $class class name is passed as a string
|
|
* @param string[]|null $httpHeaders HTTP headers
|
|
*
|
|
* @return object|array|null a single or an array of $class instances
|
|
*/
|
|
public static function deserialize($data, $class, $httpHeaders = null)
|
|
{
|
|
if (null === $data) {
|
|
return null;
|
|
}
|
|
|
|
if (strcasecmp(substr($class, -2), '[]') === 0) {
|
|
$data = is_string($data) ? json_decode($data) : $data;
|
|
|
|
if (!is_array($data)) {
|
|
throw new \InvalidArgumentException("Invalid array '$class'");
|
|
}
|
|
|
|
$subClass = substr($class, 0, -2);
|
|
$values = [];
|
|
foreach ($data as $key => $value) {
|
|
$values[] = self::deserialize($value, $subClass, null);
|
|
}
|
|
return $values;
|
|
}
|
|
|
|
if (preg_match('/^(array<|map\[)/', $class)) { // for associative array e.g. array<string,int>
|
|
$data = is_string($data) ? json_decode($data) : $data;
|
|
settype($data, 'array');
|
|
$inner = substr($class, 4, -1);
|
|
$deserialized = [];
|
|
if (strrpos($inner, ",") !== false) {
|
|
$subClass_array = explode(',', $inner, 2);
|
|
$subClass = $subClass_array[1];
|
|
foreach ($data as $key => $value) {
|
|
$deserialized[$key] = self::deserialize($value, $subClass, null);
|
|
}
|
|
}
|
|
return $deserialized;
|
|
}
|
|
|
|
if ($class === 'object') {
|
|
settype($data, 'array');
|
|
return $data;
|
|
} elseif ($class === 'mixed') {
|
|
settype($data, gettype($data));
|
|
return $data;
|
|
}
|
|
|
|
if ($class === '\DateTime') {
|
|
// Some APIs return an invalid, empty string as a
|
|
// date-time property. DateTime::__construct() will return
|
|
// the current time for empty input which is probably not
|
|
// what is meant. The invalid empty string is probably to
|
|
// be interpreted as a missing field/value. Let's handle
|
|
// this graceful.
|
|
if (!empty($data)) {
|
|
try {
|
|
return new \DateTime($data);
|
|
} catch (\Exception $exception) {
|
|
// Some APIs return a date-time with too high nanosecond
|
|
// precision for php's DateTime to handle.
|
|
// With provided regexp 6 digits of microseconds saved
|
|
return new \DateTime(self::sanitizeTimestamp($data));
|
|
}
|
|
} else {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
if ($class === '\SplFileObject') {
|
|
$data = Utils::streamFor($data);
|
|
|
|
/** @var \Psr\Http\Message\StreamInterface $data */
|
|
|
|
// determine file name
|
|
if (
|
|
is_array($httpHeaders)
|
|
&& array_key_exists('Content-Disposition', $httpHeaders)
|
|
&& preg_match('/inline; filename=[\'"]?([^\'"\s]+)[\'"]?$/i', $httpHeaders['Content-Disposition'], $match)
|
|
) {
|
|
$filename = Configuration::getDefaultConfiguration()->getTempFolderPath() . DIRECTORY_SEPARATOR . self::sanitizeFilename($match[1]);
|
|
} else {
|
|
$filename = tempnam(Configuration::getDefaultConfiguration()->getTempFolderPath(), '');
|
|
}
|
|
|
|
$file = fopen($filename, 'w');
|
|
while ($chunk = $data->read(200)) {
|
|
fwrite($file, $chunk);
|
|
}
|
|
fclose($file);
|
|
|
|
return new \SplFileObject($filename, 'r');
|
|
}
|
|
|
|
/** @psalm-suppress ParadoxicalCondition */
|
|
if (in_array($class, ['\DateTime', '\SplFileObject', 'array', 'bool', 'boolean', 'byte', 'float', 'int', 'integer', 'mixed', 'number', 'object', 'string', 'void'], true)) {
|
|
settype($data, $class);
|
|
return $data;
|
|
}
|
|
|
|
|
|
if (method_exists($class, 'getAllowableEnumValues')) {
|
|
if (!in_array($data, $class::getAllowableEnumValues(), true)) {
|
|
$imploded = implode("', '", $class::getAllowableEnumValues());
|
|
throw new \InvalidArgumentException("Invalid value for enum '$class', must be one of: '$imploded'");
|
|
}
|
|
return $data;
|
|
} else {
|
|
$data = is_string($data) ? json_decode($data) : $data;
|
|
|
|
if (is_array($data)) {
|
|
$data = (object)$data;
|
|
}
|
|
|
|
// If a discriminator is defined and points to a valid subclass, use it.
|
|
$discriminator = $class::DISCRIMINATOR;
|
|
if (!empty($discriminator) && isset($data->{$discriminator}) && is_string($data->{$discriminator})) {
|
|
$subclass = '\OpenAPI\Client\Model\\' . $data->{$discriminator};
|
|
if (is_subclass_of($subclass, $class)) {
|
|
$class = $subclass;
|
|
}
|
|
}
|
|
|
|
/** @var ModelInterface $instance */
|
|
$instance = new $class();
|
|
foreach ($instance::openAPITypes() as $property => $type) {
|
|
$propertySetter = $instance::setters()[$property];
|
|
|
|
if (!isset($propertySetter)) {
|
|
continue;
|
|
}
|
|
|
|
if (!isset($data->{$instance::attributeMap()[$property]})) {
|
|
if ($instance::isNullable($property)) {
|
|
$instance->$propertySetter(null);
|
|
}
|
|
|
|
continue;
|
|
}
|
|
|
|
if (isset($data->{$instance::attributeMap()[$property]})) {
|
|
$propertyValue = $data->{$instance::attributeMap()[$property]};
|
|
$instance->$propertySetter(self::deserialize($propertyValue, $type, null));
|
|
}
|
|
}
|
|
return $instance;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Build a query string from an array of key value pairs.
|
|
*
|
|
* This function can use the return value of `parse()` to build a query
|
|
* string. This function does not modify the provided keys when an array is
|
|
* encountered (like `http_build_query()` would).
|
|
*
|
|
* The function is copied from https://github.com/guzzle/psr7/blob/a243f80a1ca7fe8ceed4deee17f12c1930efe662/src/Query.php#L59-L112
|
|
* with a modification which is described in https://github.com/guzzle/psr7/pull/603
|
|
*
|
|
* @param array $params Query string parameters.
|
|
* @param int|false $encoding Set to false to not encode, PHP_QUERY_RFC3986
|
|
* to encode using RFC3986, or PHP_QUERY_RFC1738
|
|
* to encode using RFC1738.
|
|
*/
|
|
public static function buildQuery(array $params, $encoding = PHP_QUERY_RFC3986): string
|
|
{
|
|
if (!$params) {
|
|
return '';
|
|
}
|
|
|
|
if ($encoding === false) {
|
|
$encoder = function (string $str): string {
|
|
return $str;
|
|
};
|
|
} elseif ($encoding === PHP_QUERY_RFC3986) {
|
|
$encoder = 'rawurlencode';
|
|
} elseif ($encoding === PHP_QUERY_RFC1738) {
|
|
$encoder = 'urlencode';
|
|
} else {
|
|
throw new \InvalidArgumentException('Invalid type');
|
|
}
|
|
|
|
$castBool = Configuration::BOOLEAN_FORMAT_INT == Configuration::getDefaultConfiguration()->getBooleanFormatForQueryString()
|
|
? function ($v) { return (int) $v; }
|
|
: function ($v) { return $v ? 'true' : 'false'; };
|
|
|
|
$qs = '';
|
|
foreach ($params as $k => $v) {
|
|
$k = $encoder((string) $k);
|
|
if (!is_array($v)) {
|
|
$qs .= $k;
|
|
$v = is_bool($v) ? $castBool($v) : $v;
|
|
if ($v !== null) {
|
|
$qs .= '='.$encoder((string) $v);
|
|
}
|
|
$qs .= '&';
|
|
} else {
|
|
foreach ($v as $vv) {
|
|
$qs .= $k;
|
|
$vv = is_bool($vv) ? $castBool($vv) : $vv;
|
|
if ($vv !== null) {
|
|
$qs .= '='.$encoder((string) $vv);
|
|
}
|
|
$qs .= '&';
|
|
}
|
|
}
|
|
}
|
|
|
|
return $qs ? (string) substr($qs, 0, -1) : '';
|
|
}
|
|
}
|