8000 [Serializer] [WIP] Added annotations and MetadataAwareNormalizer by Nyholm · Pull Request #19374 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[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
wants to merge 13 commits into from
Next Next commit
Added annotations and MetadataAwareNormalizer
  • Loading branch information
Nyholm committed Sep 1, 2018
commit 489a8d8079d8bee72ee0c45dc572628f959a2776
65 changes: 65 additions & 0 deletions src/Symfony/Component/Serializer/Annotation/Accessor.php
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()
Copy link
Member
@dunglas dunglas May 3, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be accessor and mutatorright? In case the user use a different name than get* or set*?

Copy link
Member

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)

Copy link
Member
@dunglas dunglas May 3, 2018

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")

{
return $this->getter;
}

/**
* @return string
*/
public function getSetter()
{
return $this->setter;
}
}
32 changes: 32 additions & 0 deletions src/Symfony/Component/Serializer/Annotation/Exclude.php
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)) {
Copy link
Member

Choose a reason for hiding this comment

The 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;
}
}
50 changes: 50 additions & 0 deletions src/Symfony/Component/Serializer/Annotation/ExclusionPolicy.php
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;
}
}
32 changes: 32 additions & 0 deletions src/Symfony/Component/Serializer/Annotation/Expose.php
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
Copy link
Member

Choose a reason for hiding this comment

The 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;
}
}
6 changes: 6 additions & 0 deletions src/Symfony/Component/Serializer/Annotation/MaxDepth.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ class MaxDepth
*/
private $maxDepth;

/**
* @param array $data
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

useless

*/
public function __construct(array $data)
{
if (!isset($data['value'])) {
Expand All @@ -41,6 +44,9 @@ public function __construct(array $data)
$this->maxDepth = $data['value'];
}

/**
* @return int
*/
public function getMaxDepth()
{
return $this->maxDepth;
Expand Down
44 changes: 44 additions & 0 deletions src/Symfony/Component/Serializer/Annotation/ReadOnly.php
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;
}
}
40 changes: 40 additions & 0 deletions src/Symfony/Component/Serializer/Annotation/SerializedName.php
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;
}
}
52 changes: 52 additions & 0 deletions src/Symfony/Component/Serializer/Annotation/Type.php
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
Copy link
Member

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?

Copy link
Member Author

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.

Copy link
Member Author
10618

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:

/**
 * @Serializer\Type("App\Entity\User[]")
 **/
private $users;

Copy link
Member

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.

Copy link
Member Author

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.

{
/**
* @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;
}
}
Loading
0