Compare commits
2 Commits
v1.0.0-bet
...
v1.0.0-bet
| Author | SHA1 | Date | |
|---|---|---|---|
| 1a37b009ea | |||
| 5201e564ca |
@@ -27,10 +27,25 @@ class Parameters
|
|||||||
*/
|
*/
|
||||||
public static function json(string $name, object|array $document): array
|
public static function json(string $name, object|array $document): array
|
||||||
{
|
{
|
||||||
return [$name => match (is_object($document) && method_exists($document, 'toJson')) {
|
$flags = JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES;
|
||||||
true => $document->toJson(),
|
|
||||||
false => json_encode($document, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)
|
if (is_object($document)) {
|
||||||
}];
|
return [
|
||||||
|
$name => method_exists($document, 'toJson') ? $document->toJson($flags) : json_encode($document, $flags)
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
$key = array_key_first($document);
|
||||||
|
if (is_array($document[$key])) {
|
||||||
|
if (empty($document[$key])) return [$name => json_encode($document, $flags)];
|
||||||
|
if (method_exists($document[$key][array_key_first($document[$key])], 'toJson')) {
|
||||||
|
return [
|
||||||
|
$name => sprintf('{%s:[%s]}', json_encode($key, $flags),
|
||||||
|
implode(',', array_map(fn($it) => $it->toJson($flags), $document[$key])))
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return [$name => json_encode($document, $flags)];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -33,6 +33,19 @@ class ParametersTest extends TestCase
|
|||||||
'JSON parameter not constructed correctly');
|
'JSON parameter not constructed correctly');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testJsonSucceedsForArrayWithEmptyArrayParameter(): void
|
||||||
|
{
|
||||||
|
$this->assertEquals([':it' => '{"id":18,"urls":[]}'], Parameters::json(':it', ['id' => 18, 'urls' => []]),
|
||||||
|
'JSON parameter not constructed correctly');
|
||||||
|
}
|
||||||
|
|
||||||
|
#[TestDox('json succeeds for 1D array with empty array parameter')]
|
||||||
|
public function testJsonSucceedsFor1DArrayWithEmptyArrayParameter(): void
|
||||||
|
{
|
||||||
|
$this->assertEquals([':it' => '{"urls":[]}'], Parameters::json(':it', ['urls' => []]),
|
||||||
|
'JSON parameter not constructed correctly');
|
||||||
|
}
|
||||||
|
|
||||||
#[TestDox('json succeeds for stdClass')]
|
#[TestDox('json succeeds for stdClass')]
|
||||||
public function testJsonSucceedsForStdClass(): void
|
public function testJsonSucceedsForStdClass(): void
|
||||||
{
|
{
|
||||||
@@ -50,6 +63,13 @@ class ParametersTest extends TestCase
|
|||||||
'JSON parameter not constructed correctly');
|
'JSON parameter not constructed correctly');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testJsonSucceedsForArrayOfPjsonClass(): void
|
||||||
|
{
|
||||||
|
$this->assertEquals([':it' => '{"pjson":[{"id":"997","name":"another test","num_value":94}]}'],
|
||||||
|
Parameters::json(':it',
|
||||||
|
['pjson' => [new PjsonDocument(new PjsonId('997'), 'another test', 94, 'nothing')]]),
|
||||||
|
'JSON parameter not constructed correctly');
|
||||||
|
}
|
||||||
|
|
||||||
public function testNameFieldsSucceeds(): void
|
public function testNameFieldsSucceeds(): void
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user