Skip to content

Commit

Permalink
Fix everything.
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianStehle committed Aug 20, 2024
1 parent 217a1ad commit e7f84d7
Show file tree
Hide file tree
Showing 10 changed files with 785 additions and 77 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@
"autoload-dev": {
"psr-4": { "Squidex\\Client\\Test\\" : "tests/" }
}
}
}
18 changes: 17 additions & 1 deletion lib/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,82 +42,96 @@ class Configuration
{
public const BOOLEAN_FORMAT_INT = 'int';
public const BOOLEAN_FORMAT_STRING = 'string';

/**
* @var Configuration
*/
private static $defaultConfiguration;

/**
* Client ID for OAuth/Bearer authentication
*
* @var string
*/
protected $clientId = '';

/**
* Client Secret for OAuth/Bearer authentication
*
* @var string
*/
protected $clientSecret = '';

/**
* App Name
*
* @var string
*/
protected $appName = '';

/**
* Boolean format for query string
*
* @var string
*/
protected $booleanFormatForQueryString = self::BOOLEAN_FORMAT_STRING;

/**
* The host
*
* @var string
*/
protected $host = 'https://cloud.squidex.io';

/**
* The timeout in seconds
*
* @var float
*/
protected $timeout = 30.0;

/**
* The token store.
*
* @var SquidexTokenStore
*/
protected $tokenStore = null;

/**
* User agent of the HTTP request, set to "OpenAPI-Generator/{version}/PHP" by default
*
* @var string
*/
protected $userAgent = 'OpenAPI-Generator/1.0.0/PHP';

/**
* Ignore the validation of certificates.
*
* @var boolean
*/
protected $ignoreCertificates = false;

/**
* Debug switch (default set to false)
*
* @var bool
*/
protected $debug = false;

/**
* Debug file location (log to STDOUT by default)
*
* @var string
*/
protected $debugFile = 'php://output';

/**
* Debug file location (log to STDOUT by default)
*
* @var string
*/
protected $tempFolderPath;

/**
* Constructor
*/
Expand Down Expand Up @@ -545,8 +559,10 @@ public static function getHostString(array $hostsSettings, $hostIndex, array $va
$url = str_replace("{".$name."}", $variable["default_value"], $url);
}
}

return $url;
}

/**
* Returns URL based on the index and variables
*
Expand All @@ -558,4 +574,4 @@ public function getHostFromSettings($index, $variables = null)
{
return self::getHostString($this->getHostSettings(), $index, $variables);
}
}
}
101 changes: 31 additions & 70 deletions lib/ObjectSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,11 +265,6 @@ public static function toQueryValue(

$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;
}
Expand Down Expand Up @@ -343,7 +338,7 @@ public static function toFormValue($value)
* 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
* @param string|bool|\DateTime $value the value of the parameter
*
* @return string the header string
*/
Expand Down Expand Up @@ -401,6 +396,7 @@ public static function serializeCollection(array $collection, $style, $allowColl
* @param mixed $data object or primitive to be deserialized
* @param string $class class name is passed as a string
* @param string[] $httpHeaders HTTP headers
* @param string $discriminator discriminator if polymorphism is used
*
* @return object|array|null a single or an array of $class instances
*/
Expand Down Expand Up @@ -477,7 +473,7 @@ public static function deserialize($data, $class, $httpHeaders = null)
// determine file name
if (
is_array($httpHeaders)
&& array_key_exists('Content-Disposition', $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]);
Expand Down Expand Up @@ -516,10 +512,17 @@ public static function deserialize($data, $class, $httpHeaders = null)

// 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 = '\Squidex\Client\Model\\' . $data->{$discriminator};
if (is_subclass_of($subclass, $class)) {
$class = $subclass;
if (!empty($discriminator)) {
$discriminatorProperty = $class::attributeMap()[$discriminator];

if (isset($data->{$discriminatorProperty}) && is_string($data->{$discriminatorProperty})) {
$discriminatorValue = $data->{$discriminatorProperty};
$discriminatorType = $class::openAPIMAppings()[$discriminatorValue];

$subclass = '\Squidex\Client\Model\\' . $discriminatorType;
if (is_subclass_of($subclass, $class)) {
$class = $subclass;
}
}
}

Expand Down Expand Up @@ -550,64 +553,22 @@ public static function deserialize($data, $class, $httpHeaders = null)
}

/**
* 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) : '';
* Native `http_build_query` wrapper.
* @see https://www.php.net/manual/en/function.http-build-query
*
* @param array|object $data May be an array or object containing properties.
* @param string $numeric_prefix If numeric indices are used in the base array and this parameter is provided, it will be prepended to the numeric index for elements in the base array only.
* @param string|null $arg_separator arg_separator.output is used to separate arguments but may be overridden by specifying this parameter.
* @param int $encoding_type Encoding type. By default, PHP_QUERY_RFC1738.
*
* @return string
*/
public static function buildQuery(
$data,
string $numeric_prefix = '',
?string $arg_separator = null,
int $encoding_type = \PHP_QUERY_RFC3986
): string {
return \GuzzleHttp\Psr7\Query::build($data, $encoding_type);
}
}
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
<php>
<ini name="error_reporting" value="E_ALL"/>
</php>
</phpunit>
</phpunit>
18 changes: 17 additions & 1 deletion templates/Configuration.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -33,82 +33,96 @@ class Configuration
{
public const BOOLEAN_FORMAT_INT = 'int';
public const BOOLEAN_FORMAT_STRING = 'string';
/**
* @var Configuration
*/
private static $defaultConfiguration;
/**
* Client ID for OAuth/Bearer authentication
*
* @var string
*/
protected $clientId = '';
/**
* Client Secret for OAuth/Bearer authentication
*
* @var string
*/
protected $clientSecret = '';
/**
* App Name
*
* @var string
*/
protected $appName = '';
/**
* Boolean format for query string
*
* @var string
*/
protected $booleanFormatForQueryString = self::BOOLEAN_FORMAT_STRING;
/**
* The host
*
* @var string
*/
protected $host = '{{basePath}}';
/**
* The timeout in seconds
*
* @var float
*/
protected $timeout = 30.0;
/**
* The token store.
*
* @var SquidexTokenStore
*/
protected $tokenStore = null;
/**
* User agent of the HTTP request, set to "OpenAPI-Generator/{version}/PHP" by default
*
* @var string
*/
protected $userAgent = '{{{httpUserAgent}}}{{^httpUserAgent}}OpenAPI-Generator/{{{artifactVersion}}}{{^artifactVersion}}1.0.0{{/artifactVersion}}/PHP{{/httpUserAgent}}';
/**
* Ignore the validation of certificates.
*
* @var boolean
*/
protected $ignoreCertificates = false;
/**
* Debug switch (default set to false)
*
* @var bool
*/
protected $debug = false;
/**
* Debug file location (log to STDOUT by default)
*
* @var string
*/
protected $debugFile = 'php://output';
/**
* Debug file location (log to STDOUT by default)
*
* @var string
*/
protected $tempFolderPath;
/**
* Constructor
*/
Expand Down Expand Up @@ -562,8 +576,10 @@ class Configuration
$url = str_replace("{".$name."}", $variable["default_value"], $url);
}
}
return $url;
}
/**
* Returns URL based on the index and variables
*
Expand All @@ -575,4 +591,4 @@ class Configuration
{
return self::getHostString($this->getHostSettings(), $index, $variables);
}
}
}
Loading

0 comments on commit e7f84d7

Please sign in to comment.