1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Gbere\SimpleAuth\Entity; |
6
|
|
|
|
7
|
|
|
use DateTime; |
8
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
9
|
|
|
use Doctrine\Common\Collections\Collection; |
10
|
|
|
use Doctrine\ORM\Mapping as ORM; |
11
|
|
|
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @ORM\Table() |
15
|
|
|
* @ORM\HasLifecycleCallbacks() |
16
|
|
|
* @ORM\Entity(repositoryClass="Gbere\SimpleAuth\Repository\RoleRepository") |
17
|
|
|
* @UniqueEntity("name" ) |
18
|
|
|
*/ |
19
|
|
|
class Role |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var int |
23
|
|
|
* @ORM\Id() |
24
|
|
|
* @ORM\GeneratedValue() |
25
|
|
|
* @ORM\Column(type="integer") |
26
|
|
|
*/ |
27
|
|
|
private $id; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var Collection |
31
|
|
|
* @ORM\ManyToMany(targetEntity="Gbere\SimpleAuth\Entity\User", mappedBy="roles") |
32
|
|
|
*/ |
33
|
|
|
private $users; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
* @ORM\Column(type="string", length=50) |
38
|
|
|
*/ |
39
|
|
|
private $name; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var string|null |
43
|
|
|
* @ORM\Column(type="string", length=100, nullable=true) |
44
|
|
|
*/ |
45
|
|
|
private $description; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var DateTime |
49
|
|
|
* @ORM\Column(type="datetime") |
50
|
|
|
*/ |
51
|
|
|
private $createdAt; |
52
|
|
|
|
53
|
|
|
public function __construct() |
54
|
|
|
{ |
55
|
|
|
$this->users = new ArrayCollection(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function __toString(): string |
59
|
|
|
{ |
60
|
|
|
return $this->getName() ?? ''; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function getId(): ?int |
64
|
|
|
{ |
65
|
|
|
return $this->id; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function getName(): ?string |
69
|
|
|
{ |
70
|
|
|
return $this->name; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function setName(string $name): self |
74
|
|
|
{ |
75
|
|
|
$this->name = $name; |
76
|
|
|
|
77
|
|
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function getDescription(): ?string |
81
|
|
|
{ |
82
|
|
|
return $this->description; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function setDescription(?string $description): self |
86
|
|
|
{ |
87
|
|
|
$this->description = $description; |
88
|
|
|
|
89
|
|
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return Collection|User[] |
94
|
|
|
*/ |
95
|
|
|
public function getUsers(): Collection |
96
|
|
|
{ |
97
|
|
|
return $this->users; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function addUser(User $user): self |
101
|
|
|
{ |
102
|
|
|
if (!$this->users->contains($user)) { |
103
|
|
|
$this->users[] = $user; |
104
|
|
|
$user->addRoleEntity($this); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function removeUser(User $user): self |
111
|
|
|
{ |
112
|
|
|
if ($this->users->contains($user)) { |
113
|
|
|
$this->users->removeElement($user); |
114
|
|
|
$user->removeRoleEntity($this); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function getCreatedAt(): ?DateTime |
121
|
|
|
{ |
122
|
|
|
return $this->createdAt; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function setCreatedAt(DateTime $createdAt): self |
126
|
|
|
{ |
127
|
|
|
$this->createdAt = $createdAt; |
128
|
|
|
|
129
|
|
|
return $this; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @ORM\PrePersist |
134
|
|
|
*/ |
135
|
|
|
public function setCreatedAtValue(): void |
136
|
|
|
{ |
137
|
|
|
$this->createdAt = new DateTime(); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|