1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the SexyField package. |
5
|
|
|
* |
6
|
|
|
* (c) Dion Snoeijen <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare (strict_types = 1); |
13
|
|
|
|
14
|
|
|
namespace Tardigrades\Entity; |
15
|
|
|
|
16
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
17
|
|
|
use Doctrine\Common\Collections\Collection; |
18
|
|
|
use Tardigrades\SectionField\ValueObject\Created; |
19
|
|
|
use Tardigrades\SectionField\ValueObject\FullyQualifiedClassName; |
20
|
|
|
use Tardigrades\SectionField\ValueObject\Id; |
21
|
|
|
use Tardigrades\SectionField\ValueObject\Name; |
22
|
|
|
use Tardigrades\SectionField\ValueObject\Type; |
23
|
|
|
use Tardigrades\SectionField\ValueObject\Updated; |
24
|
|
|
|
25
|
|
|
class FieldType implements FieldTypeInterface |
26
|
|
|
{ |
27
|
|
|
/** @var int */ |
28
|
|
|
protected $id; |
29
|
|
|
|
30
|
|
|
/** @var string * */ |
31
|
|
|
protected $type; |
32
|
|
|
|
33
|
|
|
/** @var string */ |
34
|
|
|
protected $fullyQualifiedClassName; |
35
|
|
|
|
36
|
|
|
/** @var ArrayCollection */ |
37
|
|
|
protected $fields; |
38
|
|
|
|
39
|
|
|
/** @var \DateTime */ |
40
|
|
|
protected $created; |
41
|
|
|
|
42
|
|
|
/** @var \DateTime */ |
43
|
|
|
protected $updated; |
44
|
|
|
|
45
|
|
|
public function __construct( |
46
|
|
|
Collection $fields = null |
47
|
|
|
) { |
48
|
|
|
$this->fields = is_null($fields) ? new ArrayCollection() : $fields; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function setId(int $id): FieldTypeInterface |
52
|
|
|
{ |
53
|
|
|
$this->id = $id; |
54
|
|
|
|
55
|
|
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function getId(): ?int |
59
|
|
|
{ |
60
|
|
|
return $this->id; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function getIdValueObject(): Id |
64
|
|
|
{ |
65
|
|
|
return Id::fromInt($this->id); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function getType(): Type |
69
|
|
|
{ |
70
|
|
|
return Type::fromString($this->type); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function setType(string $type): FieldTypeInterface |
74
|
|
|
{ |
75
|
|
|
$this->type = $type; |
76
|
|
|
|
77
|
|
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function addField(FieldInterface $field): FieldTypeInterface |
81
|
|
|
{ |
82
|
|
|
if ($this->fields->contains($field)) { |
83
|
|
|
return $this; |
84
|
|
|
} |
85
|
|
|
$field->setFieldType($this); |
86
|
|
|
$this->fields->add($field); |
87
|
|
|
|
88
|
|
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function removeField(FieldInterface $field): FieldTypeInterface |
92
|
|
|
{ |
93
|
|
|
if (!$this->fields->contains($field)) { |
94
|
|
|
return $this; |
95
|
|
|
} |
96
|
|
|
$this->fields->removeElement($field); |
97
|
|
|
|
98
|
|
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function getFields(): Collection |
102
|
|
|
{ |
103
|
|
|
return $this->fields; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function hasFields(): bool |
107
|
|
|
{ |
108
|
|
|
return !$this->fields->isEmpty(); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function setFullyQualifiedClassName(string $fullyQualifiedClassName): FieldTypeInterface |
112
|
|
|
{ |
113
|
|
|
$this->fullyQualifiedClassName = $fullyQualifiedClassName; |
114
|
|
|
|
115
|
|
|
return $this; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function getFullyQualifiedClassName(): FullyQualifiedClassName |
119
|
|
|
{ |
120
|
|
|
return FullyQualifiedClassName::fromString($this->fullyQualifiedClassName); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function getName(): Name |
124
|
|
|
{ |
125
|
|
|
return Name::fromString($this->type); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function setName(string $name): FieldTypeInterface |
129
|
|
|
{ |
130
|
|
|
$this->type = $name; |
131
|
|
|
|
132
|
|
|
return $this; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function setCreated(\DateTime $created): FieldTypeInterface |
136
|
|
|
{ |
137
|
|
|
$this->created = $created; |
138
|
|
|
|
139
|
|
|
return $this; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function getCreated(): \DateTime |
143
|
|
|
{ |
144
|
|
|
return $this->created; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function getCreatedValueObject(): Created |
148
|
|
|
{ |
149
|
|
|
return Created::fromDateTime($this->created); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function setUpdated(\DateTime $updated): FieldTypeInterface |
153
|
|
|
{ |
154
|
|
|
$this->updated = $updated; |
155
|
|
|
|
156
|
|
|
return $this; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function getUpdated(): \DateTime |
160
|
|
|
{ |
161
|
|
|
return $this->updated; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
public function getUpdatedValueObject(): Updated |
165
|
|
|
{ |
166
|
|
|
return Updated::fromDateTime($this->updated); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
public function onPrePersist(): void |
170
|
|
|
{ |
171
|
|
|
$this->created = new \DateTime("now"); |
172
|
|
|
$this->updated = new \DateTime("now"); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
public function onPreUpdate(): void |
176
|
|
|
{ |
177
|
|
|
$this->updated = new \DateTime("now"); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|