Commit | Line | Data |
---|---|---|
dec6d031 JB |
1 | <?php |
2 | namespace App\Entity; | |
3 | ||
4 | use Doctrine\Common\Collections\ArrayCollection; | |
5 | use Doctrine\ORM\Mapping as ORM; | |
c3bce7b6 JB |
6 | use JMS\Serializer\Annotation\ExclusionPolicy; |
7 | use JMS\Serializer\Annotation\Expose; | |
dec6d031 JB |
8 | |
9 | /** | |
10 | * @ORM\Entity() | |
11 | * @ORM\Table(name="Person", indexes={@ORM\Index(name="authentification_idx", columns={"email", "password"}), | |
c3bce7b6 JB |
12 | * @ORM\Index(name="search_idx", columns={"firstname", "lastname", "email"})}); |
13 | * The following annotations tells the serializer to skip all properties which | |
14 | * have not marked with @Expose. | |
15 | * | |
16 | * @ExclusionPolicy("all") | |
dec6d031 JB |
17 | */ |
18 | class Person | |
19 | { | |
20 | /** | |
21 | * @ORM\Id | |
22 | * @ORM\Column(type="bigint") | |
23 | * @ORM\GeneratedValue(strategy="AUTO") | |
24 | */ | |
25 | protected $id; | |
26 | ||
27 | /** | |
28 | * @ORM\Column(type="string") | |
29 | */ | |
30 | protected $firstname; | |
31 | ||
32 | /** | |
33 | * @ORM\Column(type="string") | |
34 | */ | |
35 | protected $lastname; | |
36 | ||
37 | /** | |
38 | * @ORM\Column(type="string", unique=true) | |
39 | */ | |
40 | protected $email; | |
41 | ||
42 | /** | |
43 | * @ORM\Column(type="string") | |
44 | */ | |
45 | protected $password; | |
46 | ||
47 | /** | |
48 | * @ORM\Column(type="boolean", options={"default":false}) | |
49 | */ | |
50 | protected $online; | |
51 | ||
52 | /** | |
53 | * One person have many friends | |
54 | * @ORM\OneToMany(targetEntity="App\Entity\Friendship", mappedBy="person", cascade={"all"}) | |
55 | */ | |
56 | protected $friends; | |
57 | ||
58 | /** | |
59 | * One person have many friends | |
60 | * @ORM\OneToMany(targetEntity="App\Entity\Friendship", mappedBy="friend", cascade={"all"}) | |
61 | */ | |
62 | protected $friends_with_me; | |
63 | ||
64 | /** | |
65 | * [__construct description] | |
66 | * NOTE: Should we forbid reverse pairs (person_id=123 and friend_id=456 | |
67 | * can be viewed as the same as person_id=456 and friend_id=123) | |
68 | * if we view friendship as a bidirectionnal association? | |
69 | */ | |
70 | public function __construct() | |
71 | { | |
72 | $this->friends = new ArrayCollection(); | |
73 | $this->friends_with_me = new ArrayCollection(); | |
74 | } | |
75 | ||
76 | public function getId() | |
77 | { | |
78 | return $this->id; | |
79 | } | |
80 | ||
81 | public function getFirstName() | |
82 | { | |
83 | return $this->firstname; | |
84 | } | |
85 | ||
86 | public function getLastName() | |
87 | { | |
88 | return $this->lastname; | |
89 | } | |
90 | ||
91 | public function getEmail() | |
92 | { | |
93 | return $this->email; | |
94 | } | |
95 | ||
96 | public function getPassword() | |
97 | { | |
98 | return $this->password; | |
99 | } | |
100 | ||
101 | public function getOnline() | |
102 | { | |
103 | return $this->online; | |
104 | } | |
105 | ||
106 | public function getFriends() | |
107 | { | |
108 | return $this->friends; | |
109 | } | |
110 | ||
111 | public function getFriendsWithMe() | |
112 | { | |
113 | return $this->friends_with_me; | |
114 | } | |
115 | ||
116 | public function setId($id) | |
117 | { | |
118 | $this->id = $id; | |
119 | return $this; | |
120 | } | |
121 | ||
122 | public function setFirstName($firstname) | |
123 | { | |
124 | $this->firstname = $firstname; | |
125 | return $this; | |
126 | } | |
127 | ||
128 | public function setLastName($lastname) | |
129 | { | |
130 | $this->lastname = $lastname; | |
131 | return $this; | |
132 | } | |
133 | ||
134 | public function setEmail($email) | |
135 | { | |
136 | $this->email = $email; | |
137 | return $this; | |
138 | } | |
139 | ||
140 | public function setPassword($password) | |
141 | { | |
142 | $this->password = $password; | |
143 | return $this; | |
144 | } | |
145 | ||
146 | public function setOnline($online) | |
147 | { | |
148 | $this->online = $online; | |
149 | return $this; | |
150 | } | |
151 | ||
152 | public function addFriendship(Friendship $friendship) | |
153 | { | |
154 | $this->friends->add($friendship); | |
155 | $friendship->friend->addFriendshipWithMe($friendship); | |
156 | } | |
157 | ||
158 | public function addFriendshipWithMe(Friendship $friendship) | |
159 | { | |
160 | $this->friends_with_me->add($friendship); | |
161 | } | |
162 | ||
d68c2073 | 163 | public function addFriend(Person $friend) |
dec6d031 JB |
164 | { |
165 | $fs = new Friendship(); | |
166 | $fs->setPerson($this); | |
167 | $fs->setFriend($friend); | |
168 | // set defaults validation to true for now | |
169 | $fs->setIsValid(true); | |
170 | ||
171 | $this->addFriendship($fs); | |
172 | } | |
173 | } |