Skip to content

Commit

Permalink
Add validation errors translating
Browse files Browse the repository at this point in the history
  • Loading branch information
Nitro-N committed Jul 18, 2021
1 parent e3d8f96 commit 2a1ba20
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 9 deletions.
24 changes: 22 additions & 2 deletions ErrorHandler/StandardErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,42 @@
namespace Glavweb\UploaderBundle\ErrorHandler;

use Glavweb\UploaderBundle\Response\Response;
use Symfony\Contracts\Translation\TranslatableInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

/**
* Class StandardErrorHandler
*
* @package Glavweb\UploaderBundle
* @author Andrey Nilov <[email protected]>
* @author Andrey Nilov <[email protected]>
*/
class StandardErrorHandler implements ErrorHandlerInterface
{
/**
* @var TranslatorInterface
*/
private $translator;

/**
* StandardErrorHandler constructor.
*/
public function __construct(TranslatorInterface $translator)
{
$this->translator = $translator;
}

/**
* @param Response $response
* @param \Exception $exception
*/
public function addException(Response $response, \Exception $exception)
{
$message = $exception->getMessage();
if ($exception instanceof TranslatableInterface) {
$message = $exception->trans($this->translator);
} else {
$message = $exception->getMessage();
}

$response['error'] = $message;
}
}
2 changes: 1 addition & 1 deletion EventListener/AllowedMimetypeValidationListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function onValidate(ValidationEvent $event)
$mimeType = $file->getMimeType();

if (!in_array($mimeType, $config['allowed_mimetypes'])) {
throw new ValidationException('error.whitelist');
throw new ValidationException('error.invalid_type');
}
}
}
2 changes: 1 addition & 1 deletion EventListener/DisallowedMimetypeValidationListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function onValidate(ValidationEvent $event)
$mimeType = $file->getExtension();

if (in_array($mimeType, $config['disallowed_mimetypes'])) {
throw new ValidationException('error.blacklist');
throw new ValidationException('error.invalid_type');
}
}
}
7 changes: 5 additions & 2 deletions EventListener/MaxSizeValidationListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@ public function onValidate(ValidationEvent $event)
$config = $event->getConfig();
$file = $event->getFile();

if ($file->getSize() > $config['max_size']*(1024*1024)) {
throw new ValidationException('error.maxsize');
if ($file->getSize() > $config['max_size'] * (1024 * 1024)) {
$fileSizeInMegaBytes = round($file->getSize() / (1024 * 1024));
throw new ValidationException(
'error.maxsize', ['filesize' => $fileSizeInMegaBytes, 'maxFilesize' => $config['max_size']]
);
}
}
}
31 changes: 30 additions & 1 deletion Exception/ValidationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,44 @@
namespace Glavweb\UploaderBundle\Exception;

use Symfony\Component\HttpFoundation\File\Exception\UploadException;
use Symfony\Contracts\Translation\TranslatableInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use Throwable;

/**
* Class ValidationException
*
* @package Glavweb\UploaderBundle
* @author Andrey Nilov <[email protected]>
*/
class ValidationException extends UploadException
class ValidationException extends UploadException implements TranslatableInterface
{
/**
* @var array
*/
protected $details;

/**
* @var string
*/
protected $errorMessage;

/**
* ValidationException constructor.
*
* @param string $message
* @param array $details
* @param int $code
* @param Throwable|null $previous
*/
public function __construct($message = "", $details = [], $code = 0, Throwable $previous = null)
{
parent::__construct($message, $code, $previous);

$this->details = $details;
}


/**
* @param string $message
* @return $this
Expand All @@ -49,4 +73,9 @@ public function getErrorMessage()

return $this->errorMessage;
}

public function trans(TranslatorInterface $translator, string $locale = null): string
{
return $translator->trans($this->getMessage(), $this->details, null, $locale);
}
}
4 changes: 3 additions & 1 deletion Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
</parameters>

<services>
<service id="glavweb_uploader.error_handler.standard" class="Glavweb\UploaderBundle\ErrorHandler\StandardErrorHandler" public="true"/>
<service id="glavweb_uploader.error_handler.standard" class="Glavweb\UploaderBundle\ErrorHandler\StandardErrorHandler" public="true">
<argument type="service" id="translator" />
</service>

<service id="glavweb_uploader.uploader_manager" class="Glavweb\UploaderBundle\Manager\UploaderManager" public="true">
<argument>%glavweb_uploader.config%</argument>
Expand Down
3 changes: 3 additions & 0 deletions Resources/translations/messages+intl-icu.en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
error:
invalid_type: You can't upload files of this type
maxsize: 'File is too big ({filesize} MB). Max filesize: {maxFilesize} MB'
3 changes: 3 additions & 0 deletions Resources/translations/messages+intl-icu.ru.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
error:
invalid_type: Неверный формат файла
maxsize: Файл слишком большой ({filesize} MB). Максимальный допустимый размер файла {maxFilesize} MB
Empty file.
Empty file.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"symfony/form": "^2.7|^3.0|^4.0|^5.0",
"twig/twig": "^2.12.1|^3.0",
"symfony/filesystem": "^2.7|^3.0|^4.0|^5.0",
"symfony/finder": "^2.7|^3.0|^4.0|^5.0"
"symfony/finder": "^2.7|^3.0|^4.0|^5.0",
"symfony/translation": "^2.7|^3.0|^4.0|^5.0"
},
"suggest": {
"oneup/flysystem-bundle": "Required for flysystem storage"
Expand Down

0 comments on commit 2a1ba20

Please sign in to comment.