Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@ public function __construct(
private DataAccessorInterface $accessor,
private CollectionType $type,
private DataModelNodeInterface $item,
private DataModelNodeInterface $key,
) {
}

public function withAccessor(DataAccessorInterface $accessor): self
{
return new self($accessor, $this->type, $this->item);
return new self($accessor, $this->type, $this->item, $this->key);
}

public function getIdentifier(): string
Expand All @@ -54,4 +55,9 @@ public function getItemNode(): DataModelNodeInterface
{
return $this->item;
}

public function getKeyNode(): DataModelNodeInterface
{
return $this->key;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function testSortNodesOnCreation()
$composite = new CompositeNode(new VariableDataAccessor('data'), [
$scalar = new ScalarNode(new VariableDataAccessor('data'), Type::int()),
$object = new ObjectNode(new VariableDataAccessor('data'), Type::object(self::class), []),
$collection = new CollectionNode(new VariableDataAccessor('data'), Type::list(), new ScalarNode(new VariableDataAccessor('data'), Type::int())),
$collection = new CollectionNode(new VariableDataAccessor('data'), Type::list(), new ScalarNode(new VariableDataAccessor('data'), Type::int()), new ScalarNode(new VariableDataAccessor('key'), Type::string())),
]);

$this->assertSame([$collection, $object, $scalar], $composite->getNodes());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Symfony\Component\JsonStreamer\Tests\Fixtures\Model;

class DummyWithArray
{
/** @var ClassicDummy[] */
public array $dummies;

public string $customProperty;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Symfony\Component\JsonStreamer\Tests\Fixtures\Model;

class DummyWithNestedArray
{
/** @var DummyWithArray[] */
public array $dummies;

public string $stringProperty;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

return static function (mixed $data, \Psr\Container\ContainerInterface $valueTransformers, array $options): \Traversable {
try {
yield '[';
$prefix = '';
foreach ($data as $value1) {
yield $prefix;
yield '{"dummies":[';
$prefix = '';
foreach ($value1->dummies as $value2) {
yield $prefix;
yield '{"dummies":[';
$prefix = '';
foreach ($value2->dummies as $value3) {
yield $prefix;
yield '{"id":';
yield \json_encode($value3->id, \JSON_THROW_ON_ERROR, 506);
yield ',"name":';
yield \json_encode($value3->name, \JSON_THROW_ON_ERROR, 506);
yield '}';
$prefix = ',';
}
yield '],"customProperty":';
yield \json_encode($value2->customProperty, \JSON_THROW_ON_ERROR, 508);
yield '}';
$prefix = ',';
}
yield '],"stringProperty":';
yield \json_encode($value1->stringProperty, \JSON_THROW_ON_ERROR, 510);
yield '}';
$prefix = ',';
}
yield ']';
} catch (\JsonException $e) {
throw new \Symfony\Component\JsonStreamer\Exception\NotEncodableValueException($e->getMessage(), 0, $e);
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

return static function (mixed $data, \Psr\Container\ContainerInterface $valueTransformers, array $options): \Traversable {
try {
yield '[';
$prefix = '';
foreach ($data as $value1) {
yield $prefix;
yield '{"dummies":[';
$prefix = '';
foreach ($value1->dummies as $value2) {
yield $prefix;
yield '{"id":';
yield \json_encode($value2->id, \JSON_THROW_ON_ERROR, 508);
yield ',"name":';
yield \json_encode($value2->name, \JSON_THROW_ON_ERROR, 508);
yield '}';
$prefix = ',';
}
yield '],"customProperty":';
yield \json_encode($value1->customProperty, \JSON_THROW_ON_ERROR, 510);
yield '}';
$prefix = ',';
}
yield ']';
} catch (\JsonException $e) {
throw new \Symfony\Component\JsonStreamer\Exception\NotEncodableValueException($e->getMessage(), 0, $e);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
if (\is_array($data)) {
yield '{';
$prefix = '';
foreach ($data as $key => $value) {
$key = \substr(\json_encode($key), 1, -1);
yield "{$prefix}\"{$key}\":";
foreach ($data as $key1 => $value1) {
$key1 = \substr(\json_encode($key1), 1, -1);
yield "{$prefix}\"{$key1}\":";
yield '{"@id":';
yield \json_encode($value->id, \JSON_THROW_ON_ERROR, 510);
yield \json_encode($value1->id, \JSON_THROW_ON_ERROR, 510);
yield ',"name":';
yield \json_encode($value->name, \JSON_THROW_ON_ERROR, 510);
yield \json_encode($value1->name, \JSON_THROW_ON_ERROR, 510);
yield '}';
$prefix = ',';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
if (\is_array($data)) {
yield '[';
$prefix = '';
foreach ($data as $value) {
foreach ($data as $value1) {
yield $prefix;
yield '{"@id":';
yield \json_encode($value->id, \JSON_THROW_ON_ERROR, 510);
yield \json_encode($value1->id, \JSON_THROW_ON_ERROR, 510);
yield ',"name":';
yield \json_encode($value->name, \JSON_THROW_ON_ERROR, 510);
yield \json_encode($value1->name, \JSON_THROW_ON_ERROR, 510);
yield '}';
$prefix = ',';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
try {
yield '{';
$prefix = '';
foreach ($data as $key => $value) {
$key = \substr(\json_encode($key), 1, -1);
yield "{$prefix}\"{$key}\":";
foreach ($data as $key1 => $value1) {
$key1 = \substr(\json_encode($key1), 1, -1);
yield "{$prefix}\"{$key1}\":";
yield '{"@id":';
yield \json_encode($value->id, \JSON_THROW_ON_ERROR, 510);
yield \json_encode($value1->id, \JSON_THROW_ON_ERROR, 510);
yield ',"name":';
yield \json_encode($value->name, \JSON_THROW_ON_ERROR, 510);
yield \json_encode($value1->name, \JSON_THROW_ON_ERROR, 510);
yield '}';
$prefix = ',';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
try {
yield '{';
$prefix = '';
foreach ($data as $key => $value) {
$key = is_int($key) ? $key : \substr(\json_encode($key), 1, -1);
yield "{$prefix}\"{$key}\":";
foreach ($data as $key1 => $value1) {
$key1 = is_int($key1) ? $key1 : \substr(\json_encode($key1), 1, -1);
yield "{$prefix}\"{$key1}\":";
yield '{"id":';
yield \json_encode($value->id, \JSON_THROW_ON_ERROR, 510);
yield \json_encode($value1->id, \JSON_THROW_ON_ERROR, 510);
yield ',"name":';
yield \json_encode($value->name, \JSON_THROW_ON_ERROR, 510);
yield \json_encode($value1->name, \JSON_THROW_ON_ERROR, 510);
yield '}';
$prefix = ',';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
try {
yield '[';
$prefix = '';
foreach ($data as $value) {
foreach ($data as $value1) {
yield $prefix;
yield '{"@id":';
yield \json_encode($value->id, \JSON_THROW_ON_ERROR, 510);
yield \json_encode($value1->id, \JSON_THROW_ON_ERROR, 510);
yield ',"name":';
yield \json_encode($value->name, \JSON_THROW_ON_ERROR, 510);
yield \json_encode($value1->name, \JSON_THROW_ON_ERROR, 510);
yield '}';
$prefix = ',';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
if (\is_array($data)) {
yield '[';
$prefix = '';
foreach ($data as $value) {
foreach ($data as $value1) {
yield $prefix;
yield \json_encode($value->value, \JSON_THROW_ON_ERROR, 511);
yield \json_encode($value1->value, \JSON_THROW_ON_ERROR, 511);
$prefix = ',';
}
yield ']';
Expand Down
31 changes: 31 additions & 0 deletions src/Symfony/Component/JsonStreamer/Tests/JsonStreamWriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@
use Symfony\Component\JsonStreamer\JsonStreamWriter;
use Symfony\Component\JsonStreamer\Tests\Fixtures\Enum\DummyBackedEnum;
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\ClassicDummy;
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithArray;
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithDateTimes;
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithGenerics;
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithNameAttributes;
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithNestedArray;
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithNullableProperties;
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithPhpDoc;
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithUnionProperties;
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithValueTransformerAttributes;
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\JsonStreamableDummy;
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\SelfReferencingDummy;
use Symfony\Component\JsonStreamer\Tests\Fixtures\ValueTransformer\BooleanToStringValueTransformer;
use Symfony\Component\JsonStreamer\Tests\Fixtures\ValueTransformer\DoubleIntAndCastToStringValueTransformer;
Expand Down Expand Up @@ -107,6 +110,34 @@ public function testWriteCollection()
new \ArrayObject([new ClassicDummy(), new ClassicDummy()]),
Type::iterable(Type::object(ClassicDummy::class), Type::int()),
);

$dummyWithArray1 = new DummyWithArray();
$dummyWithArray1->dummies = [new ClassicDummy()];
$dummyWithArray1->customProperty = 'customProperty1';

$dummyWithArray2 = new DummyWithArray();
$dummyWithArray2->dummies = [new ClassicDummy()];
$dummyWithArray2->customProperty = 'customProperty2';

$this->assertWritten(
'[{"dummies":[{"id":1,"name":"dummy"}],"customProperty":"customProperty1"},{"dummies":[{"id":1,"name":"dummy"}],"customProperty":"customProperty2"}]',
[$dummyWithArray1, $dummyWithArray2],
Type::list(Type::object(DummyWithArray::class)),
);

$dummyWithNestedArray1 = new DummyWithNestedArray();
$dummyWithNestedArray1->dummies = [$dummyWithArray1];
$dummyWithNestedArray1->stringProperty = 'stringProperty1';

$dummyWithNestedArray2 = new DummyWithNestedArray();
$dummyWithNestedArray2->dummies = [$dummyWithArray2];
$dummyWithNestedArray2->stringProperty = 'stringProperty2';

$this->assertWritten(
'[{"dummies":[{"dummies":[{"id":1,"name":"dummy"}],"customProperty":"customProperty1"}],"stringProperty":"stringProperty1"},{"dummies":[{"dummies":[{"id":1,"name":"dummy"}],"customProperty":"customProperty2"}],"stringProperty":"stringProperty2"}]',
[$dummyWithNestedArray1, $dummyWithNestedArray2],
Type::list(Type::object(DummyWithNestedArray::class)),
);
}

public function testWriteObject()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
use Symfony\Component\JsonStreamer\Tests\Fixtures\Enum\DummyBackedEnum;
use Symfony\Component\JsonStreamer\Tests\Fixtures\Enum\DummyEnum;
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\ClassicDummy;
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithArray;
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithNameAttributes;
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithNestedArray;
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithOtherDummies;
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithUnionProperties;
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithValueTransformerAttributes;
Expand Down Expand Up @@ -93,6 +95,8 @@ public static function generatedStreamWriterDataProvider(): iterable
yield ['null_list', Type::list(Type::null())];
yield ['object_list', Type::list(Type::object(DummyWithNameAttributes::class))];
yield ['nullable_object_list', Type::nullable(Type::list(Type::object(DummyWithNameAttributes::class)))];
yield ['nested_list', Type::list(Type::object(DummyWithArray::class))];
yield ['double_nested_list', Type::list(Type::object(DummyWithNestedArray::class))];

yield ['dict', Type::dict()];
yield ['object_dict', Type::dict(Type::object(DummyWithNameAttributes::class))];
Expand Down Expand Up @@ -141,6 +145,7 @@ public function testCallPropertyMetadataLoaderWithProperContext()
->with(self::class, [], [
'original_type' => $type,
'generated_classes' => [self::class => true],
'depth' => 0,
])
->willReturn([]);

Expand Down
12 changes: 7 additions & 5 deletions src/Symfony/Component/JsonStreamer/Write/PhpAstBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -309,21 +309,23 @@ private function buildYieldStatements(DataModelNodeInterface $dataModelNode, arr
];
}

$keyVar = $dataModelNode->getKeyNode()->getAccessor()->toPhpExpr();

$escapedKey = $dataModelNode->getType()->getCollectionKeyType()->isIdentifiedBy(TypeIdentifier::INT)
? new Ternary($this->builder->funcCall('is_int', [$this->builder->var('key')]), $this->builder->var('key'), $this->escapeString($this->builder->var('key')))
: $this->escapeString($this->builder->var('key'));
? new Ternary($this->builder->funcCall('is_int', [$keyVar]), $keyVar, $this->escapeString($keyVar))
: $this->escapeString($keyVar);

return [
new Expression(new Yield_($this->builder->val('{'))),
new Expression(new Assign($this->builder->var('prefix'), $this->builder->val(''))),
new Foreach_($accessor, $dataModelNode->getItemNode()->getAccessor()->toPhpExpr(), [
'keyVar' => $this->builder->var('key'),
'keyVar' => $keyVar,
'stmts' => [
new Expression(new Assign($this->builder->var('key'), $escapedKey)),
new Expression(new Assign($keyVar, $escapedKey)),
new Expression(new Yield_(new Encapsed([
$this->builder->var('prefix'),
new EncapsedStringPart('"'),
$this->builder->var('key'),
$keyVar,
new EncapsedStringPart('":'),
]))),
...$this->buildYieldStatements($dataModelNode->getItemNode(), $options, $context),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function generate(Type $type, array $options = []): string
$this->phpPrinter ??= new Standard(['phpVersion' => PhpVersion::fromComponents(8, 2)]);
$this->fs ??= new Filesystem();

$dataModel = $this->createDataModel($type, new VariableDataAccessor('data'), $options);
$dataModel = $this->createDataModel($type, new VariableDataAccessor('data'), $options, ['depth' => 0]);

$nodes = $this->phpAstBuilder->build($dataModel, $options);
$nodes = $this->phpOptimizer->optimize($nodes);
Expand Down Expand Up @@ -180,10 +180,13 @@ private function createDataModel(Type $type, DataAccessorInterface $accessor, ar
}

if ($type instanceof CollectionType) {
++$context['depth'];

return new CollectionNode(
$accessor,
$type,
$this->createDataModel($type->getCollectionValueType(), new VariableDataAccessor('value'), $options, $context),
$this->createDataModel($type->getCollectionValueType(), new VariableDataAccessor('value' . $context['depth']), $options, $context),
$this->createDataModel($type->getCollectionKeyType(), new VariableDataAccessor('key' . $context['depth']), $options, $context),
);
}

Expand Down