Created
July 18, 2014 15:06
-
-
Save l3l0/e97b40500a79c29d7887 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Cocoders\File\Model; | |
class File | |
{ | |
private $id; | |
private $name; | |
private $originalName; | |
private $mimeType; | |
private $uploadedFile; | |
private $uploaded = false; | |
private $uploadedAt; | |
public function upload(UploadedFile $uploadedFile) | |
{ | |
$this->originalName = $uploadedFile->getClientOriginalName(); | |
$this->mimeType = $uploadedFile->getClientMimeType(); | |
$this->uploadedFile = $uploadedFile->getFile(); | |
$this->uploaded = true; | |
$this->uploadedAt = new \DateTime(); | |
return $this; | |
} | |
public function getUploadedFile() | |
{ | |
return $this->uploadedFile; | |
} | |
public function setName($name) | |
{ | |
$this->name = $name; | |
return $this; | |
} | |
public function getName() | |
{ | |
return $this->name; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Cocoders\Bundle\FileBundle\Controller; | |
use Symfony\Component\HttpFoundation\Request; | |
use Cocoders\File\Model\Repository\FileRepository; | |
use Cocoders\Bundle\FileBundle\File\UploadedFile; | |
class FileController | |
{ | |
private $repository; | |
public function __construct(FileRepository $repository) | |
{ | |
$this->repository = $repository; | |
} | |
public function uploadAction(Request $request) | |
{ | |
$uploadedFile = reset($request->files->all()); | |
$file = new File(); | |
$file->upload(new UploadedFile($uploadedFile)); | |
$this->repository->add($file); | |
return $file; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Cocoders\Bundle\FileBundle\File; | |
use Cocoders\File\Model\UploadedFile as BaseUploadedFile; | |
use Symfony\Component\HttpFoundation\File\File as File; | |
class UploadedFile implements BaseUploadedFile | |
{ | |
private $uploadedFile; | |
function __construct(File $uploadedFile) | |
{ | |
$this->uploadedFile = $uploadedFile; | |
} | |
public function getClientOriginalName() | |
{ | |
return $this->uploadedFile->getClientOriginalName(); | |
} | |
public function getClientMimeType() | |
{ | |
return $this->uploadedFile->getClientMimeType(); | |
} | |
public function getFile() | |
{ | |
return $this->uploadedFile; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Cocoders\File\Model; | |
interface UploadedFile | |
{ | |
public function getClientOriginalName(); | |
public function getClientMimeType(); | |
public function getFile(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment