代码同步

This commit is contained in:
Ghost
2025-03-24 14:59:19 +08:00
committed by 柳清爽
parent 4e96b79acb
commit 725ea30d25
716 changed files with 90318 additions and 26155 deletions

View File

@@ -16,7 +16,7 @@
"lizhichao/one-sm": "^1.5"
},
"require-dev": {
"phpunit/phpunit": "^4.8.35|^5.4.3"
"phpunit/phpunit": "*"
},
"autoload": {
"psr-4": {

View File

@@ -8,7 +8,7 @@
<directory>tests</directory>
</testsuite>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>

View File

@@ -160,26 +160,26 @@ class OpenApiUtilClient
}
/**
* Parse array into a string with specified style.
* Parse object into a string with specified style.
*
* @style specified style e.g. repeatList
*
* @param mixed $array the array
* @param mixed $object the object
* @param string $prefix the prefix string
* @param string $style
*
* @return string the string
*/
public static function arrayToStringWithSpecifiedStyle($array, $prefix, $style)
public static function arrayToStringWithSpecifiedStyle($object, $prefix, $style)
{
if (null === $array) {
if (null === $object) {
return '';
}
if ('repeatList' === $style) {
return self::toForm([$prefix => $array]);
return self::toForm([$prefix => $object]);
}
if ('simple' == $style || 'spaceDelimited' == $style || 'pipeDelimited' == $style) {
$strs = self::flatten($array);
$strs = self::flatten($object);
switch ($style) {
case 'spaceDelimited':
@@ -192,7 +192,8 @@ class OpenApiUtilClient
return implode(',', $strs);
}
} elseif ('json' === $style) {
return json_encode($array);
self::parse($object, $parsed);
return json_encode($parsed);
}
return '';
@@ -423,7 +424,7 @@ class OpenApiUtilClient
foreach ($items as $key => $value) {
$pos = \is_int($key) ? $key + 1 : $key;
if ($value instanceof Model) {
$value = $value->toMap();
} elseif (\is_object($value)) {
@@ -436,6 +437,9 @@ class OpenApiUtilClient
self::flatten($value, $delimiter, $prepend . $pos . $delimiter)
);
} else {
if (\is_bool($value)) {
$value = true === $value ? 'true' : 'false';
}
$flatten[$prepend . $pos] = $value;
}
}

View File

@@ -63,12 +63,14 @@ class OpenApiUtilClientTest extends TestCase
public function testToForm()
{
$this->assertEquals('client=test&strs.1=str1&strs.2=str2&tag.key=value', OpenApiUtilClient::toForm([
$this->assertEquals('bool=true&client=test&strs.1=str1&strs.2=str2&strs.3=false&tag.key=value', OpenApiUtilClient::toForm([
'client' => 'test',
'tag' => [
'key' => 'value',
],
'strs' => ['str1', 'str2'],
'strs' => ['str1', 'str2', false],
'bool' => true,
'null' => null,
]));
}
@@ -83,6 +85,7 @@ class OpenApiUtilClientTest extends TestCase
$model = new MockModel();
$model->a = 'foo';
$model->c = 'boo';
$model->r = true;
$array = [
'a' => 'a',
@@ -95,7 +98,9 @@ class OpenApiUtilClientTest extends TestCase
'c' => ['x', 'y', 'z'],
'd' => [
$model
]
],
'e' => true,
'f' => null,
];
$this->assertEquals([
'a' => 'a',
@@ -107,6 +112,10 @@ class OpenApiUtilClientTest extends TestCase
'd.1.A' => 'foo',
'd.1.b' => '',
'd.1.c' => 'boo',
'd.1.c' => 'boo',
'd.1.r' => 'true',
'e' => 'true',
'f' => null
], OpenApiUtilClient::query($array));
}
@@ -142,6 +151,48 @@ class OpenApiUtilClientTest extends TestCase
)
);
$test = new ParseModel([
'str' => 'A',
'model' => new ParseModel(['str' => 'sub model']),
'array' => [1, 2, 3],
]);
$this->assertEquals(
'{"str":"A","model":{"str":"sub model","model":null,"array":null},"array":[1,2,3]}',
OpenApiUtilClient::arrayToStringWithSpecifiedStyle(
$test,
'instance',
'json'
)
);
// model item in array
$test = [
new ParseModel([
'str' => 'A',
]),
];
$this->assertEquals(
'[{"str":"A","model":null,"array":null}]',
OpenApiUtilClient::arrayToStringWithSpecifiedStyle(
$test,
'instance',
'json'
)
);
// model item in map
$test = [
'model' => new ParseModel([
'str' => 'A',
]),
];
$this->assertEquals(
'{"model":{"str":"A","model":null,"array":null}}',
OpenApiUtilClient::arrayToStringWithSpecifiedStyle(
$test,
'instance',
'json'
)
);
$this->assertEquals(
'ok,test,2,3',
OpenApiUtilClient::arrayToStringWithSpecifiedStyle(
@@ -295,7 +346,8 @@ class OpenApiUtilClientTest extends TestCase
'b9ff646822f41ef647c1416fa2b8408923828abc0464af6706e18db3e8553da8',
OpenApiUtilClient::hexEncode(OpenApiUtilClient::sign('secret', 'source', 'ACS3-HMAC-SM3'))
);
$this->assertEquals('1d93c62698a1c26427265668e79fac099aa26c1df873669599a2fb2f272e64c9',
$this->assertEquals(
'1d93c62698a1c26427265668e79fac099aa26c1df873669599a2fb2f272e64c9',
OpenApiUtilClient::hexEncode(OpenApiUtilClient::sign('secret', 'source', 'ACS3-HMAC-SHA256'))
);
}
@@ -311,14 +363,14 @@ class OpenApiUtilClientTest extends TestCase
'array' => [1, 2, 3],
]),
[ // model item in array
new ParseModel([
'str' => 'A',
]),
new ParseModel([
'str' => 'A',
]),
],
[ // model item in map
'model' => new ParseModel([
'str' => 'A',
]),
'model' => new ParseModel([
'str' => 'A',
]),
],
],
'expected' => [
@@ -347,6 +399,12 @@ class OpenApiUtilClientTest extends TestCase
],
],
],
'expectedJsonStr' => [
'["NotArray"]',
'NotArray',
'NotArray',
'NotArray',
],
];
}
}