-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Serializer] Allow to provide (de)normalization context in mapping #39399
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?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\Serializer\Annotation; | ||
|
||
use Symfony\Component\Serializer\Exception\InvalidArgumentException; | ||
|
||
/** | ||
* Annotation class for @Context(). | ||
* | ||
* @Annotation | ||
* @Target({"PROPERTY", "METHOD"}) | ||
* | ||
* @author Maxime Steinhausser <maxime.steinhausser@gmail.com> | ||
*/ | ||
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)] | ||
final class Context | ||
{ | ||
private $context; | ||
private $normalizationContext; | ||
private $denormalizationContext; | ||
private $groups; | ||
|
||
/** | ||
* @throws InvalidArgumentException | ||
*/ | ||
public function __construct(array $options = [], array $context = [], array $normalizationContext = [], array $denormalizationContext = [], array $groups = []) | ||
{ | ||
if (!$context) { | ||
if (!array_intersect((array_keys($options)), ['normalizationContext', 'groups', 'context', 'value', 'denormalizationContext'])) { | ||
// gracefully supports context as first, unnamed attribute argument if it cannot be confused with Doctrine-style options | ||
$context = $options; | ||
} else { | ||
// If at least one of the options match, it's likely to be Doctrine-style options. Search for the context inside: | ||
$context = $options['value'] ?? $options['context'] ?? []; | ||
} | ||
} | ||
|
||
$normalizationContext = $options['normalizationContext'] ?? $normalizationContext; | ||
$denormalizationContext = $options['denormalizationContext'] ?? $denormalizationContext; | ||
|
||
foreach (compact(['context', 'normalizationContext', 'denormalizationContext']) as $key => $value) { | ||
if (!\is_array($value)) { | ||
throw new InvalidArgumentException(sprintf('Option "%s" of annotation "%s" must be an array.', $key, static::class)); | ||
} | ||
} | ||
|
||
if (!$context && !$normalizationContext && !$denormalizationContext) { | ||
throw new InvalidArgumentException(sprintf('At least one of the "context", "normalizationContext", or "denormalizationContext" options of annotation "%s" must be provided as a non-empty array.', static::class)); | ||
} | ||
|
||
$groups = (array) ($options['groups'] ?? $groups); | ||
|
||
foreach ($groups as $group) { | ||
if (!\is_string($group)) { | ||
throw new InvalidArgumentException(sprintf('Parameter "groups" of annotation "%s" must be a string or an array of strings. Got "%s".', static::class, get_debug_type($group))); | ||
} | ||
} | ||
|
||
$this->context = $context; | ||
$this->normalizationContext = $normalizationContext; | ||
$this->denormalizationContext = $denormalizationContext; | ||
$this->groups = $groups; | ||
} | ||
|
||
public function getContext(): array | ||
{ | ||
return $this->context; | ||
} | ||
|
||
public function getNormalizationContext(): array | ||
{ | ||
return $this->normalizationContext; | ||
} | ||
|
||
public function getDenormalizationContext(): array | ||
{ | ||
return $this->denormalizationContext; | ||
} | ||
|
||
public function getGroups(): array | ||
{ | ||
return $this->groups; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden
E864
Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using
*
should be fine since #33540 already relies on this. So no one should name their groups*
.Otherwise, previous version used an empty string, but we could simply opt for a distinct
defaultDenormalizationContext
property as well.