1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* AppserverIo\RestApi\Serializer |
5
|
|
|
* |
6
|
|
|
* NOTICE OF LICENSE |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
9
|
|
|
* that is available through the world-wide-web at this URL: |
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
11
|
|
|
* |
12
|
|
|
* PHP version 5 |
13
|
|
|
* |
14
|
|
|
* @author Tim Wagner <[email protected]> |
15
|
|
|
* @copyright 2018 TechDivision GmbH <[email protected]> |
16
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
17
|
|
|
* @link https://github.com/appserver-io/restapi |
18
|
|
|
* @link http://www.appserver.io |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace AppserverIo\RestApi; |
22
|
|
|
|
23
|
|
|
use JMS\Serializer\SerializerBuilder; |
24
|
|
|
use JMS\Serializer\SerializerInterface as JmsSerializerInterface; |
25
|
|
|
use JMS\Serializer\Naming\SerializedNameAnnotationStrategy; |
26
|
|
|
use JMS\Serializer\Naming\IdenticalPropertyNamingStrategy; |
27
|
|
|
use AppserverIo\Psr\HttpMessage\Protocol; |
28
|
|
|
use AppserverIo\Psr\Application\ApplicationInterface; |
29
|
|
|
use AppserverIo\Psr\Servlet\Http\HttpServletRequestInterface; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Generic serializer implementation. |
33
|
|
|
* |
34
|
|
|
* @author Tim Wagner <[email protected]> |
35
|
|
|
* @copyright 2018 TechDivision GmbH <[email protected]> |
36
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
37
|
|
|
* @link https://github.com/appserver-io/restapi |
38
|
|
|
* @link http://www.appserver.io |
39
|
|
|
*/ |
40
|
|
|
class Serializer implements \AppserverIo\RestApi\SerializerInterface |
41
|
|
|
{ |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* The array with the available header => type mappings. |
45
|
|
|
* |
46
|
|
|
* @var array |
47
|
|
|
*/ |
48
|
|
|
protected $headerToTypeMappings = array( |
49
|
|
|
'application/json' => 'json', |
50
|
|
|
'application/xml' => 'xml' |
51
|
|
|
); |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* The application instance. |
55
|
|
|
* |
56
|
|
|
* @var \AppserverIo\Psr\Application\ApplicationInterface |
57
|
|
|
*/ |
58
|
|
|
protected $application; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Initializes the instance with the passed serializer implementation. |
62
|
|
|
* |
63
|
|
|
* @param \AppserverIo\Psr\Application\ApplicationInterface $application The application instance |
64
|
|
|
*/ |
65
|
|
|
public function __construct(ApplicationInterface $application) |
66
|
|
|
{ |
67
|
|
|
$this->application = $application; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Returns the JMS serializer instance. |
72
|
|
|
* |
73
|
|
|
* @return \JMS\Serializer\Serializer The serializer instance |
74
|
|
|
*/ |
75
|
|
|
protected function getInstance() |
76
|
|
|
{ |
77
|
|
|
return $this->application->search('JmsSerializer'); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Mapps the Accept header of the passed HTTP servlet request to the type supported by the serializer. |
82
|
|
|
* |
83
|
|
|
* @param \AppserverIo\Psr\Servlet\Http\HttpServletRequestInterface $servletRequest The HTTP servlet request instance |
84
|
|
|
* |
85
|
|
|
* @return string The serializer type to use |
86
|
|
|
*/ |
87
|
|
|
public function mapHeader(HttpServletRequestInterface $servletRequest) |
88
|
|
|
{ |
89
|
|
|
return $this->headerToTypeMappings[$servletRequest->getHeader(Protocol::HEADER_ACCEPT)]; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Serializes the passed data with the given format. |
94
|
|
|
* |
95
|
|
|
* @param mixed $data The data to serialize |
96
|
|
|
* @param string $format The format used for serialization |
97
|
|
|
* |
98
|
|
|
* @return mixed The serialized data |
99
|
|
|
*/ |
100
|
|
|
public function serialize($data, $format = 'json') |
101
|
|
|
{ |
102
|
|
|
return $this->getInstance()->serialize($data, $format); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Unserialize the passed data into an object and return it. |
107
|
|
|
* |
108
|
|
|
* @param string $data The data to unserialize |
109
|
|
|
* @param string $type The type to convert the unserialized data to |
110
|
|
|
* @param string $format The format, the data has been serialized to |
111
|
|
|
* |
112
|
|
|
* @return mixed The unserialized data |
113
|
|
|
*/ |
114
|
|
|
public function unserialize($data, $type, $format = 'json') |
115
|
|
|
{ |
116
|
|
|
return $this->getInstance()->deserialize($data, $type, $format); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|