]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | ||
3 | namespace Wallabag\CoreBundle\Entity; | |
4 | ||
5 | use Doctrine\Common\Collections\ArrayCollection; | |
6 | use Doctrine\ORM\Mapping as ORM; | |
7 | use Symfony\Component\Validator\Constraints as Assert; | |
8 | use Hateoas\Configuration\Annotation as Hateoas; | |
9 | use JMS\Serializer\Annotation\XmlRoot; | |
10 | use Wallabag\CoreBundle\Helper\Tools; | |
11 | ||
12 | /** | |
13 | * Entry. | |
14 | * | |
15 | * @XmlRoot("entry") | |
16 | * @ORM\Entity(repositoryClass="Wallabag\CoreBundle\Repository\EntryRepository") | |
17 | * @ORM\Table | |
18 | * @ORM\HasLifecycleCallbacks() | |
19 | * @Hateoas\Relation("self", href = "expr('/api/entries/' ~ object.getId())") | |
20 | */ | |
21 | class Entry | |
22 | { | |
23 | /** @Serializer\XmlAttribute */ | |
24 | /** | |
25 | * @var int | |
26 | * | |
27 | * @ORM\Column(name="id", type="integer") | |
28 | * @ORM\Id | |
29 | * @ORM\GeneratedValue(strategy="AUTO") | |
30 | */ | |
31 | private $id; | |
32 | ||
33 | /** | |
34 | * @var string | |
35 | * | |
36 | * @ORM\Column(name="title", type="text", nullable=true) | |
37 | */ | |
38 | private $title; | |
39 | ||
40 | /** | |
41 | * @var string | |
42 | * | |
43 | * @Assert\NotBlank() | |
44 | * @ORM\Column(name="url", type="text", nullable=true) | |
45 | */ | |
46 | private $url; | |
47 | ||
48 | /** | |
49 | * @var bool | |
50 | * | |
51 | * @ORM\Column(name="is_archived", type="boolean") | |
52 | */ | |
53 | private $isArchived = false; | |
54 | ||
55 | /** | |
56 | * @var bool | |
57 | * | |
58 | * @ORM\Column(name="is_starred", type="boolean") | |
59 | */ | |
60 | private $isStarred = false; | |
61 | ||
62 | /** | |
63 | * @var string | |
64 | * | |
65 | * @ORM\Column(name="content", type="text", nullable=true) | |
66 | */ | |
67 | private $content; | |
68 | ||
69 | /** | |
70 | * @var date | |
71 | * | |
72 | * @ORM\Column(name="created_at", type="datetime") | |
73 | */ | |
74 | private $createdAt; | |
75 | ||
76 | /** | |
77 | * @var date | |
78 | * | |
79 | * @ORM\Column(name="updated_at", type="datetime") | |
80 | */ | |
81 | private $updatedAt; | |
82 | ||
83 | /** | |
84 | * @var string | |
85 | * | |
86 | * @ORM\Column(name="comments", type="text", nullable=true) | |
87 | */ | |
88 | private $comments; | |
89 | ||
90 | /** | |
91 | * @var string | |
92 | * | |
93 | * @ORM\Column(name="mimetype", type="text", nullable=true) | |
94 | */ | |
95 | private $mimetype; | |
96 | ||
97 | /** | |
98 | * @var int | |
99 | * | |
100 | * @ORM\Column(name="reading_time", type="integer", nullable=true) | |
101 | */ | |
102 | private $readingTime; | |
103 | ||
104 | /** | |
105 | * @var string | |
106 | * | |
107 | * @ORM\Column(name="domain_name", type="text", nullable=true) | |
108 | */ | |
109 | private $domainName; | |
110 | ||
111 | /** | |
112 | * @var bool | |
113 | * | |
114 | * @ORM\Column(name="is_public", type="boolean", nullable=true, options={"default" = false}) | |
115 | */ | |
116 | private $isPublic; | |
117 | ||
118 | /** | |
119 | * @ORM\ManyToOne(targetEntity="User", inversedBy="entries") | |
120 | */ | |
121 | private $user; | |
122 | ||
123 | /** | |
124 | * @ORM\ManyToMany(targetEntity="Tag", inversedBy="entries", cascade={"persist"}) | |
125 | * @ORM\JoinTable | |
126 | */ | |
127 | private $tags; | |
128 | ||
129 | /* | |
130 | * @param User $user | |
131 | */ | |
132 | public function __construct(User $user) | |
133 | { | |
134 | $this->user = $user; | |
135 | $this->tags = new ArrayCollection(); | |
136 | } | |
137 | ||
138 | /** | |
139 | * Get id. | |
140 | * | |
141 | * @return int | |
142 | */ | |
143 | public function getId() | |
144 | { | |
145 | return $this->id; | |
146 | } | |
147 | ||
148 | /** | |
149 | * Set title. | |
150 | * | |
151 | * @param string $title | |
152 | * | |
153 | * @return Entry | |
154 | */ | |
155 | public function setTitle($title) | |
156 | { | |
157 | $this->title = $title; | |
158 | ||
159 | return $this; | |
160 | } | |
161 | ||
162 | /** | |
163 | * Get title. | |
164 | * | |
165 | * @return string | |
166 | */ | |
167 | public function getTitle() | |
168 | { | |
169 | return $this->title; | |
170 | } | |
171 | ||
172 | /** | |
173 | * Set url. | |
174 | * | |
175 | * @param string $url | |
176 | * | |
177 | * @return Entry | |
178 | */ | |
179 | public function setUrl($url) | |
180 | { | |
181 | $this->url = $url; | |
182 | ||
183 | return $this; | |
184 | } | |
185 | ||
186 | /** | |
187 | * Get url. | |
188 | * | |
189 | * @return string | |
190 | */ | |
191 | public function getUrl() | |
192 | { | |
193 | return $this->url; | |
194 | } | |
195 | ||
196 | /** | |
197 | * Set isArchived. | |
198 | * | |
199 | * @param string $isArchived | |
200 | * | |
201 | * @return Entry | |
202 | */ | |
203 | public function setArchived($isArchived) | |
204 | { | |
205 | $this->isArchived = $isArchived; | |
206 | ||
207 | return $this; | |
208 | } | |
209 | ||
210 | /** | |
211 | * Get isArchived. | |
212 | * | |
213 | * @return string | |
214 | */ | |
215 | public function isArchived() | |
216 | { | |
217 | return $this->isArchived; | |
218 | } | |
219 | ||
220 | public function toggleArchive() | |
221 | { | |
222 | $this->isArchived = $this->isArchived() ^ 1; | |
223 | ||
224 | return $this; | |
225 | } | |
226 | ||
227 | /** | |
228 | * Set isStarred. | |
229 | * | |
230 | * @param string $isStarred | |
231 | * | |
232 | * @return Entry | |
233 | */ | |
234 | public function setStarred($isStarred) | |
235 | { | |
236 | $this->isStarred = $isStarred; | |
237 | ||
238 | return $this; | |
239 | } | |
240 | ||
241 | /** | |
242 | * Get isStarred. | |
243 | * | |
244 | * @return string | |
245 | */ | |
246 | public function isStarred() | |
247 | { | |
248 | return $this->isStarred; | |
249 | } | |
250 | ||
251 | public function toggleStar() | |
252 | { | |
253 | $this->isStarred = $this->isStarred() ^ 1; | |
254 | ||
255 | return $this; | |
256 | } | |
257 | ||
258 | /** | |
259 | * Set content. | |
260 | * | |
261 | * @param string $content | |
262 | * | |
263 | * @return Entry | |
264 | */ | |
265 | public function setContent($content) | |
266 | { | |
267 | $this->content = $content; | |
268 | $this->readingTime = Tools::getReadingTime($content); | |
269 | $this->domainName = parse_url($this->url, PHP_URL_HOST); | |
270 | ||
271 | return $this; | |
272 | } | |
273 | ||
274 | /** | |
275 | * Get content. | |
276 | * | |
277 | * @return string | |
278 | */ | |
279 | public function getContent() | |
280 | { | |
281 | return $this->content; | |
282 | } | |
283 | ||
284 | /** | |
285 | * @return User | |
286 | */ | |
287 | public function getUser() | |
288 | { | |
289 | return $this->user; | |
290 | } | |
291 | ||
292 | /** | |
293 | * @return string | |
294 | */ | |
295 | public function getCreatedAt() | |
296 | { | |
297 | return $this->createdAt; | |
298 | } | |
299 | ||
300 | /** | |
301 | * @return string | |
302 | */ | |
303 | public function getUpdatedAt() | |
304 | { | |
305 | return $this->updatedAt; | |
306 | } | |
307 | ||
308 | /** | |
309 | * @ORM\PrePersist | |
310 | * @ORM\PreUpdate | |
311 | */ | |
312 | public function timestamps() | |
313 | { | |
314 | if (is_null($this->createdAt)) { | |
315 | $this->createdAt = new \DateTime(); | |
316 | } | |
317 | ||
318 | $this->updatedAt = new \DateTime(); | |
319 | } | |
320 | ||
321 | /** | |
322 | * @return string | |
323 | */ | |
324 | public function getComments() | |
325 | { | |
326 | return $this->comments; | |
327 | } | |
328 | ||
329 | /** | |
330 | * @param string $comments | |
331 | */ | |
332 | public function setComments($comments) | |
333 | { | |
334 | $this->comments = $comments; | |
335 | } | |
336 | ||
337 | /** | |
338 | * @return string | |
339 | */ | |
340 | public function getMimetype() | |
341 | { | |
342 | return $this->mimetype; | |
343 | } | |
344 | ||
345 | /** | |
346 | * @param string $mimetype | |
347 | */ | |
348 | public function setMimetype($mimetype) | |
349 | { | |
350 | $this->mimetype = $mimetype; | |
351 | } | |
352 | ||
353 | /** | |
354 | * @return int | |
355 | */ | |
356 | public function getReadingTime() | |
357 | { | |
358 | return $this->readingTime; | |
359 | } | |
360 | ||
361 | /** | |
362 | * @param int $readingTime | |
363 | */ | |
364 | public function setReadingTime($readingTime) | |
365 | { | |
366 | $this->readingTime = $readingTime; | |
367 | } | |
368 | ||
369 | /** | |
370 | * @return string | |
371 | */ | |
372 | public function getDomainName() | |
373 | { | |
374 | return $this->domainName; | |
375 | } | |
376 | ||
377 | /** | |
378 | * @param string $domainName | |
379 | */ | |
380 | public function setDomainName($domainName) | |
381 | { | |
382 | $this->domainName = $domainName; | |
383 | } | |
384 | ||
385 | /** | |
386 | * @return bool | |
387 | */ | |
388 | public function isPublic() | |
389 | { | |
390 | return $this->isPublic; | |
391 | } | |
392 | ||
393 | /** | |
394 | * @param bool $isPublic | |
395 | */ | |
396 | public function setIsPublic($isPublic) | |
397 | { | |
398 | $this->isPublic = $isPublic; | |
399 | } | |
400 | ||
401 | /** | |
402 | * @return ArrayCollection<Tag> | |
403 | */ | |
404 | public function getTags() | |
405 | { | |
406 | return $this->tags; | |
407 | } | |
408 | ||
409 | /** | |
410 | * @param Tag $tag | |
411 | */ | |
412 | public function addTag(Tag $tag) | |
413 | { | |
414 | $this->tags[] = $tag; | |
415 | $tag->addEntry($this); | |
416 | } | |
417 | ||
418 | public function removeTag(Tag $tag) | |
419 | { | |
420 | $this->tags->removeElement($tag); | |
421 | } | |
422 | } |