1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* AnimeDb package. |
4
|
|
|
* |
5
|
|
|
* @author Peter Gribanov <[email protected]> |
6
|
|
|
* @copyright Copyright (c) 2011, Peter Gribanov |
7
|
|
|
* @license http://opensource.org/licenses/GPL-3.0 GPL v3 |
8
|
|
|
*/ |
9
|
|
|
namespace AnimeDb\Bundle\AppBundle\Entity\Field; |
10
|
|
|
|
11
|
|
|
use AnimeDb\Bundle\AppBundle\Service\Downloader\Entity\BaseEntity; |
12
|
|
|
use AnimeDb\Bundle\AppBundle\Service\Downloader\Entity\ImageInterface; |
13
|
|
|
use Symfony\Component\Validator\Constraints as Assert; |
14
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
15
|
|
|
|
16
|
|
|
class Image extends BaseEntity implements ImageInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Image from URL. |
20
|
|
|
* |
21
|
|
|
* @Assert\Url() |
22
|
|
|
* |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $remote = ''; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Local image. |
29
|
|
|
* |
30
|
|
|
* @Assert\Image( |
31
|
|
|
* maxSize = "2048k", |
32
|
|
|
* minWidth = 200, |
33
|
|
|
* minHeight = 200, |
34
|
|
|
* mimeTypes = {"image/bmp","image/gif","image/jpeg","image/png"}, |
35
|
|
|
* mimeTypesMessage = "Please upload a valid image file" |
36
|
|
|
* ) |
37
|
|
|
* |
38
|
|
|
* @var UploadedFile|null |
39
|
|
|
*/ |
40
|
|
|
protected $local; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param string $remote |
44
|
|
|
* |
45
|
|
|
* @return Image |
46
|
|
|
*/ |
47
|
4 |
|
public function setRemote($remote) |
48
|
|
|
{ |
49
|
4 |
|
$this->remote = $remote; |
50
|
|
|
|
51
|
4 |
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
3 |
|
public function getRemote() |
58
|
|
|
{ |
59
|
3 |
|
return $this->remote; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param UploadedFile $local |
64
|
|
|
* |
65
|
|
|
* @return Image |
66
|
|
|
*/ |
67
|
3 |
|
public function setLocal(UploadedFile $local) |
68
|
|
|
{ |
69
|
3 |
|
$this->local = $local; |
70
|
|
|
|
71
|
3 |
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return UploadedFile |
76
|
|
|
*/ |
77
|
2 |
|
public function getLocal() |
78
|
|
|
{ |
79
|
2 |
|
return $this->local; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param string $filename |
84
|
|
|
*/ |
85
|
2 |
|
public function setFilename($filename) |
86
|
|
|
{ |
87
|
|
|
// it is a filename prefix, not a suffix for the download path |
88
|
2 |
|
if (strpos($filename, 'tmp/') !== 0) { |
89
|
1 |
|
$filename = 'tmp/'.date('Ymd').'/'.$filename; |
90
|
|
|
} |
91
|
2 |
|
parent::setFilename($filename); |
92
|
2 |
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Clear local and remote file. |
96
|
|
|
*/ |
97
|
1 |
|
public function clear() |
98
|
|
|
{ |
99
|
1 |
|
$this->remote = ''; |
100
|
1 |
|
$this->local = null; |
101
|
1 |
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Has remote or local image. |
105
|
|
|
* |
106
|
|
|
* @Assert\True(message = "No selected image") |
107
|
|
|
* |
108
|
|
|
* @return bool |
109
|
|
|
*/ |
110
|
2 |
|
public function hasImage() |
111
|
|
|
{ |
112
|
2 |
|
return $this->remote || !is_null($this->local); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|