-
-
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
Changes from 1 commit
489a8d8
d7ff93b
e453509
a683887
4a6b08f
d292915
aaada1c
bb09437
0fe9da5
d28a7e2
0e96e86
ec0474d
05a0691
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\Serializer\Annotation; | ||
|
||
use Symfony\Component\Serializer\Exception\InvalidArgumentException; | ||
|
||
/** | ||
* @Annotation | ||
* @Target({"PROPERTY"}) | ||
* | ||
* @author Tobias Nyholm <tobias.nyholm@gmail.com> | ||
*/ | ||
final class Accessor | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $getter; | ||
/** | ||
* @var string | ||
*/ | ||
private $setter; | ||
|
||
/** | ||
* @param array $data | ||
*/ | ||
public function __construct(array $data) | ||
{ | ||
if (empty($data)) { | ||
throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" cannot be empty.', get_class($this))); | ||
} | ||
|
||
foreach (array('getter', 'setter') 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->getter && null === $this->setter) { | ||
throw new InvalidArgumentException(sprintf('Either option "getter" or "setter" must be given for annotation %s', get_class($this))); | ||
} | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getGetter() | ||
{ | ||
return $this->getter; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getSetter() | ||
{ | ||
return $this->setter; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\Serializer\Annotation; | ||
|
||
use Symfony\Component\Serializer\Exception\InvalidArgumentException; | ||
|
||
/** | ||
* @Annotation | ||
* @Target({"PROPERTY"}) | ||
* | ||
* @author Tobias Nyholm <tobias.nyholm@gmail.com> | ||
*/ | ||
final class Exclude | ||
{ | ||
/** | ||
* @param array $data | ||
*/ | ||
public function __construct(array $data = array()) | ||
{ | ||
if (!empty($data)) { | ||
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. If I remember correctly, we don't do that for other annotations. Can't you just drop the constructor? |
||
throw new InvalidArgumentException(sprintf('No parameter is allowed for annotation "%s".', get_class($this))); | ||
} | ||
} | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
public function getValue() | ||
{ | ||
return true; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
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; | ||
|
||
/** | ||
* @param array $data | ||
*/ | ||
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".'); | ||
} | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getPolicy() | ||
{ | ||
return $this->policy; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\Serializer\Annotation; | ||
|
||
use Symfony\Component\Serializer\Exception\InvalidArgumentException; | ||
|
||
/** | ||
* @Annotation | ||
* @Target({"PROPERTY", "METHOD"}) | ||
* | ||
* @author Tobias Nyholm <tobias.nyholm@gmail.com> | ||
*/ | ||
final class Expose | ||
{ | ||
/** | ||
* @param array $data | ||
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. useless |
||
*/ | ||
public function __construct(array $data = array()) | ||
{ | ||
if (!empty($data)) { | ||
throw new InvalidArgumentException(sprintf('No parameter is allowed for annotation "%s".', get_class($this))); | ||
} | ||
} | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
public function getValue() | ||
{ | ||
return true; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,9 @@ class MaxDepth | |
*/ | ||
private $maxDepth; | ||
|
||
/** | ||
* @param array $data | ||
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. useless |
||
*/ | ||
public function __construct(array $data) | ||
{ | ||
if (!isset($data['value'])) { | ||
|
@@ -41,6 +44,9 @@ public function __construct(array $data) | |
$this->maxDepth = $data['value']; | ||
} | ||
|
||
/** | ||
* @return int | ||
*/ | ||
public function getMaxDepth() | ||
{ | ||
return $this->maxDepth; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
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; | ||
|
||
/** | ||
* @param array $data | ||
*/ | ||
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']; | ||
} | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
public function getReadOnly() | ||
{ | ||
return $this->readOnly; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
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; | ||
|
||
/** | ||
* @param array $data | ||
*/ | ||
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() | ||
{ | ||
return $this->name; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\Serializer\Annotation; | ||
|
||
use Symfony\Component\Serializer\Exception\InvalidArgumentException; | ||
|
||
/** | ||
* @Annotation | ||
* @Target({"PROPERTY"}) | ||
* | ||
* @author Tobias Nyholm <tobias.nyholm@gmail.com> | ||
*/ | ||
final class Type | ||
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. What is the use case for this one? 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. 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 commentThe reason will be displayed to describe this comment to others. Learn more. Indeed. It is when you write: /**
* @Serializer\Type("App\Entity\User[]")
**/
private $users; 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. 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 commentThe 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. |
||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $type; | ||
|
||
/** | ||
* @param array $data | ||
*/ | ||
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 ($pos !== 0) { | ||
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']; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getType() | ||
{ | ||
return $this->type; | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.
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.
should be
accessor
andmutator
right? In case the user use a different name thanget*
orset*
?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.
: string
(docblocks can now be converted to return types everywhere)Uh oh!
There was an error while loading. Please reload this page.
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 about just
@Methods
as annotation name? Usage:@Methods(accessor="getSomething", mutator="setSomething")