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