ObjectWithLifecycleCallbacks.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. namespace JMS\Serializer\Tests\Fixtures;
  3. use JMS\Serializer\Annotation\Exclude;
  4. use JMS\Serializer\Annotation\PostDeserialize;
  5. use JMS\Serializer\Annotation\PostSerialize;
  6. use JMS\Serializer\Annotation\PreSerialize;
  7. use JMS\Serializer\Annotation\Type;
  8. class ObjectWithLifecycleCallbacks
  9. {
  10. /**
  11. * @Exclude
  12. */
  13. private $firstname;
  14. /**
  15. * @Exclude
  16. */
  17. private $lastname;
  18. /**
  19. * @Type("string")
  20. */
  21. private $name;
  22. public function __construct($firstname = 'Foo', $lastname = 'Bar')
  23. {
  24. $this->firstname = $firstname;
  25. $this->lastname = $lastname;
  26. }
  27. /**
  28. * @PreSerialize
  29. */
  30. private function prepareForSerialization()
  31. {
  32. $this->name = $this->firstname . ' ' . $this->lastname;
  33. }
  34. /**
  35. * @PostSerialize
  36. */
  37. private function cleanUpAfterSerialization()
  38. {
  39. $this->name = null;
  40. }
  41. /**
  42. * @PostDeserialize
  43. */
  44. private function afterDeserialization()
  45. {
  46. list($this->firstname, $this->lastname) = explode(' ', $this->name);
  47. $this->name = null;
  48. }
  49. }