1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Moo\FlashCardBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) Mohamed Alsharaf <[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
|
|
|
namespace Moo\FlashCardBundle\Entity; |
13
|
|
|
|
14
|
|
|
use Doctrine\ORM\Mapping as ORM; |
15
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
16
|
|
|
use Symfony\Component\Validator\Constraints as Assert; |
17
|
|
|
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Category is the entity class that represents a record from the database. |
21
|
|
|
* |
22
|
|
|
* @author Mohamed Alsharaf <[email protected]> |
23
|
|
|
* @ORM\Table(name="card_category") |
24
|
|
|
* @ORM\Entity(repositoryClass="Moo\FlashCardBundle\Repository\Category") |
25
|
|
|
* @ORM\HasLifecycleCallbacks() |
26
|
|
|
* @UniqueEntity( |
27
|
|
|
* fields={"title"}, |
28
|
|
|
* message="There is an existing category with the same title." |
29
|
|
|
* ) |
30
|
|
|
*/ |
31
|
|
|
class Category |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* @var int |
35
|
|
|
* |
36
|
|
|
* @ORM\Column(name="id", type="integer", nullable=false) |
37
|
|
|
* @ORM\Id |
38
|
|
|
* @ORM\GeneratedValue(strategy="IDENTITY") |
39
|
|
|
*/ |
40
|
|
|
private $id; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var string |
44
|
|
|
* |
45
|
|
|
* @ORM\Column(name="title", type="string", length=255, nullable=false,unique=true) |
46
|
|
|
* @Assert\NotBlank() |
47
|
|
|
* @Assert\Length( |
48
|
|
|
* min = "5", |
49
|
|
|
* max = "255", |
50
|
|
|
* minMessage = "Category title must be at least {{ limit }} characters length", |
51
|
|
|
* maxMessage = "Category title cannot be longer than {{ limit }} characters length" |
52
|
|
|
* ) |
53
|
|
|
*/ |
54
|
|
|
private $title; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var string |
58
|
|
|
* |
59
|
|
|
* @ORM\Column(name="description", type="text", nullable=true) |
60
|
|
|
*/ |
61
|
|
|
private $description; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var \DateTime |
65
|
|
|
* |
66
|
|
|
* @ORM\Column(name="created", type="datetime", nullable=false) |
67
|
|
|
*/ |
68
|
|
|
private $created; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @var \DateTime |
72
|
|
|
* |
73
|
|
|
* @ORM\Column(name="updated", type="datetime", nullable=false) |
74
|
|
|
*/ |
75
|
|
|
private $updated; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @var int |
79
|
|
|
* |
80
|
|
|
* @ORM\Column(name="active", type="boolean", nullable=true) |
81
|
|
|
*/ |
82
|
|
|
private $active = false; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @var Category |
86
|
|
|
* |
87
|
|
|
* @ORM\ManyToOne(targetEntity="Category", inversedBy="children", fetch="EAGER") |
88
|
|
|
* @ORM\JoinColumns({ |
89
|
|
|
* @ORM\JoinColumn(name="parent", referencedColumnName="id") |
90
|
|
|
* }) |
91
|
|
|
*/ |
92
|
|
|
private $parent; |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @ORM\OneToMany(targetEntity="Category", mappedBy="parent", fetch="EAGER") |
96
|
|
|
*/ |
97
|
|
|
private $children; |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @ORM\OneToMany(targetEntity="Card", mappedBy="category", fetch="EAGER") |
101
|
|
|
*/ |
102
|
|
|
private $cards; |
103
|
|
|
|
104
|
11 |
|
public function __construct() |
105
|
|
|
{ |
106
|
11 |
|
$this->cards = new ArrayCollection(); |
107
|
11 |
|
$this->children = new ArrayCollection(); |
108
|
11 |
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Get id |
112
|
|
|
* |
113
|
|
|
* @return int |
114
|
|
|
*/ |
115
|
8 |
|
public function getId() |
116
|
|
|
{ |
117
|
8 |
|
return $this->id; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Set title |
122
|
|
|
* |
123
|
|
|
* @param string $title |
124
|
|
|
* |
125
|
|
|
* @return Category |
126
|
|
|
*/ |
127
|
11 |
|
public function setTitle($title) |
128
|
|
|
{ |
129
|
11 |
|
$this->title = $title; |
130
|
|
|
|
131
|
11 |
|
return $this; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Get title |
136
|
|
|
* |
137
|
|
|
* @return string |
138
|
|
|
*/ |
139
|
4 |
|
public function getTitle() |
140
|
|
|
{ |
141
|
4 |
|
return $this->title; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Set description |
146
|
|
|
* |
147
|
|
|
* @param string $description |
148
|
|
|
* |
149
|
|
|
* @return Category |
150
|
|
|
*/ |
151
|
9 |
|
public function setDescription($description) |
152
|
|
|
{ |
153
|
9 |
|
$this->description = $description; |
154
|
|
|
|
155
|
9 |
|
return $this; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Get description |
160
|
|
|
* |
161
|
|
|
* @return string |
162
|
|
|
*/ |
163
|
1 |
|
public function getDescription() |
164
|
|
|
{ |
165
|
1 |
|
return $this->description; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Set created |
170
|
|
|
* |
171
|
|
|
* @param \DateTime $created |
172
|
|
|
* |
173
|
|
|
* @return Category |
174
|
|
|
*/ |
175
|
9 |
View Code Duplication |
public function setCreated($created = null) |
|
|
|
|
176
|
|
|
{ |
177
|
9 |
|
if ($created === null) { |
178
|
8 |
|
$created = new \DateTime(); |
179
|
8 |
|
$this->setUpdated($created); |
180
|
|
|
} |
181
|
9 |
|
$this->created = $created; |
182
|
|
|
|
183
|
9 |
|
return $this; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Get created |
188
|
|
|
* |
189
|
|
|
* @return \DateTime |
190
|
|
|
*/ |
191
|
2 |
|
public function getCreated() |
192
|
|
|
{ |
193
|
2 |
|
return $this->created; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Set updated |
198
|
|
|
* |
199
|
|
|
* @param \DateTime $updated |
200
|
|
|
* |
201
|
|
|
* @return Category |
202
|
|
|
*/ |
203
|
8 |
View Code Duplication |
public function setUpdated($updated = null) |
|
|
|
|
204
|
|
|
{ |
205
|
8 |
|
if ($updated === null) { |
206
|
|
|
$updated = new \DateTime(); |
207
|
|
|
} |
208
|
8 |
|
$this->updated = $updated; |
209
|
|
|
|
210
|
8 |
|
return $this; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Get updated |
215
|
|
|
* |
216
|
|
|
* @return \DateTime |
217
|
|
|
*/ |
218
|
1 |
|
public function getUpdated() |
219
|
|
|
{ |
220
|
1 |
|
return $this->updated; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Set isActive |
225
|
|
|
* |
226
|
|
|
* @param int $isActive |
227
|
|
|
* |
228
|
|
|
* @return Category |
229
|
|
|
*/ |
230
|
9 |
|
public function setActive($isActive) |
231
|
|
|
{ |
232
|
9 |
|
$this->active = $isActive; |
233
|
|
|
|
234
|
9 |
|
return $this; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Get isActive |
239
|
|
|
* |
240
|
|
|
* @return int |
241
|
|
|
*/ |
242
|
4 |
|
public function isActive() |
243
|
|
|
{ |
244
|
4 |
|
return $this->active; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Set parent |
249
|
|
|
* |
250
|
|
|
* @param Category $parent |
251
|
|
|
* |
252
|
|
|
* @return Category |
253
|
|
|
*/ |
254
|
6 |
|
public function setParent(Category $parent = null) |
255
|
|
|
{ |
256
|
6 |
|
$this->parent = $parent; |
257
|
|
|
|
258
|
6 |
|
return $this; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Get parent |
263
|
|
|
* |
264
|
|
|
* @return Category |
265
|
|
|
*/ |
266
|
1 |
|
public function getParent() |
267
|
|
|
{ |
268
|
1 |
|
return $this->parent; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* @return ArrayCollection |
273
|
|
|
*/ |
274
|
1 |
|
public function getCards() |
275
|
|
|
{ |
276
|
1 |
|
return $this->cards; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* @ORM\PrePersist() |
281
|
|
|
*/ |
282
|
|
|
public function prePersist() |
283
|
|
|
{ |
284
|
|
|
$this->setCreated(); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* @ORM\PreUpdate() |
289
|
|
|
*/ |
290
|
|
|
public function preUpdate() |
291
|
|
|
{ |
292
|
|
|
$this->setUpdated(); |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
public function __toString() |
296
|
|
|
{ |
297
|
|
|
return $this->getTitle() ?: 'New Category'; |
298
|
|
|
} |
299
|
|
|
} |
300
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.