-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[JsonStreamer] Add number object support #59915
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,8 +17,10 @@ | |
| use Symfony\Component\JsonStreamer\Mapping\PropertyMetadataLoader; | ||
| use Symfony\Component\JsonStreamer\Mapping\PropertyMetadataLoaderInterface; | ||
| use Symfony\Component\JsonStreamer\Mapping\Write\AttributePropertyMetadataLoader; | ||
| use Symfony\Component\JsonStreamer\Mapping\Write\DateTimeTypePropertyMetadataLoader; | ||
| use Symfony\Component\JsonStreamer\Mapping\Write\ValueObjectTypePropertyMetadataLoader; | ||
| use Symfony\Component\JsonStreamer\ValueTransformer\BcMathNumberToStringValueTransformer; | ||
| use Symfony\Component\JsonStreamer\ValueTransformer\DateTimeToStringValueTransformer; | ||
| use Symfony\Component\JsonStreamer\ValueTransformer\GmpNumberToStringValueTransformer; | ||
| use Symfony\Component\JsonStreamer\ValueTransformer\ValueTransformerInterface; | ||
| use Symfony\Component\JsonStreamer\Write\StreamWriterGenerator; | ||
| use Symfony\Component\TypeInfo\Type; | ||
|
|
@@ -88,6 +90,8 @@ public static function create(array $valueTransformers = [], ?string $streamWrit | |
| $streamWritersDir ??= sys_get_temp_dir().'/json_streamer/write'; | ||
| $valueTransformers += [ | ||
| 'json_streamer.value_transformer.date_time_to_string' => new DateTimeToStringValueTransformer(), | ||
| 'json_streamer.value_transformer.bc_math_number_to_string' => new BcMathNumberToStringValueTransformer(), | ||
| 'json_streamer.value_transformer.gmp_number_to_string' => new GmpNumberToStringValueTransformer(), | ||
| ]; | ||
|
|
||
| $valueTransformersContainer = new class($valueTransformers) implements ContainerInterface { | ||
|
|
@@ -109,15 +113,15 @@ public function get(string $id): ValueTransformerInterface | |
|
|
||
| $typeContextFactory = new TypeContextFactory(class_exists(PhpDocParser::class) ? new StringTypeResolver() : null); | ||
|
|
||
| $propertyMetadataLoader = new GenericTypePropertyMetadataLoader( | ||
| new DateTimeTypePropertyMetadataLoader( | ||
| $propertyMetadataLoader = new ValueObjectTypePropertyMetadataLoader( | ||
| new GenericTypePropertyMetadataLoader( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why changing the order of decorators ? |
||
| new AttributePropertyMetadataLoader( | ||
| new PropertyMetadataLoader(TypeResolver::create()), | ||
| $valueTransformersContainer, | ||
| TypeResolver::create(), | ||
| ), | ||
| $typeContextFactory, | ||
| ), | ||
| $typeContextFactory, | ||
| ); | ||
|
|
||
| return new self($valueTransformersContainer, $propertyMetadataLoader, $streamWritersDir); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -73,9 +73,7 @@ public function withNativeToStreamValueTransformers(array $nativeToStreamValueTr | |
| public function withAdditionalNativeToStreamValueTransformer(string|\Closure $nativeToStreamValueTransformer): self | ||
| { | ||
| $nativeToStreamValueTransformers = $this->nativeToStreamValueTransformers; | ||
|
|
||
| $nativeToStreamValueTransformers[] = $nativeToStreamValueTransformer; | ||
| $nativeToStreamValueTransformers = array_values(array_unique($nativeToStreamValueTransformers)); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Stop trying to make value transformers unique, as it does not add any value, and didn't work anyway because |
||
|
|
||
| return $this->withNativeToStreamValueTransformers($nativeToStreamValueTransformers); | ||
| } | ||
|
|
@@ -99,9 +97,7 @@ public function withStreamToNativeValueTransformers(array $streamToNativeValueTr | |
| public function withAdditionalStreamToNativeValueTransformer(string|\Closure $streamToNativeValueTransformer): self | ||
| { | ||
| $streamToNativeValueTransformers = $this->streamToNativeValueTransformers; | ||
|
|
||
| $streamToNativeValueTransformers[] = $streamToNativeValueTransformer; | ||
| $streamToNativeValueTransformers = array_values(array_unique($streamToNativeValueTransformers)); | ||
|
|
||
| return $this->withStreamToNativeValueTransformers($streamToNativeValueTransformers); | ||
| } | ||
|
|
||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the Symfony package. | ||
| * | ||
| * (c) Fabien Potencier <fabien@symfony.com> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace Symfony\Component\JsonStreamer\Mapping\Read; | ||
|
|
||
| use BcMath\Number; | ||
| use Symfony\Component\JsonStreamer\Exception\InvalidArgumentException; | ||
| use Symfony\Component\JsonStreamer\Mapping\PropertyMetadataLoaderInterface; | ||
| use Symfony\Component\JsonStreamer\ValueTransformer\IntStringToBcMathNumberValueTransformer; | ||
| use Symfony\Component\JsonStreamer\ValueTransformer\IntStringToGmpNumberValueTransformer; | ||
| use Symfony\Component\JsonStreamer\ValueTransformer\StringToDateTimeValueTransformer; | ||
| use Symfony\Component\TypeInfo\Type; | ||
| use Symfony\Component\TypeInfo\Type\UnionType; | ||
|
|
||
| /** | ||
| * Transforms scalar to value object. | ||
| * | ||
| * @author Mathias Arlaud <mathias.arlaud@gmail.com> | ||
| * | ||
| * @internal | ||
| */ | ||
| final class ValueObjectTypePropertyMetadataLoader implements PropertyMetadataLoaderInterface | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should this be split as well for the same reasons (#59915 (comment))? |
||
| { | ||
| public function __construct( | ||
| private PropertyMetadataLoaderInterface $decorated, | ||
| ) { | ||
| } | ||
|
|
||
| public function load(string $className, array $options = [], array $context = []): array | ||
| { | ||
| $result = $this->decorated->load($className, $options, $context); | ||
|
|
||
| foreach ($result as &$metadata) { | ||
| $type = $metadata->getType(); | ||
| $newTypeParts = []; | ||
|
|
||
| foreach ($type instanceof UnionType ? $type->getTypes() : [$type] as $t) { | ||
| $newTypePart = $t; | ||
|
|
||
| if ($t->isIdentifiedBy(\DateTimeInterface::class)) { | ||
| if ($t->isIdentifiedBy(\DateTime::class)) { | ||
| throw new InvalidArgumentException('The "DateTime" class is not supported. Use "DateTimeImmutable" instead.'); | ||
| } | ||
|
|
||
| $metadata = $metadata->withAdditionalStreamToNativeValueTransformer('json_streamer.value_transformer.string_to_date_time'); | ||
| $newTypePart = StringToDateTimeValueTransformer::getStreamValueType(); | ||
| } elseif ($t->isIdentifiedBy(Number::class)) { | ||
| $metadata = $metadata->withAdditionalStreamToNativeValueTransformer('json_streamer.value_transformer.int_string_to_bc_math_number'); | ||
| $newTypePart = IntStringToBcMathNumberValueTransformer::getStreamValueType(); | ||
| } elseif ($t->isIdentifiedBy(\GMP::class)) { | ||
| $metadata = $metadata->withAdditionalStreamToNativeValueTransformer('json_streamer.value_transformer.int_string_to_gmp_number'); | ||
| $newTypePart = IntStringToGmpNumberValueTransformer::getStreamValueType(); | ||
| } | ||
|
|
||
| $newTypeParts = [ | ||
| ...$newTypeParts, | ||
| ...($newTypePart instanceof UnionType ? $newTypePart->getTypes() : [$newTypePart]), | ||
| ]; | ||
| } | ||
|
|
||
| $newTypeParts = array_values(array_unique($newTypeParts)); | ||
| $newType = \count($newTypeParts) > 1 ? Type::union(...$newTypeParts) : $newTypeParts[0]; | ||
|
|
||
| $metadata = $metadata->withType($newType); | ||
| } | ||
|
|
||
| return $result; | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.