|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sinergi\Users\Group; |
|
4
|
|
|
|
|
5
|
|
|
use DateTime; |
|
6
|
|
|
use Sinergi\Users\User\UserEntityInterface; |
|
7
|
|
|
use Sinergi\Users\User\UserRepositoryInterface; |
|
8
|
|
|
|
|
9
|
|
|
trait GroupEntityTrait |
|
10
|
|
|
{ |
|
11
|
|
|
protected $id; |
|
12
|
|
|
/** @var UserEntityInterface[] */ |
|
13
|
|
|
protected $users; |
|
14
|
|
|
protected $creationDatetime; |
|
15
|
|
|
protected $modificationDatetime; |
|
16
|
|
|
/** @var UserRepositoryInterface */ |
|
17
|
|
|
protected $userRepository; |
|
18
|
|
|
|
|
19
|
|
|
public function __construct(UserRepositoryInterface $userRepository = null) |
|
|
|
|
|
|
20
|
|
|
{ |
|
21
|
|
|
$this->setCreationDatetime(new DateTime()); |
|
22
|
|
|
$this->setModificationDatetime(new DateTime()); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** @return int */ |
|
26
|
|
|
public function getId() |
|
27
|
|
|
{ |
|
28
|
|
|
return $this->id; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function setId(int $id): GroupEntityInterface |
|
32
|
|
|
{ |
|
33
|
|
|
$this->id = $id; |
|
34
|
|
|
return $this; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** @return UserEntityInterface[] */ |
|
38
|
|
|
public function getUsers() |
|
39
|
|
|
{ |
|
40
|
|
|
if (is_array($this->users)) { |
|
41
|
|
|
return $this->users; |
|
42
|
|
|
} elseif (!$this->userRepository) { |
|
43
|
|
|
throw new \Exception('Cannot fetch users without user repository'); |
|
44
|
|
|
} |
|
45
|
|
|
return $this->users = $this->userRepository->findByGroupId($this->getId()); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function getCreationDatetime(): DateTime |
|
49
|
|
|
{ |
|
50
|
|
|
return $this->creationDatetime; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function setCreationDatetime(DateTime $creationDatetime): GroupEntityInterface |
|
54
|
|
|
{ |
|
55
|
|
|
$this->creationDatetime = $creationDatetime; |
|
56
|
|
|
return $this; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function getModificationDatetime(): DateTime |
|
60
|
|
|
{ |
|
61
|
|
|
return $this->modificationDatetime; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function setModificationDatetime(DateTime $modificationDatetime): GroupEntityInterface |
|
65
|
|
|
{ |
|
66
|
|
|
$this->modificationDatetime = $modificationDatetime; |
|
67
|
|
|
return $this; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function setUserRepository(UserRepositoryInterface $userRepository): GroupEntityInterface |
|
71
|
|
|
{ |
|
72
|
|
|
$this->userRepository = $userRepository; |
|
73
|
|
|
return $this; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function toArray(): array |
|
77
|
|
|
{ |
|
78
|
|
|
return [ |
|
79
|
|
|
'id' => $this->getId(), |
|
80
|
|
|
'creationDatetime' => $this->getCreationDatetime()->format('Y-m-d H:i:s'), |
|
81
|
|
|
'modificationDatetime' => $this->getModificationDatetime()->format('Y-m-d H:i:s'), |
|
82
|
|
|
]; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function jsonSerialize() |
|
86
|
|
|
{ |
|
87
|
|
|
return $this->toArray(); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.