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