ObjectWithLifecycleCallbacks.php 1.4 KB

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