1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace VSV\GVQ_API\Company\Repositories\Entities; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
6
|
|
|
use Doctrine\Common\Collections\Collection; |
7
|
|
|
use Doctrine\ORM\Mapping as ORM; |
8
|
|
|
use Ramsey\Uuid\Uuid; |
9
|
|
|
use VSV\GVQ_API\Common\Repositories\Entities\Entity; |
10
|
|
|
use VSV\GVQ_API\Common\ValueObjects\NotEmptyString; |
11
|
|
|
use VSV\GVQ_API\Company\Models\Company; |
12
|
|
|
use VSV\GVQ_API\Company\Models\TranslatedAlias; |
13
|
|
|
use VSV\GVQ_API\Company\Models\TranslatedAliases; |
14
|
|
|
use VSV\GVQ_API\User\Repositories\Entities\UserEntity; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @ORM\Entity() |
18
|
|
|
* @ORM\Table(name="company") |
19
|
|
|
*/ |
20
|
|
|
class CompanyEntity extends Entity |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
* @ORM\Column(type="string", length=255, nullable=false) |
25
|
|
|
*/ |
26
|
|
|
private $name; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var Collection |
30
|
|
|
* @ORM\OneToMany(targetEntity="TranslatedAliasEntity", mappedBy="companyEntity", cascade={"all"}) |
31
|
|
|
* |
32
|
|
|
*/ |
33
|
|
|
private $translatedAliasEntities; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var UserEntity |
37
|
|
|
* @ORM\ManyToOne(targetEntity="VSV\GVQ_API\User\Repositories\Entities\UserEntity", fetch="EAGER") |
38
|
|
|
* @ORM\JoinColumn(name="user_id", referencedColumnName="id", nullable=false) |
39
|
|
|
*/ |
40
|
|
|
private $userEntity; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param string $id |
44
|
|
|
* @param string $name |
45
|
|
|
* @param Collection $translatedAliasEntities |
46
|
|
|
* @param UserEntity $user |
47
|
|
|
*/ |
48
|
|
|
public function __construct( |
49
|
|
|
string $id, |
50
|
|
|
string $name, |
51
|
|
|
Collection $translatedAliasEntities, |
52
|
|
|
UserEntity $user |
53
|
|
|
) { |
54
|
|
|
parent::__construct($id); |
55
|
|
|
|
56
|
|
|
$this->name = $name; |
57
|
|
|
$this->translatedAliasEntities = $translatedAliasEntities; |
58
|
|
|
$this->userEntity = $user; |
59
|
|
|
|
60
|
|
|
foreach ($translatedAliasEntities as $translatedAliasEntity) { |
61
|
|
|
$translatedAliasEntity->setCompanyEntity($this); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param Company $company |
67
|
|
|
* @return CompanyEntity |
68
|
|
|
*/ |
69
|
|
|
public static function fromCompany(Company $company): CompanyEntity |
70
|
|
|
{ |
71
|
|
|
/** @var TranslatedAliasEntity[] $translatedAliasEntities */ |
72
|
|
|
$translatedAliasEntities = array_map( |
73
|
|
|
function (TranslatedAlias $translatedAlias) { |
74
|
|
|
return TranslatedAliasEntity::fromTranslatedAlias($translatedAlias); |
75
|
|
|
}, |
76
|
|
|
$company->getTranslatedAliases()->toArray() |
77
|
|
|
); |
78
|
|
|
|
79
|
|
|
$companyEntity = new CompanyEntity( |
80
|
|
|
$company->getId()->toString(), |
81
|
|
|
$company->getName()->toNative(), |
82
|
|
|
new ArrayCollection($translatedAliasEntities), |
83
|
|
|
UserEntity::fromUser($company->getUser()) |
84
|
|
|
); |
85
|
|
|
|
86
|
|
|
return $companyEntity; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return Company |
91
|
|
|
*/ |
92
|
|
|
public function toCompany(): Company |
93
|
|
|
{ |
94
|
|
|
$translatedAliases = new TranslatedAliases( |
95
|
|
|
...array_map( |
96
|
|
|
function (TranslatedAliasEntity $translatedAliasEntity) { |
97
|
|
|
return $translatedAliasEntity->toTranslatedAlias(); |
98
|
|
|
}, |
99
|
|
|
$this->getTranslatedAliasEntities()->toArray() |
100
|
|
|
) |
101
|
|
|
); |
102
|
|
|
|
103
|
|
|
return new Company( |
104
|
|
|
Uuid::fromString($this->getId()), |
105
|
|
|
new NotEmptyString($this->getName()), |
106
|
|
|
$translatedAliases, |
107
|
|
|
$this->getUserEntity()->toUser() |
108
|
|
|
); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
|
|
public function getName(): string |
115
|
|
|
{ |
116
|
|
|
return $this->name; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return Collection |
121
|
|
|
*/ |
122
|
|
|
public function getTranslatedAliasEntities(): Collection |
123
|
|
|
{ |
124
|
|
|
return $this->translatedAliasEntities; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @return UserEntity |
129
|
|
|
*/ |
130
|
|
|
public function getUserEntity(): UserEntity |
131
|
|
|
{ |
132
|
|
|
return $this->userEntity; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param UserEntity $userEntity |
137
|
|
|
*/ |
138
|
|
|
public function setUserEntity(UserEntity $userEntity): void |
139
|
|
|
{ |
140
|
|
|
$this->userEntity = $userEntity; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|