Skip to content

[Serializer] Add PHP Attributes to serializer examples #15099

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 1 commit into from
Mar 31, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions components/serializer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,26 @@ Then, create your groups definition:
// ...
}

.. code-block:: php-attributes

namespace Acme;

use Symfony\Component\Serializer\Annotation\Groups;

class MyObj
{
#[Groups(['group1', 'group2'])]
public $foo;

#[Groups(['group3'])]
public function getBar() // is* methods are also supported
{
return $this->bar;
}

// ...
}

.. code-block:: yaml

Acme\MyObj:
Expand Down Expand Up @@ -437,6 +457,20 @@ Option 1: Using ``@Ignore`` Annotation
public $bar;
}

.. code-block:: php-attributes

namespace App\Model;

use Symfony\Component\Serializer\Annotation\Ignore;

class MyClass
{
public $foo;

#[Ignore]
public $bar;
}

.. code-block:: yaml

App\Model\MyClass:
Expand Down Expand Up @@ -658,6 +692,25 @@ defines a ``Person`` entity with a ``firstName`` property:
// ...
}

.. code-block:: php-attributes

namespace App\Entity;

use Symfony\Component\Serializer\Annotation\SerializedName;

class Person
{
#[SerializedName('customer_name')]
private $firstName;

public function __construct($firstName)
{
$this->firstName = $firstName;
}

// ...
}

.. code-block:: yaml

App\Entity\Person:
Expand Down Expand Up @@ -1214,6 +1267,20 @@ Here, we set it to 2 for the ``$child`` property:
// ...
}

.. code-block:: php-attributes

namespace Acme;

use Symfony\Component\Serializer\Annotation\MaxDepth;

class MyObj
{
#[MaxDepth(2)]
public $child;

// ...
}

.. code-block:: yaml

Acme\MyObj:
Expand Down Expand Up @@ -1524,6 +1591,23 @@ and ``BitBucketCodeRepository`` classes:
// ...
}

.. code-block:: php-attributes

namespace App;

use App\BitBucketCodeRepository;
use App\GitHubCodeRepository;
use Symfony\Component\Serializer\Annotation\DiscriminatorMap;

#[DiscriminatorMap(typeProperty: 'type', mapping: [
'github' => GitHubCodeRepository::class,
'bitbucket' => BitBucketCodeRepository::class,
])]
interface CodeRepository
{
// ...
}

.. code-block:: yaml

App\CodeRepository:
Expand Down