1 | <?php |
||
45 | class JmsSerializer implements SerializerInterface |
||
46 | { |
||
47 | /** |
||
48 | * @var string |
||
49 | */ |
||
50 | const SERIALIZER_FORMAT = 'json'; |
||
51 | |||
52 | /** |
||
53 | * This class is used to deserialize the response. |
||
54 | * This entity should only care about the elasticsearch part of the |
||
55 | * response, not about the source part or the results underneath hits->hits. |
||
56 | * |
||
57 | * @var string |
||
58 | */ |
||
59 | private $deserializerClass = 'Elastification\Client\Serializer\JmsSerializer\SearchResponseEntity'; |
||
60 | |||
61 | /** |
||
62 | * @var Serializer |
||
63 | */ |
||
64 | private $jms; |
||
65 | |||
66 | /** |
||
67 | * @var SerializationContext |
||
68 | */ |
||
69 | private $jmsSerializeContext; |
||
70 | |||
71 | /** |
||
72 | * @var DeserializationContext |
||
73 | */ |
||
74 | private $jmsDeserializeContext; |
||
75 | /** |
||
76 | * |
||
77 | * @var array |
||
78 | */ |
||
79 | private $indexTypeClassMap; |
||
80 | /** |
||
81 | * |
||
82 | * @var SourceSubscribingHandler |
||
83 | */ |
||
84 | private $handler; |
||
85 | |||
86 | /** |
||
87 | * @param Serializer $jms |
||
88 | * @param SourceSubscribingHandler $handler This must be the same instance as the one you used for the |
||
89 | * init of the Serializer |
||
90 | * @param array $indexTypeClassMap Represents an array of ['indexName' => ['typeName' => |
||
91 | * 'SourceClassName']] |
||
92 | */ |
||
93 | 9 | function __construct(Serializer $jms, SourceSubscribingHandler $handler, array $indexTypeClassMap) |
|
|
|||
94 | { |
||
95 | 9 | $this->jms = $jms; |
|
96 | 9 | $this->indexTypeClassMap = $indexTypeClassMap; |
|
97 | 9 | $this->handler = $handler; |
|
98 | 9 | } |
|
99 | |||
100 | /** |
||
101 | * Serializes given data to string |
||
102 | * |
||
103 | * @param mixed $data |
||
104 | * @param array $params |
||
105 | * |
||
106 | * @return string |
||
107 | */ |
||
108 | 4 | public function serialize($data, array $params = array()) |
|
109 | { |
||
110 | 4 | return $this->jms->serialize( |
|
111 | 4 | $data, |
|
112 | 4 | self::SERIALIZER_FORMAT, |
|
113 | 4 | $this->determineContext($this->jmsSerializeContext, $params) |
|
114 | 4 | ); |
|
115 | } |
||
116 | |||
117 | /** |
||
118 | * Deserializes given data to array or object |
||
119 | * |
||
120 | * @param string $data |
||
121 | * @param array $params |
||
122 | * |
||
123 | * @return GatewayInterface |
||
124 | */ |
||
125 | 4 | public function deserialize($data, array $params = array()) |
|
126 | { |
||
127 | 4 | $sourceClass = $this->getSourceClassFromMapping($params); |
|
128 | 4 | $this->handler->setSourceDeSerClass($sourceClass); |
|
129 | |||
130 | 4 | return new NativeObjectGateway( |
|
131 | 4 | $this->jms->deserialize( |
|
132 | 4 | $data, |
|
133 | 4 | $this->deserializerClass, |
|
134 | 4 | self::SERIALIZER_FORMAT, |
|
135 | 4 | $this->determineContext($this->jmsDeserializeContext, $params) |
|
136 | 4 | ) |
|
137 | 4 | ); |
|
138 | } |
||
139 | |||
140 | /** |
||
141 | * gets the source class. |
||
142 | * |
||
143 | * @param array $params |
||
144 | * |
||
145 | * @return string |
||
146 | * @author Daniel Wendlandt |
||
147 | */ |
||
148 | 4 | private function getSourceClassFromMapping(array $params) |
|
149 | { |
||
150 | 4 | $index = $params['index']; |
|
151 | 4 | $type = $params['type']; |
|
152 | 4 | if (!isset($this->indexTypeClassMap[$index])) { |
|
153 | throw new DeserializationFailureException('Cannot find index in source class map: ' . $index); |
||
154 | } |
||
155 | |||
156 | 4 | if (!isset($this->indexTypeClassMap[$index][$type])) { |
|
157 | throw new DeserializationFailureException( |
||
158 | 'Cannot find type in source class map: ' . $type . ' in index ' . $index |
||
159 | ); |
||
160 | } |
||
161 | |||
162 | 4 | return $this->indexTypeClassMap[$index][$type]; |
|
163 | } |
||
164 | |||
165 | /** |
||
166 | * Simple rule: Use internal if present but let params override any internal. |
||
167 | * |
||
168 | * @param Context $internalProperty |
||
169 | * @param array $params |
||
170 | * |
||
171 | * @return Context|null |
||
172 | * @author Mario Mueller |
||
173 | */ |
||
174 | 8 | private function determineContext($internalProperty, array $params) |
|
189 | |||
190 | /** |
||
191 | * @return SerializationContext |
||
192 | * @author Mario Mueller (autogenerated code) |
||
193 | */ |
||
194 | 1 | public function getJmsSerializeContext() |
|
198 | |||
199 | /** |
||
200 | * @param SerializationContext $jmsContext |
||
201 | * |
||
202 | * @return void |
||
203 | * @author Mario Mueller (autogenerated code) |
||
204 | */ |
||
205 | 3 | public function setJmsSerializeContext(SerializationContext $jmsContext) |
|
209 | |||
210 | /** |
||
211 | * @return DeserializationContext |
||
212 | * @author Mario Mueller (autogenerated code) |
||
213 | */ |
||
214 | 1 | public function getJmsDeserializeContext() |
|
218 | |||
219 | /** |
||
220 | * @param DeserializationContext $jmsDeserializeContext |
||
221 | * |
||
222 | * @return void |
||
223 | * @author Mario Mueller (autogenerated code) |
||
224 | */ |
||
225 | 3 | public function setJmsDeserializeContext($jmsDeserializeContext) |
|
229 | |||
230 | /** |
||
231 | * @return string |
||
232 | * @author Mario Mueller (autogenerated code) |
||
233 | */ |
||
234 | 1 | public function getDeserializerClass() |
|
238 | |||
239 | /** |
||
240 | * @param string $deserializerClass |
||
241 | * |
||
242 | * @return void |
||
243 | * @author Mario Mueller (autogenerated code) |
||
244 | */ |
||
245 | 1 | public function setDeserializerClass($deserializerClass) |
|
249 | } |
||
250 |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.