-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathClient.php
110 lines (97 loc) · 2.53 KB
/
Client.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
/**
* @copyright 2016-present Hostnet B.V.
*/
declare(strict_types=1);
namespace Hostnet\Component\EntityMutation\Functional\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Hostnet\Component\EntityMutation\Mutation;
use Hostnet\Component\EntityMutation\MutationAwareInterface;
/**
* @ORM\Entity()
* @Mutation(strategy="current")
*/
class Client implements MutationAwareInterface
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*
* @var int
*/
private $id;
/**
* @ORM\Embedded(class="ContactInfo")
*
* @var ContactInfo
*/
private $contact_info;
/**
* The history of this object.
*
* @ORM\OneToMany(targetEntity="ClientMutation", mappedBy="client")
* @ORM\OrderBy(value={"id"="DESC"})
* @var Collection
*/
private $mutations;
/**
* @param ContactInfo $contact_info
*/
public function __construct(ContactInfo $contact_info)
{
$this->contact_info = $contact_info;
$this->mutations = new ArrayCollection();
}
public function getContactInfo(): ContactInfo
{
return $this->contact_info;
}
/**
* @return mixed
*/
public function getId()
{
return $this->id;
}
/**
* @param ContactInfo $contact_info
* @return $this
*/
public function setContactInfo(ContactInfo $contact_info)
{
$this->contact_info = $contact_info;
return $this;
}
/**
* @param ClientMutation $mutation
*/
public function addMutation($mutation): void
{
// $this->mutations is sorted by id descending, so we should add new
// items at the start of the Collection. Doctrine collections don't
// allow this right now, so we add it to the end. This is fixed in
// getMutations.
$this->mutations->add($mutation);
}
/**
* @return ClientMutation[]
*/
public function getMutations(): array
{
$mutations = $this->mutations->toArray();
usort($mutations, function (ClientMutation $ma, ClientMutation $mb) {
if ($ma->getId() === $mb->getId()) {
return 0;
}
return ($ma->getId() > $mb->getId()) ? -1 : 1;
});
return $mutations;
}
public function getPreviousMutation(): ClientMutation
{
throw new \BadMethodCallException(__METHOD__ . ' is not implemented.');
}
}