UploadedFile.php (6650B)
1 <?php 2 3 namespace PartKeepr\UploadedFileBundle\Entity; 4 5 use Doctrine\ORM\Mapping as ORM; 6 use PartKeepr\CoreBundle\Entity\BaseEntity; 7 use Ramsey\Uuid\Uuid; 8 use Symfony\Component\Serializer\Annotation\Groups; 9 10 /** 11 * @ORM\MappedSuperclass 12 */ 13 abstract class UploadedFile extends BaseEntity 14 { 15 /** 16 * Specifies the type of the file. 17 * 18 * @ORM\Column(type="string") 19 * @Groups({"default"}) 20 * 21 * @var string 22 **/ 23 private $type; 24 25 /** 26 * The unique filename of the file. Note that the filename does not contain any extension or any path information. 27 * 28 * @ORM\Column(type="string") 29 * @Groups({"default"}) 30 * 31 * @var string 32 */ 33 private $filename; 34 35 /** 36 * The original name of the file. Includes the extension of the file, but no path information. 37 * 38 * @ORM\Column(type="string",nullable=true,name="originalname") 39 * @Groups({"default"}) 40 * 41 * @var string 42 */ 43 private $originalFilename; 44 45 /** 46 * The MimeType for the file. 47 * 48 * @ORM\Column(type="string") 49 * @Groups({"default"}) 50 * 51 * @var string 52 */ 53 private $mimetype; 54 55 /** 56 * The size of the uploaded file. 57 * 58 * @ORM\Column(type="integer") 59 * @Groups({"default"}) 60 * 61 * @var int 62 */ 63 private $size; 64 65 /** 66 * Holds the extension of the given file. 67 * 68 * @ORM\Column(type="string",nullable=true) 69 * @Groups({"default"}) 70 * 71 * @var string 72 */ 73 private $extension; 74 75 /** 76 * The description of this attachment. 77 * 78 * @ORM\Column(type="text",nullable=true) 79 * @Groups({"default"}) 80 * 81 * @var string 82 */ 83 private $description; 84 85 /** 86 * Holds an ID of a replacement image. 87 * 88 * @Groups({"default"}) 89 */ 90 private $replacement = null; 91 92 /** 93 * @ORM\Column(type="datetime",nullable=false) 94 * 95 * @var \DateTime 96 */ 97 private $created; 98 99 public function __construct() 100 { 101 $this->filename = Uuid::uuid1()->toString(); 102 $this->setCreated(new \DateTime()); 103 } 104 105 /** 106 * Returns the created date. 107 * 108 * @return mixed 109 */ 110 public function getCreated() 111 { 112 return $this->created; 113 } 114 115 /** 116 * Sets the created date. 117 * 118 * @param mixed $created 119 */ 120 public function setCreated($created) 121 { 122 $this->created = $created; 123 } 124 125 /** 126 * Returns the replacement image, if set. 127 * 128 * @return mixed 129 */ 130 public function getReplacement() 131 { 132 return $this->replacement; 133 } 134 135 /** 136 * Sets a replacement image. 137 * 138 * @param $replacement 139 */ 140 public function setReplacement($replacement) 141 { 142 $this->replacement = $replacement; 143 } 144 145 /** 146 * Returns the original filename. 147 * 148 * @return string The original filename 149 */ 150 public function getOriginalFilename() 151 { 152 return $this->originalFilename; 153 } 154 155 /** 156 * Sets the original filename. 157 * 158 * @param string $filename The original filename 159 */ 160 public function setOriginalFilename($filename) 161 { 162 $this->originalFilename = $filename; 163 } 164 165 /** 166 * Returns the size of this file. 167 * 168 * @return int The size in bytes 169 */ 170 public function getSize() 171 { 172 return $this->size; 173 } 174 175 /** 176 * Sets the size of the file. 177 * 178 * @param int $size The size in bytes 179 */ 180 public function setSize($size) 181 { 182 $this->size = intval($size); 183 } 184 185 /** 186 * Returns the type of the file. 187 * 188 * @param none 189 * 190 * @return string The type of the file 191 */ 192 public function getType() 193 { 194 return $this->type; 195 } 196 197 /** 198 * Sets the type of the file. Once the type is set, 199 * it may not be changed later. 200 * 201 * @param string $type The type of the file 202 */ 203 protected function setType($type) 204 { 205 $this->type = $type; 206 } 207 208 /** 209 * Returns the description for this attachment. 210 * 211 * @return string The description 212 */ 213 public function getDescription() 214 { 215 return $this->description; 216 } 217 218 /** 219 * Sets the description for this attachment. 220 * 221 * @param string $description The attachment description 222 */ 223 public function setDescription($description) 224 { 225 $this->description = $description; 226 } 227 228 public function getFullFilename() 229 { 230 return $this->getFilename().'.'.$this->getExtension(); 231 } 232 233 /** 234 * Returns the plain filename without path and suffix. 235 * 236 * @return string The plain filename without path and suffix 237 */ 238 public function getFilename() 239 { 240 return $this->filename; 241 } 242 243 /** 244 * Sets the plain filename without path and suffix. 245 * 246 * @param $filename string The plain filename without path and suffix 247 */ 248 public function setFilename($filename) 249 { 250 $this->filename = $filename; 251 } 252 253 /** 254 * Returns the extension for the file. 255 * 256 * @return string The extension 257 */ 258 public function getExtension() 259 { 260 if ($this->extension == '') { 261 /* @noinspection PhpDeprecationInspection */ 262 return $this->getLegacyExtension(); 263 } 264 265 return $this->extension; 266 } 267 268 /** 269 * Sets the extension for this file. 270 * 271 * @param string $extension The extension 272 */ 273 public function setExtension($extension) 274 { 275 $this->extension = $extension; 276 } 277 278 /** 279 * Returns the extension for the given mime type. 280 * 281 * This function simply extracts that information from the mime type; 282 * special cases are not handled. e.g. if you have image/foobar, it would 283 * return "foobar" as extension. 284 * 285 * @todo Implement conversion from mimetype to extension in the setup routine 286 * 287 * @return string The extension 288 * 289 * @deprecated 290 */ 291 public function getLegacyExtension() 292 { 293 $data = explode('/', $this->getMimeType()); 294 295 if (array_key_exists(1, $data)) { 296 return $data[1]; 297 } else { 298 return 'undefined'; 299 } 300 } 301 302 /** 303 * Returns the mime type for this file. 304 * 305 * @return string The mimetype for this file, e.g. text/plain 306 */ 307 public function getMimeType() 308 { 309 return $this->mimetype; 310 } 311 312 /** 313 * Sets the mimetype for this file. 314 * 315 * @param string $mimeType The mimetype 316 */ 317 public function setMimeType($mimeType) 318 { 319 $this->mimetype = $mimeType; 320 } 321 }