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