Skip to content

Commit 38563aa

Browse files
committed
[JsonEncoder] Fix max depth handling and json errors
1 parent df2996e commit 38563aa

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+625
-261
lines changed

DataModel/FunctionDataAccessor.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ public function __construct(
3333
) {
3434
}
3535

36+
public function getObjectAccessor(): ?DataAccessorInterface
37+
{
38+
return $this->objectAccessor;
39+
}
40+
41+
public function withObjectAccessor(?DataAccessorInterface $accessor): self
42+
{
43+
return new self($this->functionName, $this->arguments, $accessor);
44+
}
45+
3646
public function toPhpExpr(): Expr
3747
{
3848
$builder = new BuilderFactory();

DataModel/PropertyDataAccessor.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ public function __construct(
2929
) {
3030
}
3131

32+
public function getObjectAccessor(): DataAccessorInterface
33+
{
34+
return $this->objectAccessor;
35+
}
36+
37+
public function withObjectAccessor(DataAccessorInterface $accessor): self
38+
{
39+
return new self($accessor, $this->propertyName);
40+
}
41+
3242
public function toPhpExpr(): Expr
3343
{
3444
return (new BuilderFactory())->propertyFetch($this->objectAccessor->toPhpExpr(), $this->propertyName);

DataModel/Read/ObjectNode.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ final class ObjectNode implements DataModelNodeInterface
3030
public function __construct(
3131
private ObjectType $type,
3232
private array $properties,
33-
private bool $ghost = false,
33+
private bool $mock = false,
3434
) {
3535
}
3636

37-
public static function createGhost(ObjectType|UnionType $type): self
37+
public static function createMock(ObjectType|UnionType $type): self
3838
{
3939
return new self($type, [], true);
4040
}
@@ -57,8 +57,8 @@ public function getProperties(): array
5757
return $this->properties;
5858
}
5959

60-
public function isGhost(): bool
60+
public function isMock(): bool
6161
{
62-
return $this->ghost;
62+
return $this->mock;
6363
}
6464
}

DataModel/Write/BackedEnumNode.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ public function __construct(
3131
) {
3232
}
3333

34+
public function withAccessor(DataAccessorInterface $accessor): self
35+
{
36+
return new self($accessor, $this->type);
37+
}
38+
39+
public function getIdentifier(): string
40+
{
41+
return (string) $this->getType();
42+
}
43+
3444
public function getAccessor(): DataAccessorInterface
3545
{
3646
return $this->accessor;

DataModel/Write/CollectionNode.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ public function __construct(
3030
) {
3131
}
3232

33+
public function withAccessor(DataAccessorInterface $accessor): self
34+
{
35+
return new self($accessor, $this->type, $this->item);
36+
}
37+
38+
public function getIdentifier(): string
39+
{
40+
return (string) $this->getType();
41+
}
42+
3343
public function getAccessor(): DataAccessorInterface
3444
{
3545
return $this->accessor;

DataModel/Write/CompositeNode.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,16 @@ public function __construct(
6060
$this->nodes = $nodes;
6161
}
6262

63+
public function withAccessor(DataAccessorInterface $accessor): self
64+
{
65+
return new self($accessor, array_map(static fn (DataModelNodeInterface $n): DataModelNodeInterface => $n->withAccessor($accessor), $this->nodes));
66+
}
67+
68+
public function getIdentifier(): string
69+
{
70+
return (string) $this->getType();
71+
}
72+
6373
public function getAccessor(): DataAccessorInterface
6474
{
6575
return $this->accessor;

DataModel/Write/DataModelNodeInterface.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@
2323
*/
2424
interface DataModelNodeInterface
2525
{
26+
public function getIdentifier(): string;
27+
2628
public function getType(): Type;
2729

2830
public function getAccessor(): DataAccessorInterface;
31+
32+
public function withAccessor(DataAccessorInterface $accessor): self;
2933
}

DataModel/Write/ExceptionNode.php

Lines changed: 0 additions & 49 deletions
This file was deleted.

DataModel/Write/ObjectNode.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
namespace Symfony\Component\JsonStreamer\DataModel\Write;
1313

1414
use Symfony\Component\JsonStreamer\DataModel\DataAccessorInterface;
15+
use Symfony\Component\JsonStreamer\DataModel\FunctionDataAccessor;
16+
use Symfony\Component\JsonStreamer\DataModel\PropertyDataAccessor;
1517
use Symfony\Component\TypeInfo\Type\ObjectType;
1618

1719
/**
@@ -30,9 +32,36 @@ public function __construct(
3032
private DataAccessorInterface $accessor,
3133
private ObjectType $type,
3234
private array $properties,
35+
private bool $mock = false,
3336
) {
3437
}
3538

39+
public static function createMock(DataAccessorInterface $accessor, ObjectType $type): self
40+
{
41+
return new self($accessor, $type, [], true);
42+
}
43+
44+
public function withAccessor(DataAccessorInterface $accessor): self
45+
{
46+
$properties = [];
47+
foreach ($this->properties as $key => $property) {
48+
$propertyAccessor = $property->getAccessor();
49+
50+
if ($propertyAccessor instanceof PropertyDataAccessor || $propertyAccessor instanceof FunctionDataAccessor && $propertyAccessor->getObjectAccessor()) {
51+
$propertyAccessor = $propertyAccessor->withObjectAccessor($accessor);
52+
}
53+
54+
$properties[$key] = $property->withAccessor($propertyAccessor);
55+
}
56+
57+
return new self($accessor, $this->type, $properties, $this->mock);
58+
}
59+
60+
public function getIdentifier(): string
61+
{
62+
return (string) $this->getType();
63+
}
64+
3665
public function getAccessor(): DataAccessorInterface
3766
{
3867
return $this->accessor;
@@ -50,4 +79,9 @@ public function getProperties(): array
5079
{
5180
return $this->properties;
5281
}
82+
83+
public function isMock(): bool
84+
{
85+
return $this->mock;
86+
}
5387
}

DataModel/Write/ScalarNode.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ public function __construct(
3131
) {
3232
}
3333

34+
public function withAccessor(DataAccessorInterface $accessor): self
35+
{
36+
return new self($accessor, $this->type);
37+
}
38+
39+
public function getIdentifier(): string
40+
{
41+
return (string) $this->getType();
42+
}
43+
3444
public function getAccessor(): DataAccessorInterface
3545
{
3646
return $this->accessor;

0 commit comments

Comments
 (0)