diff --git a/ErrorHandler/StandardErrorHandler.php b/ErrorHandler/StandardErrorHandler.php index 4713590..8e39781 100644 --- a/ErrorHandler/StandardErrorHandler.php +++ b/ErrorHandler/StandardErrorHandler.php @@ -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 + * @author Andrey Nilov */ 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; } } diff --git a/EventListener/AllowedMimetypeValidationListener.php b/EventListener/AllowedMimetypeValidationListener.php index 367fb5a..d278a1a 100644 --- a/EventListener/AllowedMimetypeValidationListener.php +++ b/EventListener/AllowedMimetypeValidationListener.php @@ -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'); } } } diff --git a/EventListener/DisallowedMimetypeValidationListener.php b/EventListener/DisallowedMimetypeValidationListener.php index 26253ec..f41f9e9 100644 --- a/EventListener/DisallowedMimetypeValidationListener.php +++ b/EventListener/DisallowedMimetypeValidationListener.php @@ -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'); } } } diff --git a/EventListener/MaxSizeValidationListener.php b/EventListener/MaxSizeValidationListener.php index 8b5f97e..baca1fa 100644 --- a/EventListener/MaxSizeValidationListener.php +++ b/EventListener/MaxSizeValidationListener.php @@ -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']] + ); } } } diff --git a/Exception/ValidationException.php b/Exception/ValidationException.php index 542e425..de2e44c 100644 --- a/Exception/ValidationException.php +++ b/Exception/ValidationException.php @@ -12,6 +12,9 @@ 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 @@ -19,13 +22,34 @@ * @package Glavweb\UploaderBundle * @author Andrey Nilov */ -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 @@ -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); + } } diff --git a/Resources/config/services.xml b/Resources/config/services.xml index 73ad48f..b673681 100644 --- a/Resources/config/services.xml +++ b/Resources/config/services.xml @@ -12,7 +12,9 @@ - + + + %glavweb_uploader.config% diff --git a/Resources/translations/messages+intl-icu.en.yml b/Resources/translations/messages+intl-icu.en.yml new file mode 100644 index 0000000..50f4451 --- /dev/null +++ b/Resources/translations/messages+intl-icu.en.yml @@ -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' \ No newline at end of file diff --git a/Resources/translations/messages+intl-icu.ru.yml b/Resources/translations/messages+intl-icu.ru.yml new file mode 100644 index 0000000..c6a16bc --- /dev/null +++ b/Resources/translations/messages+intl-icu.ru.yml @@ -0,0 +1,3 @@ +error: + invalid_type: Неверный формат файла + maxsize: Файл слишком большой ({filesize} MB). Максимальный допустимый размер файла {maxFilesize} MB \ No newline at end of file diff --git a/Resources/translations/messages.en.yml b/Resources/translations/messages.en.yml deleted file mode 100644 index e69de29..0000000 diff --git a/Resources/translations/messages.ru.yml b/Resources/translations/messages.ru.yml deleted file mode 100644 index e69de29..0000000 diff --git a/composer.json b/composer.json index 7afda43..57c17be 100644 --- a/composer.json +++ b/composer.json @@ -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"