BaseSerializationTest.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. <?php
  2. /*
  3. * Copyright 2011 Johannes M. Schmitt <schmittjoh@gmail.com>
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. namespace JMS\SerializerBundle\Tests\Serializer;
  18. use JMS\SerializerBundle\Tests\Fixtures\AccessorOrderParent;
  19. use JMS\SerializerBundle\Tests\Fixtures\AccessorOrderChild;
  20. use JMS\SerializerBundle\Tests\Fixtures\GetSetObject;
  21. use JMS\SerializerBundle\Tests\Fixtures\IndexedCommentsBlogPost;
  22. use JMS\SerializerBundle\Tests\Fixtures\CurrencyAwareOrder;
  23. use JMS\SerializerBundle\Tests\Fixtures\CurrencyAwarePrice;
  24. use JMS\SerializerBundle\Tests\Fixtures\Order;
  25. use Symfony\Component\Yaml\Inline;
  26. use JMS\SerializerBundle\Serializer\YamlSerializationVisitor;
  27. use Doctrine\Common\Collections\ArrayCollection;
  28. use Symfony\Component\Form\Form;
  29. use Symfony\Component\Form\FormError;
  30. use Symfony\Component\Validator\ConstraintViolation;
  31. use Symfony\Component\Validator\ConstraintViolationList;
  32. use JMS\SerializerBundle\Serializer\Handler\DeserializationHandlerInterface;
  33. use JMS\SerializerBundle\Serializer\Handler\SerializationHandlerInterface;
  34. use JMS\SerializerBundle\Tests\Fixtures\AuthorList;
  35. use JMS\SerializerBundle\Serializer\VisitorInterface;
  36. use JMS\SerializerBundle\Serializer\XmlDeserializationVisitor;
  37. use JMS\SerializerBundle\Serializer\Construction\UnserializeObjectConstructor;
  38. use JMS\SerializerBundle\Serializer\JsonDeserializationVisitor;
  39. use JMS\SerializerBundle\Tests\Fixtures\Log;
  40. use JMS\SerializerBundle\Serializer\Handler\ArrayCollectionHandler;
  41. use JMS\SerializerBundle\Serializer\Handler\ObjectBasedCustomHandler;
  42. use JMS\SerializerBundle\Serializer\Handler\DateTimeHandler;
  43. use JMS\SerializerBundle\Serializer\Handler\FormErrorHandler;
  44. use JMS\SerializerBundle\Serializer\Handler\ConstraintViolationHandler;
  45. use JMS\SerializerBundle\Serializer\Handler\DoctrineProxyHandler;
  46. use JMS\SerializerBundle\Tests\Fixtures\Comment;
  47. use JMS\SerializerBundle\Tests\Fixtures\Author;
  48. use JMS\SerializerBundle\Tests\Fixtures\AuthorReadOnly;
  49. use JMS\SerializerBundle\Tests\Fixtures\BlogPost;
  50. use JMS\SerializerBundle\Tests\Fixtures\ObjectWithLifecycleCallbacks;
  51. use JMS\SerializerBundle\Tests\Fixtures\CircularReferenceParent;
  52. use JMS\SerializerBundle\Tests\Fixtures\InlineParent;
  53. use JMS\SerializerBundle\Tests\Fixtures\GroupsObject;
  54. use JMS\SerializerBundle\Serializer\XmlSerializationVisitor;
  55. use Doctrine\Common\Annotations\AnnotationReader;
  56. use JMS\SerializerBundle\Metadata\Driver\AnnotationDriver;
  57. use Metadata\MetadataFactory;
  58. use JMS\SerializerBundle\Tests\Fixtures\SimpleObject;
  59. use JMS\SerializerBundle\Tests\Fixtures\SimpleObjectProxy;
  60. use JMS\SerializerBundle\Tests\Fixtures\Price;
  61. use JMS\SerializerBundle\Serializer\Naming\CamelCaseNamingStrategy;
  62. use JMS\SerializerBundle\Serializer\Naming\SerializedNameAnnotationStrategy;
  63. use JMS\SerializerBundle\Serializer\JsonSerializationVisitor;
  64. use JMS\SerializerBundle\Serializer\Serializer;
  65. abstract class BaseSerializationTest extends \PHPUnit_Framework_TestCase
  66. {
  67. public function testString()
  68. {
  69. $this->assertEquals($this->getContent('string'), $this->serialize('foo'));
  70. if ($this->hasDeserializer()) {
  71. $this->assertEquals('foo', $this->deserialize($this->getContent('string'), 'string'));
  72. }
  73. }
  74. /**
  75. * @dataProvider getBooleans
  76. */
  77. public function testBooleans($strBoolean, $boolean)
  78. {
  79. $this->assertEquals($this->getContent('boolean_'.$strBoolean), $this->serialize($boolean));
  80. if ($this->hasDeserializer()) {
  81. $this->assertSame($boolean, $this->deserialize($this->getContent('boolean_'.$strBoolean), 'boolean'));
  82. }
  83. }
  84. public function getBooleans()
  85. {
  86. return array(array('true', true), array('false', false));
  87. }
  88. /**
  89. * @dataProvider getNumerics
  90. */
  91. public function testNumerics($key, $value)
  92. {
  93. $this->assertEquals($this->getContent($key), $this->serialize($value));
  94. if ($this->hasDeserializer()) {
  95. $this->assertEquals($value, $this->deserialize($this->getContent($key), is_double($value) ? 'double' : 'integer'));
  96. }
  97. }
  98. public function getNumerics()
  99. {
  100. return array(
  101. array('integer', 1),
  102. array('float', 4.533),
  103. array('float_trailing_zero', 1.0),
  104. );
  105. }
  106. public function testSimpleObject()
  107. {
  108. $this->assertEquals($this->getContent('simple_object'), $this->serialize($obj = new SimpleObject('foo', 'bar')));
  109. if ($this->hasDeserializer()) {
  110. $this->assertEquals($obj, $this->deserialize($this->getContent('simple_object'), get_class($obj)));
  111. }
  112. }
  113. public function testArrayStrings()
  114. {
  115. $data = array('foo', 'bar');
  116. $this->assertEquals($this->getContent('array_strings'), $this->serialize($data));
  117. if ($this->hasDeserializer()) {
  118. $this->assertEquals($data, $this->deserialize($this->getContent('array_strings'), 'array<string>'));
  119. }
  120. }
  121. public function testArrayBooleans()
  122. {
  123. $data = array(true, false);
  124. $this->assertEquals($this->getContent('array_booleans'), $this->serialize($data));
  125. if ($this->hasDeserializer()) {
  126. $this->assertEquals($data, $this->deserialize($this->getContent('array_booleans'), 'array<boolean>'));
  127. }
  128. }
  129. public function testArrayIntegers()
  130. {
  131. $data = array(1, 3, 4);
  132. $this->assertEquals($this->getContent('array_integers'), $this->serialize($data));
  133. if ($this->hasDeserializer()) {
  134. $this->assertEquals($data, $this->deserialize($this->getContent('array_integers'), 'array<integer>'));
  135. }
  136. }
  137. public function testArrayFloats()
  138. {
  139. $data = array(1.34, 3.0, 6.42);
  140. $this->assertEquals($this->getContent('array_floats'), $this->serialize($data));
  141. if ($this->hasDeserializer()) {
  142. $this->assertEquals($data, $this->deserialize($this->getContent('array_floats'), 'array<double>'));
  143. }
  144. }
  145. public function testArrayObjects()
  146. {
  147. $data = array(new SimpleObject('foo', 'bar'), new SimpleObject('baz', 'boo'));
  148. $this->assertEquals($this->getContent('array_objects'), $this->serialize($data));
  149. if ($this->hasDeserializer()) {
  150. $this->assertEquals($data, $this->deserialize($this->getContent('array_objects'), 'array<JMS\SerializerBundle\Tests\Fixtures\SimpleObject>'));
  151. }
  152. }
  153. public function testArrayMixed()
  154. {
  155. $this->assertEquals($this->getContent('array_mixed'), $this->serialize(array('foo', 1, true, new SimpleObject('foo', 'bar'), array(1, 3, true))));
  156. }
  157. public function testBlogPost()
  158. {
  159. $post = new BlogPost('This is a nice title.', $author = new Author('Foo Bar'), new \DateTime('2011-07-30 00:00', new \DateTimeZone('UTC')));
  160. $post->addComment($comment = new Comment($author, 'foo'));
  161. $this->assertEquals($this->getContent('blog_post'), $this->serialize($post));
  162. if ($this->hasDeserializer()) {
  163. $deserialized = $this->deserialize($this->getContent('blog_post'), get_class($post));
  164. $this->assertEquals('2011-07-30T00:00:00+0000', $this->getField($deserialized, 'createdAt')->format(\DateTime::ISO8601));
  165. $this->assertAttributeEquals('This is a nice title.', 'title', $deserialized);
  166. $this->assertAttributeSame(false, 'published', $deserialized);
  167. $this->assertAttributeEquals(new ArrayCollection(array($comment)), 'comments', $deserialized);
  168. $this->assertAttributeEquals($author, 'author', $deserialized);
  169. }
  170. }
  171. public function testReadOnly()
  172. {
  173. $author = new AuthorReadOnly(123, 'Ruud Kamphuis');
  174. $this->assertEquals($this->getContent('readonly'), $this->serialize($author));
  175. if ($this->hasDeserializer()) {
  176. $deserialized = $this->deserialize($this->getContent('readonly'), get_class($author));
  177. $this->assertNull($this->getField($deserialized, 'id'));
  178. $this->assertEquals('Ruud Kamphuis', $this->getField($deserialized, 'name'));
  179. }
  180. }
  181. public function testPrice()
  182. {
  183. $price = new Price(3);
  184. $this->assertEquals($this->getContent('price'), $this->serialize($price));
  185. if ($this->hasDeserializer()) {
  186. $deserialized = $this->deserialize($this->getContent('price'), get_class($price));
  187. $this->assertEquals(3, $this->getField($deserialized, 'price'));
  188. }
  189. }
  190. public function testOrder()
  191. {
  192. $order = new Order(new Price(12.34));
  193. $this->assertEquals($this->getContent('order'), $this->serialize($order));
  194. if ($this->hasDeserializer()) {
  195. $this->assertEquals($order, $this->deserialize($this->getContent('order'), get_class($order)));
  196. }
  197. }
  198. public function testCurrencyAwarePrice()
  199. {
  200. $price = new CurrencyAwarePrice(2.34);
  201. $this->assertEquals($this->getContent('currency_aware_price'), $this->serialize($price));
  202. if ($this->hasDeserializer()) {
  203. $this->assertEquals($price, $this->deserialize($this->getContent('currency_aware_price'), get_class($price)));
  204. }
  205. }
  206. public function testOrderWithCurrencyAwarePrice()
  207. {
  208. $order = new CurrencyAwareOrder(new CurrencyAwarePrice(1.23));
  209. $this->assertEquals($this->getContent('order_with_currency_aware_price'), $this->serialize($order));
  210. if ($this->hasDeserializer()) {
  211. $this->assertEquals($order, $this->deserialize($this->getContent('order_with_currency_aware_price'), get_class($order)));
  212. }
  213. }
  214. public function testArticle()
  215. {
  216. $article = new Article();
  217. $article->element = 'custom';
  218. $article->value = 'serialized';
  219. $result = $this->serialize($article);
  220. $this->assertEquals($this->getContent('article'), $result);
  221. if ($this->hasDeserializer()) {
  222. $this->assertEquals($article, $this->deserialize($result, 'JMS\SerializerBundle\Tests\Serializer\Article'));
  223. }
  224. }
  225. public function testInline()
  226. {
  227. $inline = new InlineParent();
  228. $result = $this->serialize($inline);
  229. $this->assertEquals($this->getContent('inline'), $result);
  230. //no deserialization support
  231. /*if ($this->hasDeserializer()) {
  232. $this->assertEquals($inline, $this->deserialize($result, 'JMS\SerializerBundle\Tests\Serializer\InlineParent'));
  233. }*/
  234. }
  235. /**
  236. * @group test
  237. */
  238. public function testLog()
  239. {
  240. $this->assertEquals($this->getContent('log'), $this->serialize($log = new Log()));
  241. if ($this->hasDeserializer()) {
  242. $deserialized = $this->deserialize($this->getContent('log'), get_class($log));
  243. $this->assertEquals($log, $deserialized);
  244. }
  245. }
  246. public function testCircularReference()
  247. {
  248. $object = new CircularReferenceParent();
  249. $this->assertEquals($this->getContent('circular_reference'), $this->serialize($object));
  250. if ($this->hasDeserializer()) {
  251. $deserialized = $this->deserialize($this->getContent('circular_reference'), get_class($object));
  252. $col = $this->getField($deserialized, 'collection');
  253. $this->assertEquals(2, count($col));
  254. $this->assertEquals('child1', $col[0]->getName());
  255. $this->assertEquals('child2', $col[1]->getName());
  256. $this->assertSame($deserialized, $col[0]->getParent());
  257. $this->assertSame($deserialized, $col[1]->getParent());
  258. $col = $this->getField($deserialized, 'anotherCollection');
  259. $this->assertEquals(2, count($col));
  260. $this->assertEquals('child1', $col[0]->getName());
  261. $this->assertEquals('child2', $col[1]->getName());
  262. $this->assertSame($deserialized, $col[0]->getParent());
  263. $this->assertSame($deserialized, $col[1]->getParent());
  264. }
  265. }
  266. public function testLifecycleCallbacks()
  267. {
  268. $object = new ObjectWithLifecycleCallbacks();
  269. $this->assertEquals($this->getContent('lifecycle_callbacks'), $this->serialize($object));
  270. $this->assertAttributeSame(null, 'name', $object);
  271. if ($this->hasDeserializer()) {
  272. $deserialized = $this->deserialize($this->getContent('lifecycle_callbacks'), get_class($object));
  273. $this->assertEquals($object, $deserialized);
  274. }
  275. }
  276. public function testFormErrors()
  277. {
  278. $errors = array(
  279. new FormError('This is the form error'),
  280. new FormError('Another error')
  281. );
  282. $this->assertEquals($this->getContent('form_errors'), $this->serialize($errors));
  283. }
  284. public function testNestedFormErrors()
  285. {
  286. $dispather = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
  287. $form = new Form('foo', $dispather);
  288. $form->addError(new FormError('This is the form error'));
  289. $child = new Form('bar', $dispather);
  290. $child->addError(new FormError('Error of the child form'));
  291. $form->add($child);
  292. $this->assertEquals($this->getContent('nested_form_errors'), $this->serialize($form));
  293. }
  294. public function testConstraintViolation()
  295. {
  296. $violation = new ConstraintViolation('Message of violation', array(), null, 'foo', null);
  297. $this->assertEquals($this->getContent('constraint_violation'), $this->serialize($violation));
  298. }
  299. public function testConstraintViolationList()
  300. {
  301. $violations = new ConstraintViolationList();
  302. $violations->add(new ConstraintViolation('Message of violation', array(), null, 'foo', null));
  303. $violations->add(new ConstraintViolation('Message of another violation', array(), null, 'bar', null));
  304. $this->assertEquals($this->getContent('constraint_violation_list'), $this->serialize($violations));
  305. }
  306. public function testDoctrineProxy()
  307. {
  308. if (!class_exists('Doctrine\ORM\Version')) {
  309. $this->markTestSkipped('Doctrine is not available.');
  310. }
  311. $object = new SimpleObjectProxy('foo', 'bar');
  312. $this->assertEquals($this->getContent('orm_proxy'), $this->serialize($object));
  313. }
  314. public function testInitializedDoctrineProxy()
  315. {
  316. if (!class_exists('Doctrine\ORM\Version')) {
  317. $this->markTestSkipped('Doctrine is not available.');
  318. }
  319. $object = new SimpleObjectProxy('foo', 'bar');
  320. $object->__load();
  321. $this->assertEquals($this->getContent('orm_proxy'), $this->serialize($object));
  322. }
  323. public function testCustomAccessor()
  324. {
  325. $post = new IndexedCommentsBlogPost();
  326. $this->assertEquals($this->getContent('custom_accessor'), $this->serialize($post));
  327. }
  328. public function testMixedAccessTypes()
  329. {
  330. $object = new GetSetObject();
  331. $this->assertEquals($this->getContent('mixed_access_types'), $this->serialize($object));
  332. if ($this->hasDeserializer()) {
  333. $object = $this->deserialize($this->getContent('mixed_access_types'), 'JMS\SerializerBundle\Tests\Fixtures\GetSetObject');
  334. $this->assertAttributeEquals(1, 'id', $object);
  335. $this->assertAttributeEquals('Johannes', 'name', $object);
  336. }
  337. }
  338. public function testAccessorOrder()
  339. {
  340. $this->assertEquals($this->getContent('accessor_order_child'), $this->serialize(new AccessorOrderChild()));
  341. $this->assertEquals($this->getContent('accessor_order_parent'), $this->serialize(new AccessorOrderParent()));
  342. }
  343. public function testGroups()
  344. {
  345. $serializer = $this->getSerializer();
  346. $groupsObject = new GroupsObject();
  347. $this->assertEquals($this->getContent('groups_all'), $serializer->serialize($groupsObject, $this->getFormat()));
  348. $serializer->setGroups(array("foo"));
  349. $this->assertEquals($this->getContent('groups_foo'), $serializer->serialize($groupsObject, $this->getFormat()));
  350. $serializer->setGroups(array("foo", "bar"));
  351. $this->assertEquals($this->getContent('groups_foobar'), $serializer->serialize($groupsObject, $this->getFormat()));
  352. $serializer->setGroups(null);
  353. $this->assertEquals($this->getContent('groups_all'), $serializer->serialize($groupsObject, $this->getFormat()));
  354. $serializer->setGroups(array());
  355. $this->assertEquals($this->getContent('groups_all'), $serializer->serialize($groupsObject, $this->getFormat()));
  356. }
  357. abstract protected function getContent($key);
  358. abstract protected function getFormat();
  359. protected function hasDeserializer()
  360. {
  361. return true;
  362. }
  363. protected function serialize($data)
  364. {
  365. return $this->getSerializer()->serialize($data, $this->getFormat());
  366. }
  367. protected function deserialize($content, $type)
  368. {
  369. return $this->getSerializer()->deserialize($content, $type, $this->getFormat());
  370. }
  371. protected function getSerializer()
  372. {
  373. $factory = new MetadataFactory(new AnnotationDriver(new AnnotationReader()));
  374. $namingStrategy = new SerializedNameAnnotationStrategy(new CamelCaseNamingStrategy());
  375. $objectConstructor = new UnserializeObjectConstructor();
  376. $customSerializationHandlers = $this->getSerializationHandlers();
  377. $customDeserializationHandlers = $this->getDeserializationHandlers();
  378. $serializationVisitors = array(
  379. 'json' => new JsonSerializationVisitor($namingStrategy, $customSerializationHandlers),
  380. 'xml' => new XmlSerializationVisitor($namingStrategy, $customSerializationHandlers),
  381. 'yml' => new YamlSerializationVisitor($namingStrategy, $customSerializationHandlers),
  382. );
  383. $deserializationVisitors = array(
  384. 'json' => new JsonDeserializationVisitor($namingStrategy, $customDeserializationHandlers, $objectConstructor),
  385. 'xml' => new XmlDeserializationVisitor($namingStrategy, $customDeserializationHandlers, $objectConstructor),
  386. );
  387. return new Serializer($factory, $serializationVisitors, $deserializationVisitors);
  388. }
  389. protected function getSerializationHandlers()
  390. {
  391. $translatorMock = $this->getMock('Symfony\\Component\\Translation\\TranslatorInterface');
  392. $translatorMock
  393. ->expects($this->any())
  394. ->method('trans')
  395. ->will($this->returnArgument(0));
  396. $factory = new MetadataFactory(new AnnotationDriver(new AnnotationReader()));
  397. $objectConstructor = new UnserializeObjectConstructor();
  398. $handlers = array(
  399. new ObjectBasedCustomHandler($objectConstructor, $factory),
  400. new DateTimeHandler(),
  401. new FormErrorHandler($translatorMock),
  402. new ConstraintViolationHandler(),
  403. new DoctrineProxyHandler(),
  404. );
  405. return $handlers;
  406. }
  407. protected function getDeserializationHandlers()
  408. {
  409. $factory = new MetadataFactory(new AnnotationDriver(new AnnotationReader()));
  410. $objectConstructor = new UnserializeObjectConstructor();
  411. $handlers = array(
  412. new ObjectBasedCustomHandler($objectConstructor, $factory),
  413. new DateTimeHandler(),
  414. new ArrayCollectionHandler(),
  415. new AuthorListDeserializationHandler(),
  416. );
  417. return $handlers;
  418. }
  419. private function getField($obj, $name)
  420. {
  421. $ref = new \ReflectionProperty($obj, $name);
  422. $ref->setAccessible(true);
  423. return $ref->getValue($obj);
  424. }
  425. private function setField($obj, $name, $value)
  426. {
  427. $ref = new \ReflectionProperty($obj, $name);
  428. $ref->setAccessible(true);
  429. $ref->setValue($obj, $value);
  430. }
  431. }
  432. class AuthorListDeserializationHandler implements DeserializationHandlerInterface
  433. {
  434. public function deserialize(VisitorInterface $visitor, $data, $type, &$visited)
  435. {
  436. if ('AuthorList' !== $type) {
  437. return;
  438. }
  439. $visited = true;
  440. $elements = $visitor->getNavigator()->accept($data, 'array<integer, JMS\SerializerBundle\Tests\Fixtures\Author>', $visitor);
  441. $list = new AuthorList();
  442. foreach ($elements as $author) {
  443. $list->add($author);
  444. }
  445. return $list;
  446. }
  447. }
  448. class Article implements SerializationHandlerInterface, DeserializationHandlerInterface
  449. {
  450. public $element;
  451. public $value;
  452. public function serialize(VisitorInterface $visitor, $data, $type, &$visited)
  453. {
  454. if (!$data instanceof Article) {
  455. return;
  456. }
  457. if ($visitor instanceof XmlSerializationVisitor) {
  458. $visited = true;
  459. if (null === $visitor->document) {
  460. $visitor->document = $visitor->createDocument(null, null, false);
  461. }
  462. $visitor->document->appendChild($visitor->document->createElement($this->element, $this->value));
  463. } elseif ($visitor instanceof JsonSerializationVisitor) {
  464. $visited = true;
  465. $visitor->setRoot(array($this->element => $this->value));
  466. } elseif ($visitor instanceof YamlSerializationVisitor) {
  467. $visited = true;
  468. $visitor->writer->writeln(Inline::dump($this->element).': '.Inline::dump($this->value));
  469. }
  470. }
  471. public function deserialize(VisitorInterface $visitor, $data, $type, &$visited)
  472. {
  473. if ('JMS\SerializerBundle\Tests\Serializer\Article' !== $type) {
  474. return;
  475. }
  476. if ($visitor instanceof XmlDeserializationVisitor) {
  477. $visited = true;
  478. $this->element = $data->getName();
  479. $this->value = (string)$data;
  480. } elseif ($visitor instanceof JsonDeserializationVisitor) {
  481. $visited = true;
  482. $this->element = key($data);
  483. $this->value = reset($data);
  484. }
  485. return $this;
  486. }
  487. }