Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Serializer] add union types #41926

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

Merged
merged 1 commit into from
Jul 1, 2021
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
5 changes: 1 addition & 4 deletions 5 src/Symfony/Component/Serializer/Annotation/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ final class Context
*
* @throws InvalidArgumentException
*/
public function __construct(array $options = [], array $context = [], array $normalizationContext = [], array $denormalizationContext = [], $groups = [])
public function __construct(array $options = [], array $context = [], array $normalizationContext = [], array $denormalizationContext = [], string|array $groups = [])
{
if (!$context) {
if (!array_intersect((array_keys($options)), ['normalizationContext', 'groups', 'context', 'value', 'denormalizationContext'])) {
Expand All @@ -48,9 +48,6 @@ public function __construct(array $options = [], array $context = [], array $nor
$context = $options['value'] ?? $options['context'] ?? [];
}
}
if (!\is_string($groups) && !\is_array($groups)) {
throw new \TypeError(sprintf('"%s": Expected parameter $groups to be a string or an array of strings, got "%s".', __METHOD__, get_debug_type($groups)));
}

$normalizationContext = $options['normalizationContext'] ?? $normalizationContext;
$denormalizationContext = $options['denormalizationContext'] ?? $denormalizationContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,32 +25,11 @@
#[\Attribute(\Attribute::TARGET_CLASS)]
class DiscriminatorMap
{
/**
* @var string
*/
private $typeProperty;

/**
* @var array
*/
private $mapping;

/**
* @param string $typeProperty
*
* @throws InvalidArgumentException
*/
public function __construct($typeProperty, array $mapping = null)
public function __construct(string $typeProperty, array $mapping)
{
if (\is_array($typeProperty)) {
trigger_deprecation('symfony/serializer', '5.3', 'Passing an array as first argument to "%s" is deprecated. Use named arguments instead.', __METHOD__);

$mapping = $typeProperty['mapping'] ?? null;
$typeProperty = $typeProperty['typeProperty'] ?? null;
} elseif (!\is_string($typeProperty)) {
throw new \TypeError(sprintf('"%s": Argument $typeProperty was expected to be a string or array, got "%s".', __METHOD__, get_debug_type($typeProperty)));
}

if (empty($typeProperty)) {
throw new InvalidArgumentException(sprintf('Parameter "typeProperty" of annotation "%s" cannot be empty.', static::class));
}
Expand Down
19 changes: 4 additions & 15 deletions 19 src/Symfony/Component/Serializer/Annotation/Groups.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,36 +32,25 @@ class Groups

/**
* @param string|string[] $groups
*
* @throws InvalidArgumentException
*/
public function __construct($groups)
public function __construct(string|array $groups)
{
if (\is_string($groups)) {
$groups = (array) $groups;
} elseif (!\is_array($groups)) {
throw new \TypeError(sprintf('"%s": Parameter $groups is expected to be a string or an array of strings, got "%s".', __METHOD__, get_debug_type($groups)));
} elseif (isset($groups['value'])) {
trigger_deprecation('symfony/serializer', '5.3', 'Passing an array of properties as first argument to "%s" is deprecated. Use named arguments instead.', __METHOD__);
$groups = (array) $groups;

$groups = (array) $groups['value'];
}
if (empty($groups)) {
throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" cannot be empty.', static::class));
}

foreach ($groups as $group) {
if (!\is_string($group)) {
throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" must be a string or an array of strings.', static::class));
if (!\is_string($group) || '' === $group) {
throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" must be a string or an array of non-empty strings.', static::class));
}
}

$this->groups = $groups;
}

/**
* Gets groups.
*
* @return string[]
*/
public function getGroups()
Expand Down
19 changes: 2 additions & 17 deletions 19 src/Symfony/Component/Serializer/Annotation/MaxDepth.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,11 @@
#[\Attribute(\Attribute::TARGET_METHOD | \Attribute::TARGET_PROPERTY)]
class MaxDepth
{
/**
* @var int
*/
private $maxDepth;

/**
* @param int $maxDepth
*/
public function __construct($maxDepth)
public function __construct(int $maxDepth)
{
if (\is_array($maxDepth)) {
trigger_deprecation('symfony/serializer', '5.3', 'Passing an array as first argument to "%s" is deprecated. Use named arguments instead.', __METHOD__);

if (!isset($maxDepth['value'])) {
throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" should be set.', static::class));
}
$maxDepth = $maxDepth['value'];
}

if (!\is_int($maxDepth) || $maxDepth <= 0) {
if ($maxDepth <= 0) {
throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" must be a positive integer.', static::class));
}

Expand Down
19 changes: 2 additions & 17 deletions 19 src/Symfony/Component/Serializer/Annotation/SerializedName.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,11 @@
#[\Attribute(\Attribute::TARGET_METHOD | \Attribute::TARGET_PROPERTY)]
final class SerializedName
{
/**
* @var string
*/
private $serializedName;

/**
* @param string $serializedName
*/
public function __construct($serializedName)
public function __construct(string $serializedName)
{
if (\is_array($serializedName)) {
trigger_deprecation('symfony/serializer', '5.3', 'Passing an array as first argument to "%s" is deprecated. Use named arguments instead.', __METHOD__);

if (!isset($serializedName['value'])) {
throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" should be set.', static::class));
}
$serializedName = $serializedName['value'];
}

if (!\is_string($serializedName) || empty($serializedName)) {
if (empty($serializedName)) {
throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" must be a non-empty string.', static::class));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function __construct(array $classesToCompile, ClassMetadataFactoryInterfa
/**
* {@inheritdoc}
*/
public function warmUp($cacheDir)
public function warmUp(string $cacheDir)
{
$metadatas = [];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function __construct(array $encoders = [])
/**
* {@inheritdoc}
*/
final public function encode($data, string $format, array $context = [])
final public function encode(mixed $data, string $format, array $context = [])
{
return $this->getEncoder($format, $context)->encode($data, $format, $context);
}
Expand Down
2 changes: 1 addition & 1 deletion 2 src/Symfony/Component/Serializer/Encoder/CsvEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function __construct(array $defaultContext = [])
/**
* {@inheritdoc}
*/
public function encode($data, string $format, array $context = [])
public function encode(mixed $data, string $format, array $context = [])
{
$handle = fopen('php://temp,', 'w+');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ interface EncoderInterface
*
* @throws UnexpectedValueException
*/
public function encode($data, string $format, array $context = []);
public function encode(mixed $data, string $format, array $context = []);

/**
* Checks whether the serializer can encode to given format.
Expand Down
4 changes: 1 addition & 3 deletions 4 src/Symfony/Component/Serializer/Encoder/JsonEncode.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,9 @@ public function __construct(array $defaultContext = [])
}

/**
* Encodes PHP data to a JSON string.
*
* {@inheritdoc}
*/
public function encode($data, string $format, array $context = [])
public function encode(mixed $data, string $format, array $context = [])
{
$options = $context[self::OPTIONS] ?? $this->defaultContext[self::OPTIONS];

Expand Down
2 changes: 1 addition & 1 deletion 2 src/Symfony/Component/Serializer/Encoder/JsonEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function __construct(JsonEncode $encodingImpl = null, JsonDecode $decodin
/**
* {@inheritdoc}
*/
public function encode($data, string $format, array $context = [])
public function encode(mixed $data, string $format, array $context = [])
{
return $this->encodingImpl->encode($data, self::FORMAT, $context);
}
Expand Down
17 changes: 5 additions & 12 deletions 17 src/Symfony/Component/Serializer/Encoder/XmlEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public function __construct(array $defaultContext = [])
/**
* {@inheritdoc}
*/
public function encode($data, string $format, array $context = [])
public function encode(mixed $data, string $format, array $context = [])
{
$encoderIgnoredNodeTypes = $context[self::ENCODER_IGNORED_NODE_TYPES] ?? $this->defaultContext[self::ENCODER_IGNORED_NODE_TYPES];
$ignorePiNode = \in_array(\XML_PI_NODE, $encoderIgnoredNodeTypes, true);
Expand Down Expand Up @@ -217,10 +217,7 @@ final protected function appendCData(\DOMNode $node, string $val): bool
return true;
}

/**
* @param \DOMDocumentFragment $fragment
*/
final protected function appendDocumentFragment(\DOMNode $node, $fragment): bool
final protected function appendDocumentFragment(\DOMNode $node, \DOMDocumentFragment $fragment): bool
{
if ($fragment instanceof \DOMDocumentFragment) {
$node->appendChild($fragment);
Expand Down Expand Up @@ -357,11 +354,9 @@ private function parseXmlValue(\DOMNode $node, array $context = [])
/**
* Parse the data and convert it to DOMElements.
*
* @param array|object $data
*
* @throws NotEncodableValueException
*/
private function buildXml(\DOMNode $parentNode, $data, string $xmlRootNodeName = null): bool
private function buildXml(\DOMNode $parentNode, mixed $data, string $xmlRootNodeName = null): bool
{
$append = true;
$removeEmptyTags = $this->context[self::REMOVE_EMPTY_TAGS] ?? $this->defaultContext[self::REMOVE_EMPTY_TAGS] ?? false;
Expand Down Expand Up @@ -431,10 +426,8 @@ private function buildXml(\DOMNode $parentNode, $data, string $xmlRootNodeName =

/**
* Selects the type of node to create and appends it to the parent.
*
* @param array|object $data
*/
private function appendNode(\DOMNode $parentNode, $data, string $nodeName, string $key = null): bool
private function appendNode(\DOMNode $parentNode, mixed $data, string $nodeName, string $key = null): bool
{
$node = $this->dom->createElement($nodeName);
if (null !== $key) {
Expand Down Expand Up @@ -462,7 +455,7 @@ private function needsCdataWrapping(string $val): bool
*
* @throws NotEncodableValueException
*/
private function selectNodeType(\DOMNode $node, $val): bool
private function selectNodeType(\DOMNode $node, mixed $val): bool
{
if (\is_array($val)) {
return $this->buildXml($node, $val);
Expand Down
2 changes: 1 addition & 1 deletion 2 src/Symfony/Component/Serializer/Encoder/YamlEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function __construct(Dumper $dumper = null, Parser $parser = null, array
/**
* {@inheritdoc}
*/
public function encode($data, string $format, array $context = [])
public function encode(mixed $data, string $format, array $context = [])
{
$context = array_merge($this->defaultContext, $context);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function getMappingForClass(string $class): ?ClassDiscriminatorMapping
/**
* {@inheritdoc}
*/
public function getMappingForMappedObject($object): ?ClassDiscriminatorMapping
public function getMappingForMappedObject(object|string $object): ?ClassDiscriminatorMapping
{
if ($this->classMetadataFactory->hasMetadataFor($object)) {
$metadata = $this->classMetadataFactory->getMetadataFor($object);
Expand All @@ -65,7 +65,7 @@ public function getMappingForMappedObject($object): ?ClassDiscriminatorMapping
/**
* {@inheritdoc}
*/
public function getTypeForMappedObject($object): ?string
public function getTypeForMappedObject(object|string $object): ?string
{
if (null === $mapping = $this->getMappingForMappedObject($object)) {
return null;
Expand All @@ -74,7 +74,7 @@ public function getTypeForMappedObject($object): ?string
return $mapping->getMappedObjectType($object);
}

private function resolveMappingForMappedObject($object)
private function resolveMappingForMappedObject(object|string $object)
{
$reflectionClass = new \ReflectionClass($object);
if ($parentClass = $reflectionClass->getParentClass()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,7 @@ public function getClassForType(string $type): ?string
return $this->typesMapping[$type] ?? null;
}

/**
* @param object|string $object
*/
public function getMappedObjectType($object): ?string
public function getMappedObjectType(object|string $object): ?string
{
foreach ($this->typesMapping as $type => $typeClass) {
if (is_a($object, $typeClass)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,7 @@ interface ClassDiscriminatorResolverInterface
{
public function getMappingForClass(string $class): ?ClassDiscriminatorMapping;

/**
* @param object|string $object
*/
public function getMappingForMappedObject($object): ?ClassDiscriminatorMapping;
public function getMappingForMappedObject(object|string $object): ?ClassDiscriminatorMapping;

/**
* @param object|string $object
*/
public function getTypeForMappedObject($object): ?string;
public function getTypeForMappedObject(object|string $object): ?string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function __construct(ClassMetadataFactoryInterface $decorated, CacheItemP
/**
* {@inheritdoc}
*/
public function getMetadataFor($value)
public function getMetadataFor(string|object $value)
{
$class = $this->getClass($value);

Expand All @@ -67,7 +67,7 @@ public function getMetadataFor($value)
/**
* {@inheritdoc}
*/
public function hasMetadataFor($value)
public function hasMetadataFor(mixed $value)
{
return $this->decorated->hasMetadataFor($value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function __construct(LoaderInterface $loader)
/**
* {@inheritdoc}
*/
public function getMetadataFor($value)
public function getMetadataFor(string|object $value)
{
$class = $this->getClass($value);

Expand Down Expand Up @@ -67,7 +67,7 @@ public function getMetadataFor($value)
/**
* {@inheritdoc}
*/
public function hasMetadataFor($value)
public function hasMetadataFor(mixed $value)
{
return \is_object($value) || (\is_string($value) && (class_exists($value) || interface_exists($value, false)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,16 @@ interface ClassMetadataFactoryInterface
* {@link \Symfony\Component\Serializer\Mapping\Loader\LoaderInterface::loadClassMetadata()} method for further
* configuration. At last, the new object is returned.
*
* @param string|object $value
*
* @return ClassMetadataInterface
*
* @throws InvalidArgumentException
*/
public function getMetadataFor($value);
public function getMetadataFor(string|object $value);

/**
* Checks if class has metadata.
*
* @param mixed $value
*
* @return bool
*/
public function hasMetadataFor($value);
public function hasMetadataFor(mixed $value);
}
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.