Skip to content

Commit 3cd9483

Browse files
mtarldnicolas-grekas
authored andcommitted
[JsonStreamer] Merge PropertyMetadata value transformers
1 parent 70201ca commit 3cd9483

11 files changed

+89
-25
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ CHANGELOG
99
* Add `_current_object` to the context passed to value transformers during write operations
1010
* Add `include_null_properties` option to encode the properties with `null` value
1111
* Add synthetic properties support
12+
* Deprecate `PropertyMetadata::$streamToNativeValueTransformers`, use `PropertyMetadata::$valueTransformers` instead
13+
* Deprecate `PropertyMetadata::getNativeToStreamValueTransformer()` and `PropertyMetadata::getStreamToNativeValueTransformers()`, use `PropertyMetadata::getValueTransformers()` instead
14+
* Deprecate `PropertyMetadata::withNativeToStreamValueTransformers()` and `PropertyMetadata::withStreamToNativeValueTransformers()`, use `PropertyMetadata::withValueTransformers()` instead
15+
* Deprecate `PropertyMetadata::withAdditionalNativeToStreamValueTransformer()` and `PropertyMetadata::withAdditionalStreamToNativeValueTransformer`, use `PropertyMetadata::withAdditionalValueTransformer()` instead
1216

1317
7.3
1418
---

Mapping/PropertyMetadata.php

Lines changed: 70 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,23 @@
2020
*/
2121
final class PropertyMetadata
2222
{
23+
private ?array $streamToNativeValueTransformers;
24+
2325
/**
24-
* @param list<string|\Closure> $nativeToStreamValueTransformers
25-
* @param list<string|\Closure> $streamToNativeValueTransformers
26+
* @param list<string|\Closure> $valueTransformers
27+
* @param list<string|\Closure>|null $streamToNativeValueTransformers
2628
*/
2729
public function __construct(
2830
private ?string $name,
2931
private Type $type,
30-
private array $nativeToStreamValueTransformers = [],
31-
private array $streamToNativeValueTransformers = [],
32+
private array $valueTransformers = [],
33+
?array $streamToNativeValueTransformers = null,
3234
) {
35+
if (null !== $streamToNativeValueTransformers) {
36+
trigger_deprecation('symfony/json-streamer', '7.4', 'The "streamToNativeValueTransformers" parameter of the "%s()" method is deprecated. Use "valueTransformers" instead.', __METHOD__);
37+
}
38+
39+
$this->streamToNativeValueTransformers = $streamToNativeValueTransformers;
3340
}
3441

3542
/**
@@ -51,7 +58,7 @@ public function getName(): ?string
5158

5259
public function withName(?string $name): self
5360
{
54-
return new self($name, $this->type, $this->nativeToStreamValueTransformers, $this->streamToNativeValueTransformers);
61+
return new self($name, $this->type, $this->valueTransformers, $this->streamToNativeValueTransformers);
5562
}
5663

5764
public function getType(): Type
@@ -61,28 +68,67 @@ public function getType(): Type
6168

6269
public function withType(Type $type): self
6370
{
64-
return new self($this->name, $type, $this->nativeToStreamValueTransformers, $this->streamToNativeValueTransformers);
71+
return new self($this->name, $type, $this->valueTransformers, $this->streamToNativeValueTransformers);
72+
}
73+
74+
/**
75+
* @return list<string|\Closure>
76+
*/
77+
public function getValueTransformers(): array
78+
{
79+
return $this->valueTransformers;
80+
}
81+
82+
/**
83+
* @param list<string|\Closure> $valueTransformers
84+
*/
85+
public function withValueTransformers(array $valueTransformers): self
86+
{
87+
return new self($this->name, $this->type, $valueTransformers);
88+
}
89+
90+
public function withAdditionalValueTransformer(string|\Closure $valueTransformer): self
91+
{
92+
$valueTransformers = $this->valueTransformers;
93+
94+
$valueTransformers[] = $valueTransformer;
95+
$valueTransformers = array_values(array_unique($valueTransformers));
96+
97+
return $this->withValueTransformers($valueTransformers);
6598
}
6699

67100
/**
101+
* @deprecated since Symfony 7.4, use "getValueTransformers" instead
102+
*
68103
* @return list<string|\Closure>
69104
*/
70105
public function getNativeToStreamValueTransformer(): array
71106
{
72-
return $this->nativeToStreamValueTransformers;
107+
trigger_deprecation('symfony/json-streamer', '7.4', 'The "%s()" method is deprecated, use "%s::getValueTransformers()" instead.', __METHOD__, self::class);
108+
109+
return $this->valueTransformers;
73110
}
74111

75112
/**
113+
* @deprecated since Symfony 7.4, use "withValueTransformers" instead
114+
*
76115
* @param list<string|\Closure> $nativeToStreamValueTransformers
77116
*/
78117
public function withNativeToStreamValueTransformers(array $nativeToStreamValueTransformers): self
79118
{
119+
trigger_deprecation('symfony/json-streamer', '7.4', 'The "%s()" method is deprecated, use "%s::withValueTransformers()" instead.', __METHOD__, self::class);
120+
80121
return new self($this->name, $this->type, $nativeToStreamValueTransformers, $this->streamToNativeValueTransformers);
81122
}
82123

124+
/**
125+
* @deprecated since Symfony 7.4, use "withAdditionalValueTransformer" instead
126+
*/
83127
public function withAdditionalNativeToStreamValueTransformer(string|\Closure $nativeToStreamValueTransformer): self
84128
{
85-
$nativeToStreamValueTransformers = $this->nativeToStreamValueTransformers;
129+
trigger_deprecation('symfony/json-streamer', '7.4', 'The "%s()" method is deprecated, use "%s::withAdditionalValueTransformer()" instead.', __METHOD__, self::class);
130+
131+
$nativeToStreamValueTransformers = $this->valueTransformers;
86132

87133
$nativeToStreamValueTransformers[] = $nativeToStreamValueTransformer;
88134
$nativeToStreamValueTransformers = array_values(array_unique($nativeToStreamValueTransformers));
@@ -91,24 +137,37 @@ public function withAdditionalNativeToStreamValueTransformer(string|\Closure $na
91137
}
92138

93139
/**
140+
* @deprecated since Symfony 7.4, use "getValueTransformers" instead
141+
*
94142
* @return list<string|\Closure>
95143
*/
96144
public function getStreamToNativeValueTransformers(): array
97145
{
98-
return $this->streamToNativeValueTransformers;
146+
trigger_deprecation('symfony/json-streamer', '7.4', 'The "%s()" method is deprecated, use "%s::getValueTransformers()" instead.', __METHOD__, self::class);
147+
148+
return $this->streamToNativeValueTransformers ?? [];
99149
}
100150

101151
/**
152+
* @deprecated since Symfony 7.4, use "withValueTransformers" instead
153+
*
102154
* @param list<string|\Closure> $streamToNativeValueTransformers
103155
*/
104156
public function withStreamToNativeValueTransformers(array $streamToNativeValueTransformers): self
105157
{
106-
return new self($this->name, $this->type, $this->nativeToStreamValueTransformers, $streamToNativeValueTransformers);
158+
trigger_deprecation('symfony/json-streamer', '7.4', 'The "%s()" method is deprecated, use "%s::withValueTransformers()" instead.', __METHOD__, self::class);
159+
160+
return new self($this->name, $this->type, $this->valueTransformers, $streamToNativeValueTransformers);
107161
}
108162

163+
/**
164+
* @deprecated since Symfony 7.4, use "withAdditionalValueTransformer" instead
165+
*/
109166
public function withAdditionalStreamToNativeValueTransformer(string|\Closure $streamToNativeValueTransformer): self
110167
{
111-
$streamToNativeValueTransformers = $this->streamToNativeValueTransformers;
168+
trigger_deprecation('symfony/json-streamer', '7.4', 'The "%s()" method is deprecated, use "%s::withAdditionalValueTransformer()" instead.', __METHOD__, self::class);
169+
170+
$streamToNativeValueTransformers = $this->streamToNativeValueTransformers ?? [];
112171

113172
$streamToNativeValueTransformers[] = $streamToNativeValueTransformer;
114173
$streamToNativeValueTransformers = array_values(array_unique($streamToNativeValueTransformers));

Mapping/Read/AttributePropertyMetadataLoader.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function load(string $className, array $options = [], array $context = []
6666

6767
$result[$streamedName] = $initialMetadata
6868
->withType($valueTransformerService::getStreamValueType())
69-
->withAdditionalStreamToNativeValueTransformer($valueTransformer);
69+
->withAdditionalValueTransformer($valueTransformer);
7070

7171
continue;
7272
}
@@ -83,7 +83,7 @@ public function load(string $className, array $options = [], array $context = []
8383

8484
$result[$streamedName] = $initialMetadata
8585
->withType($this->typeResolver->resolve($parameterReflection))
86-
->withAdditionalStreamToNativeValueTransformer($valueTransformer);
86+
->withAdditionalValueTransformer($valueTransformer);
8787
}
8888

8989
return $result;

Mapping/Read/DateTimeTypePropertyMetadataLoader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function load(string $className, array $options = [], array $context = []
4444

4545
$metadata = $metadata
4646
->withType(StringToDateTimeValueTransformer::getStreamValueType())
47-
->withAdditionalStreamToNativeValueTransformer('json_streamer.value_transformer.string_to_date_time');
47+
->withAdditionalValueTransformer('json_streamer.value_transformer.string_to_date_time');
4848
}
4949
}
5050

Mapping/Write/AttributePropertyMetadataLoader.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function load(string $className, array $options = [], array $context = []
6666

6767
$result[$streamedName] = $initialMetadata
6868
->withType($valueTransformerService::getStreamValueType())
69-
->withAdditionalNativeToStreamValueTransformer($valueTransformer);
69+
->withAdditionalValueTransformer($valueTransformer);
7070

7171
continue;
7272
}
@@ -79,7 +79,7 @@ public function load(string $className, array $options = [], array $context = []
7979

8080
$result[$streamedName] = $initialMetadata
8181
->withType($this->typeResolver->resolve($valueTransformerReflection))
82-
->withAdditionalNativeToStreamValueTransformer($valueTransformer);
82+
->withAdditionalValueTransformer($valueTransformer);
8383
}
8484

8585
return $result;

Mapping/Write/DateTimeTypePropertyMetadataLoader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function load(string $className, array $options = [], array $context = []
3939
if ($type instanceof ObjectType && is_a($type->getClassName(), \DateTimeInterface::class, true)) {
4040
$metadata = $metadata
4141
->withType(DateTimeToStringValueTransformer::getStreamValueType())
42-
->withAdditionalNativeToStreamValueTransformer('json_streamer.value_transformer.date_time_to_string');
42+
->withAdditionalValueTransformer('json_streamer.value_transformer.date_time_to_string');
4343
}
4444
}
4545

Read/StreamReaderGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ private function createDataModel(Type $type, array $options = [], array $context
135135
'name' => $propertyMetadata->getName(),
136136
'value' => $this->createDataModel($propertyMetadata->getType(), $options, $context),
137137
'accessor' => function (string $accessor) use ($propertyMetadata): string {
138-
foreach ($propertyMetadata->getStreamToNativeValueTransformers() as $valueTransformer) {
138+
foreach ($propertyMetadata->getValueTransformers() as $valueTransformer) {
139139
if (\is_string($valueTransformer)) {
140140
$accessor = "\$valueTransformers->get('$valueTransformer')->transform($accessor, \$options)";
141141

Tests/Mapping/Read/AttributePropertyMetadataLoaderTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ public function testRetrieveValueTransformer()
4242
]), TypeResolver::create());
4343

4444
$this->assertEquals([
45-
'id' => new PropertyMetadata('id', Type::string(), [], [DivideStringAndCastToIntValueTransformer::class]),
46-
'active' => new PropertyMetadata('active', Type::string(), [], [StringToBooleanValueTransformer::class]),
47-
'name' => new PropertyMetadata('name', Type::string(), [], [\Closure::fromCallable('strtoupper')]),
48-
'range' => new PropertyMetadata('range', Type::string(), [], [\Closure::fromCallable(DummyWithValueTransformerAttributes::explodeRange(...))]),
45+
'id' => new PropertyMetadata('id', Type::string(), [DivideStringAndCastToIntValueTransformer::class]),
46+
'active' => new PropertyMetadata('active', Type::string(), [StringToBooleanValueTransformer::class]),
47+
'name' => new PropertyMetadata('name', Type::string(), [\Closure::fromCallable('strtoupper')]),
48+
'range' => new PropertyMetadata('range', Type::string(), [\Closure::fromCallable(DummyWithValueTransformerAttributes::explodeRange(...))]),
4949
], $loader->load(DummyWithValueTransformerAttributes::class));
5050
}
5151

Tests/Mapping/Read/DateTimeTypePropertyMetadataLoaderTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ public function testAddStringToDateTimeValueTransformer()
2929
]));
3030

3131
$this->assertEquals([
32-
'interface' => new PropertyMetadata('interface', Type::string(), [], ['json_streamer.value_transformer.string_to_date_time']),
33-
'immutable' => new PropertyMetadata('immutable', Type::string(), [], ['json_streamer.value_transformer.string_to_date_time']),
32+
'interface' => new PropertyMetadata('interface', Type::string(), ['json_streamer.value_transformer.string_to_date_time']),
33+
'immutable' => new PropertyMetadata('immutable', Type::string(), ['json_streamer.value_transformer.string_to_date_time']),
3434
'other' => new PropertyMetadata('other', Type::object(self::class)),
3535
], $loader->load(self::class));
3636
}

Write/StreamWriterGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ private function createDataModel(Type $type, string $accessor, array $options =
135135
foreach ($propertiesMetadata as $streamedName => $propertyMetadata) {
136136
$propertyAccessor = $propertyMetadata->getName() ? $accessor.'->'.$propertyMetadata->getName() : 'null';
137137

138-
foreach ($propertyMetadata->getNativeToStreamValueTransformer() as $valueTransformer) {
138+
foreach ($propertyMetadata->getValueTransformers() as $valueTransformer) {
139139
if (\is_string($valueTransformer)) {
140140
$valueTransformerServiceAccessor = "\$valueTransformers->get('$valueTransformer')";
141141
$propertyAccessor = "{$valueTransformerServiceAccessor}->transform($propertyAccessor, ['_current_object' => $accessor] + \$options)";

0 commit comments

Comments
 (0)