Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8190b74

Browse files
committed
Merge branch '5.2' into 5.x
* 5.2: [Serializer] Add PHP Attributes to serializer examples
2 parents 5aec411 + 8c0d93d commit 8190b74

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed

components/serializer.rst

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,26 @@ Then, create your groups definition:
315315
// ...
316316
}
317317
318+
.. code-block:: php-attributes
319+
320+
namespace Acme;
321+
322+
use Symfony\Component\Serializer\Annotation\Groups;
323+
324+
class MyObj
325+
{
326+
#[Groups(['group1', 'group2'])]
327+
public $foo;
328+
329+
#[Groups(['group3'])]
330+
public function getBar() // is* methods are also supported
331+
{
332+
return $this->bar;
333+
}
334+
335+
// ...
336+
}
337+
318338
.. code-block:: yaml
319339
320340
Acme\MyObj:
@@ -437,6 +457,20 @@ Option 1: Using ``@Ignore`` Annotation
437457
public $bar;
438458
}
439459
460+
.. code-block:: php-attributes
461+
462+
namespace App\Model;
463+
464+
use Symfony\Component\Serializer\Annotation\Ignore;
465+
466+
class MyClass
467+
{
468+
public $foo;
469+
470+
#[Ignore]
471+
public $bar;
472+
}
473+
440474
.. code-block:: yaml
441475
442476
App\Model\MyClass:
@@ -658,6 +692,25 @@ defines a ``Person`` entity with a ``firstName`` property:
658692
// ...
659693
}
660694
695+
.. code-block:: php-attributes
696+
697+
namespace App\Entity;
698+
699+
use Symfony\Component\Serializer\Annotation\SerializedName;
700+
701+
class Person
702+
{
703+
#[SerializedName('customer_name')]
704+
private $firstName;
705+
706+
public function __construct($firstName)
707+
{
708+
$this->firstName = $firstName;
709+
}
710+
711+
// ...
712+
}
713+
661714
.. code-block:: yaml
662715
663716
App\Entity\Person:
@@ -1218,6 +1271,20 @@ Here, we set it to 2 for the ``$child`` property:
12181271
// ...
12191272
}
12201273
1274+
.. code-block:: php-attributes
1275+
1276+
namespace Acme;
1277+
1278+
use Symfony\Component\Serializer\Annotation\MaxDepth;
1279+
1280+
class MyObj
1281+
{
1282+
#[MaxDepth(2)]
1283+
public $child;
1284+
1285+
// ...
1286+
}
1287+
12211288
.. code-block:: yaml
12221289
12231290
Acme\MyObj:
@@ -1528,6 +1595,23 @@ and ``BitBucketCodeRepository`` classes:
15281595
// ...
15291596
}
15301597
1598+
.. code-block:: php-attributes
1599+
1600+
namespace App;
1601+
1602+
use App\BitBucketCodeRepository;
1603+
use App\GitHubCodeRepository;
1604+
use Symfony\Component\Serializer\Annotation\DiscriminatorMap;
1605+
1606+
#[DiscriminatorMap(typeProperty: 'type', mapping: [
1607+
'github' => GitHubCodeRepository::class,
1608+
'bitbucket' => BitBucketCodeRepository::class,
1609+
])]
1610+
interface CodeRepository
1611+
{
1612+
// ...
1613+
}
1614+
15311615
.. code-block:: yaml
15321616
15331617
App\CodeRepository:

0 commit comments

Comments
 (0)