ObjectWithLifecycleCallbacks.php 946 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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\PostDeserialize;
  6. use JMS\SerializerBundle\Annotation\Type;
  7. class ObjectWithLifecycleCallbacks
  8. {
  9. /**
  10. * @Exclude
  11. */
  12. private $firstname;
  13. /**
  14. * @Exclude
  15. */
  16. private $lastname;
  17. /**
  18. * @Type("string")
  19. */
  20. private $name;
  21. public function __construct($firstname = 'Foo', $lastname = 'Bar')
  22. {
  23. $this->firstname = $firstname;
  24. $this->lastname = $lastname;
  25. }
  26. /**
  27. * @PreSerialize
  28. */
  29. private function prepareForSerialization()
  30. {
  31. $this->name = $this->firstname.' '.$this->lastname;
  32. }
  33. /**
  34. * @PostDeserialize
  35. */
  36. private function afterDeserialization()
  37. {
  38. list($this->firstname, $this->lastname) = explode(' ', $this->name);
  39. }
  40. }