Skip to content

Commit 099b015

Browse files
committedOct 21, 2023
decorator
1 parent 12a2ed3 commit 099b015

File tree

5 files changed

+628
-3
lines changed

5 files changed

+628
-3
lines changed
 

‎.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22
###> symfony/framework-bundle ###
3+
.idea
34
/.env.local
45
/.env.local.php
56
/.env.*.local

‎composer.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,18 @@
77
"php": ">=8.1",
88
"ext-ctype": "*",
99
"ext-iconv": "*",
10+
"phpdocumentor/reflection-docblock": "^5.3",
11+
"phpstan/phpdoc-parser": "^1.24",
1012
"symfony/console": "6.3.*",
1113
"symfony/dotenv": "6.3.*",
1214
"symfony/flex": "^2",
1315
"symfony/framework-bundle": "6.3.*",
16+
"symfony/property-access": "6.3.*",
17+
"symfony/property-info": "6.3.*",
1418
"symfony/runtime": "6.3.*",
19+
"symfony/serializer": "6.3.*",
1520
"symfony/yaml": "6.3.*"
1621
},
17-
"require-dev": {
18-
},
1922
"config": {
2023
"allow-plugins": {
2124
"php-http/discovery": true,

‎composer.lock

Lines changed: 575 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace App\Controller;
4+
5+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
6+
use Symfony\Component\Routing\Annotation\Route;
7+
use Symfony\Component\Serializer\Encoder\JsonEncoder;
8+
use Symfony\Component\Serializer\SerializerInterface;
9+
10+
11+
class DecoratorDesignPattern extends AbstractController
12+
{
13+
14+
#[Route(path: "/decorate", name: "decorate", methods: ["GET"])]
15+
public function decorateSerializer(SerializerInterface $serializer)
16+
{
17+
dd($serializer->serialize(['name' => 'rahul'], JsonEncoder::FORMAT));
18+
}
19+
}

‎src/Services/CustomSerializer.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace App\Services;
4+
5+
use Symfony\Component\DependencyInjection\Attribute\AsDecorator;
6+
use Symfony\Component\Serializer\SerializerInterface;
7+
8+
9+
#[AsDecorator('serializer')]
10+
class CustomSerializer implements SerializerInterface
11+
{
12+
13+
public function __construct(private readonly SerializerInterface $serializer)
14+
{
15+
}
16+
17+
public function serialize(mixed $data, string $format, array $context = []): string
18+
{
19+
$serializedData = $this->serializer->serialize($data, $format);
20+
21+
return sprintf("serialized data :%s", $serializedData);
22+
}
23+
24+
public function deserialize(mixed $data, string $type, string $format, array $context = []): mixed
25+
{
26+
return "custom deserialization was successful";
27+
}
28+
}

0 commit comments

Comments
 (0)
Please sign in to comment.