|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the StfalconApiBundle. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Stfalcon LLC <stfalcon.com> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
declare(strict_types=1); |
|
12
|
|
|
|
|
13
|
|
|
namespace StfalconStudio\ApiBundle\Serializer; |
|
14
|
|
|
|
|
15
|
|
|
use Symfony\Component\Serializer\Serializer as BaseSerializer; |
|
16
|
|
|
use Symfony\Component\Serializer\SerializerInterface; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Serializer. |
|
20
|
|
|
*/ |
|
21
|
|
|
class Serializer |
|
22
|
|
|
{ |
|
23
|
|
|
public const DEFAULT_FORMAT = 'json'; |
|
24
|
|
|
|
|
25
|
|
|
/** @var SerializerInterface|BaseSerializer */ |
|
26
|
|
|
protected $symfonySerializer; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param SerializerInterface|BaseSerializer $serializer |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct(SerializerInterface $serializer) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->symfonySerializer = $serializer; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param object|array $object |
|
38
|
|
|
* @param string $serializationGroup |
|
39
|
|
|
* @param mixed[] $context |
|
40
|
|
|
* |
|
41
|
|
|
* @return string |
|
42
|
|
|
*/ |
|
43
|
|
|
public function serialize($object, string $serializationGroup, array $context = []): string |
|
44
|
|
|
{ |
|
45
|
|
|
$preparedContext = \array_merge( |
|
46
|
|
|
$context, |
|
47
|
|
|
[ |
|
48
|
|
|
'group' => $serializationGroup, |
|
49
|
|
|
'json_encode_options' => JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE, |
|
50
|
|
|
] |
|
51
|
|
|
); |
|
52
|
|
|
|
|
53
|
|
|
return $this->symfonySerializer->serialize($object, self::DEFAULT_FORMAT, $preparedContext); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param mixed $data |
|
58
|
|
|
* @param string $type |
|
59
|
|
|
* @param string $format |
|
60
|
|
|
* @param mixed[] $context |
|
61
|
|
|
* |
|
62
|
|
|
* @return object|array |
|
63
|
|
|
*/ |
|
64
|
|
|
public function deserialize($data, string $type, string $format, array $context = []) |
|
65
|
|
|
{ |
|
66
|
|
|
return $this->symfonySerializer->deserialize($data, $type, $format, $context); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|