1<?php
2
3declare(strict_types=1);
4
5namespace JMS\Serializer\Tests\Fixtures;
6
7use Doctrine\Common\Collections\ArrayCollection;
8use JMS\Serializer\Annotation\PostDeserialize;
9use JMS\Serializer\Annotation\Type;
10
11/** No annotation */
12class CircularReferenceParent
13{
14    /** @Type("array<JMS\Serializer\Tests\Fixtures\CircularReferenceChild>") */
15    protected $collection = [];
16
17    /** @Type("ArrayCollection<JMS\Serializer\Tests\Fixtures\CircularReferenceChild>") */
18    private $anotherCollection;
19
20    public function __construct()
21    {
22        $this->collection[] = new CircularReferenceChild('child1', $this);
23        $this->collection[] = new CircularReferenceChild('child2', $this);
24
25        $this->anotherCollection = new ArrayCollection();
26        $this->anotherCollection->add(new CircularReferenceChild('child1', $this));
27        $this->anotherCollection->add(new CircularReferenceChild('child2', $this));
28    }
29
30    /** @PostDeserialize */
31    private function afterDeserialization()
32    {
33        if (!$this->collection) {
34            $this->collection = [];
35        }
36        foreach ($this->collection as $v) {
37            $v->setParent($this);
38        }
39
40        if (!$this->anotherCollection) {
41            $this->anotherCollection = new ArrayCollection();
42        }
43        foreach ($this->anotherCollection as $v) {
44            $v->setParent($this);
45        }
46    }
47}
48