1212namespace Symfony \Component \JsonStreamer \DataModel \Write ;
1313
1414use Symfony \Component \JsonStreamer \DataModel \DataAccessorInterface ;
15+ use Symfony \Component \JsonStreamer \DataModel \FunctionDataAccessor ;
16+ use Symfony \Component \JsonStreamer \DataModel \PropertyDataAccessor ;
1517use 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}
0 commit comments