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
26 changes: 26 additions & 0 deletions src/Symfony/Component/Serializer/Annotation/Exclude.php
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 src/Symfony/Component/Serializer/Annotation/ExclusionPolicy.php
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;
}
}
26 changes: 26 additions & 0 deletions src/Symfony/Component/Serializer/Annotation/Expose.php
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;
}
}
66 changes: 66 additions & 0 deletions src/Symfony/Component/Serializer/Annotation/Methods.php
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;
}
}
48 changes: 48 additions & 0 deletions src/Symfony/Component/Serializer/Annotation/ReadOnly.php
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 src/Symfony/Component/Serializer/Annotation/SerializedName.php
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;
}
}
55 changes: 55 additions & 0 deletions src/Symfony/Component/Serializer/Annotation/Type.php
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
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

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;

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;
}
}
Loading