Created
March 30, 2016 14:19
-
-
Save jehaby/7f0912bbb3323004f4483a1a2540617d to your computer and use it in GitHub Desktop.
2 of one fields valid
This file contains hidden or 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 AppBundle\Controller; | |
... | |
class ApiController extends Controller | |
{ | |
/** | |
* @Route("...") | |
* @Method("POST") | |
*/ | |
public function postFilesAction(Request $request) | |
{ | |
$validator = $this->get('validator'); | |
$requestCredentials = PostRequestCredentials::fromRequest($request, $this->getParameter('max_filesize')); | |
$errors = $validator->validate($requestCredentials); | |
if (count($errors) !== 0) { | |
return $this->processAndReturnErrors($errors); | |
} | |
... | |
} | |
... | |
} |
This file contains hidden or 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 AppBundle\RequestCredentials; | |
use Symfony\Component\HttpFoundation\File\File; | |
use Symfony\Component\HttpFoundation\File\UploadedFile; | |
use Symfony\Component\HttpFoundation\Request; | |
use Symfony\Component\Validator\Constraints as Assert; | |
use Symfony\Component\Validator\Context\ExecutionContextInterface; | |
use Symfony\Component\Validator\Mapping\ClassMetadata; | |
class PostRequestCredentials | |
{ | |
const FILES_POST_USER_ID_BLANK = 2; | |
const FILES_POST_FILE_TOO_BIG = 3; | |
const FILES_POST_FILE_NOT_IMAGE = 4; | |
const FILES_POST_CREATION_THROUGH_URL_ERROR = 5; | |
public static $maxFilesize; | |
/** | |
* @Assert\NotBlank(payload = {"error_code" = "2"}, message="user_id mustn't be blank") | |
* @Assert\Type(type="digit", payload = {"error_code" = "2"}, message="user_id mustn't be blank") | |
* @Assert\Range(min = 1, minMessage="user_id must be more than zero", invalidMessage="user_id must be a positive integer") | |
*/ | |
private $userId; | |
private $file; | |
/** | |
* @Assert\Url(payload = {"error_code" = "5"}) | |
*/ | |
private $fileUrl; | |
public static function fromRequest(Request $request, $maxFilesize) | |
{ | |
$instance = new self; | |
$instance->userId = $request->get('user_id'); | |
$instance->file = $request->files->get('file'); | |
$instance->fileUrl = $request->get('file_url'); | |
self::$maxFilesize = $maxFilesize; | |
return $instance; | |
} | |
/** | |
* @Assert\Callback() | |
*/ | |
public function validate(ExecutionContextInterface $context) | |
{ | |
if (! ($this->fileExistsAndValid() || $this->fileUrlExistsAndValid())) { | |
$context->buildViolation('Neither file nor file_url is present.')->addViolation(); | |
} | |
} | |
private function fileExistsAndValid() | |
{ | |
return isset($this->file) && ($this->file instanceof UploadedFile); | |
} | |
private function fileUrlExistsAndValid() | |
{ | |
return isset($this->fileUrl) && ($this->fileUrl); | |
} | |
/** | |
* @return integer | |
*/ | |
public function getUserId() | |
{ | |
return $this->userId; | |
} | |
/** | |
* @return File | null | |
*/ | |
public function getFile() | |
{ | |
return $this->file; | |
} | |
/** | |
* @return string | |
*/ | |
public function getFileUrl() | |
{ | |
return $this->fileUrl; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment