-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Serializer] [WIP] Added annotations and MetadataAwareNormalizer #19374
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
Closed
Closed
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
489a8d8
Added annotations and MetadataAwareNormalizer
Nyholm d7ff93b
Reverted test fix
Nyholm e453509
Removed code not needed
Nyholm a683887
Make constructor parameter optional
Nyholm 4a6b08f
Code style from fabpot.io
Nyholm d292915
Renamed setters=>mutator, getters=>accessors
Nyholm aaada1c
minor
Nyholm bb09437
Removed some doc blocks
Nyholm 0fe9da5
Updates according to feedback and style changes
Nyholm d28a7e2
Added support for Yaml
Nyholm 0e96e86
Added support for XML
Nyholm ec0474d
Updating tests
Nyholm 05a0691
Typo
Nyholm 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,26 @@ | ||
<?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; | ||
|
||
/** | ||
* @Annotation | ||
* @Target({"PROPERTY"}) | ||
* | ||
* @author Tobias Nyholm <tobias.nyholm@gmail.com> | ||
*/ | ||
final class Exclude | ||
{ | ||
public function getValue(): bool | ||
{ | ||
return true; | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
src/Symfony/Component/Serializer/Annotation/ExclusionPolicy.php
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,53 @@ | ||
<?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 | ||
* @Target({"CLASS"}) | ||
* | ||
* @author Tobias Nyholm <tobias.nyholm@gmail.com> | ||
*/ | ||
final class ExclusionPolicy | ||
{ | ||
const NONE = 'NONE'; | ||
const ALL = 'ALL'; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $policy; | ||
|
||
public function __construct(array $data) | ||
{ | ||
if (!isset($data['value']) || !$data['value']) { | ||
throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" cannot be empty.', \get_class($this))); | ||
} | ||
|
||
if (!\is_string($data['value'])) { | ||
throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" must be a string.', \get_class($this))); | ||
} | ||
|
||
$this->policy = strtoupper($data['value']); | ||
|
||
if (self::NONE !== $this->policy && self::ALL !== $this->policy) { | ||
throw new InvalidArgumentException('Exclusion policy must either be "ALL", or "NONE".'); | ||
} | ||
} | ||
|
||
public function getPolicy(): string | ||
{ | ||
return $this->policy; | ||
} | ||
} |
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,26 @@ | ||
<?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; | ||
|
||
/** | ||
* @Annotation | ||
* @Target({"PROPERTY", "METHOD"}) | ||
* | ||
* @author Tobias Nyholm <tobias.nyholm@gmail.com> | ||
*/ | ||
final class Expose | ||
{ | ||
public function getValue(): bool | ||
{ | ||
return true; | ||
} | ||
} |
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,66 @@ | ||
<?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 | ||
* @Target({"PROPERTY"}) | ||
* | ||
* @author Tobias Nyholm <tobias.nyholm@gmail.com> | ||
*/ | ||
final class Methods | ||
{ | ||
/** | ||
* @var null|string | ||
*/ | ||
private $accessor; | ||
|
||
/** | ||
* @var null|string | ||
*/ | ||
private $mutator; | ||
|
||
public function __construct(array $data) | ||
{ | ||
if (empty($data)) { | ||
throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" cannot be empty.', \get_class($this))); | ||
} | ||
|
||
foreach (array('accessor', 'mutator') as $parameter) { | ||
if (!isset($data[$parameter]) || !$data[$parameter]) { | ||
continue; | ||
} | ||
|
||
if (!\is_string($data[$parameter])) { | ||
throw new InvalidArgumentException(sprintf('Parameter "%s" of annotation "%s" must be a string.', $parameter, \get_class($this))); | ||
} | ||
|
||
$this->$parameter = $data[$parameter]; | ||
} | ||
|
||
if (null === $this->accessor && null === $this->mutator) { | ||
throw new InvalidArgumentException(sprintf('Either option "getter" or "setter" must be given for annotation %s', \get_class($this))); | ||
} | ||
} | ||
|
||
public function getAccessor(): ?string | ||
{ | ||
return $this->accessor; | ||
} | ||
|
||
public function getMutator(): ?string | ||
{ | ||
return $this->mutator; | ||
} | ||
} |
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,48 @@ | ||
<?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 | ||
* @Target({"PROPERTY", "CLASS"}) | ||
* | ||
* @author Tobias Nyholm <tobias.nyholm@gmail.com> | ||
*/ | ||
final class ReadOnly | ||
{ | ||
/** | ||
* @var bool | ||
*/ | ||
private $readOnly; | ||
|
||
public function __construct(array $data = array()) | ||
{ | ||
if (empty($data) || !isset($data['value'])) { | ||
$this->readOnly = true; | ||
|
||
return; | ||
} | ||
|
||
if (!\is_bool($data['value'])) { | ||
throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" must be a boolean.', \get_class($this))); | ||
} | ||
|
||
$this->readOnly = $data['value']; | ||
} | ||
|
||
public function getReadOnly(): bool | ||
{ | ||
return $this->readOnly; | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src/Symfony/Component/Serializer/Annotation/SerializedName.php
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,46 @@ | ||
<?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 | ||
* @Target({"PROPERTY", "METHOD"}) | ||
* | ||
* @author Tobias Nyholm <tobias.nyholm@gmail.com> | ||
*/ | ||
final class SerializedName | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $name; | ||
|
||
public function __construct(array $data) | ||
{ | ||
if (!isset($data['value']) || !$data['value']) { | ||
throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" cannot be empty.', \get_class($this))); | ||
} | ||
|
||
if (!\is_string($data['value'])) { | ||
throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" must be a string.', \get_class($this))); | ||
} | ||
|
||
$this->name = $data['value']; | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
return $this->name; | ||
} | ||
} |
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,55 @@ | ||
<?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 | ||
* @Target({"PROPERTY"}) | ||
* | ||
* @author Tobias Nyholm <tobias.nyholm@gmail.com> | ||
*/ | ||
final class Type | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $type; | ||
|
||
public function __construct(array $data) | ||
{ | ||
if (!isset($data['value']) || !$data['value']) { | ||
throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" cannot be empty.', \get_class($this))); | ||
} | ||
|
||
if (!\is_string($data['value'])) { | ||
throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" must be a string.', \get_class($this))); | ||
} | ||
|
||
$type = $data['value']; | ||
|
||
if (false !== $pos = strpos($type, '\\')) { | ||
// This is a referencet to a class | ||
if (0 !== $pos) { | ||
throw new InvalidArgumentException(sprintf('When referring to an class you you must begin the type with backslash (\\) you provided "%s" for annotation "%s".', $type, \get_class($this))); | ||
} | ||
} | ||
|
||
$this->type = $data['value']; | ||
} | ||
|
||
public function getType(): string | ||
{ | ||
return $this->type; | ||
} | ||
} |
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.
What is the use case for this one?
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.
I think the purpose is to define types of the values in an array. Im not sure though. I will check 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.
Indeed. It is when you write:
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.
IIUC, this use case is already supported through the PropertyInfo component. I would be in favor of dropping it.
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.
Yeah, If the feature is there Im happy to drop it. But the current state of the PR needs this class.