From 1bfde01f42fb2e3f911f1e884eff9e82797e0f04 Mon Sep 17 00:00:00 2001 From: Maxime Renou Date: Thu, 21 Feb 2019 12:28:50 +0100 Subject: [PATCH] Awesome features (you'll see) --- ValidatorBundle/Validator.php | 32 ++++++++++++++++++++++++++++++++ composer.json | 3 ++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/ValidatorBundle/Validator.php b/ValidatorBundle/Validator.php index a275320..3163980 100644 --- a/ValidatorBundle/Validator.php +++ b/ValidatorBundle/Validator.php @@ -2,6 +2,8 @@ namespace Bluesquare\ValidatorBundle; +use Bluesquare\StorageBundle\Exceptions\MimeTypeException; +use Bluesquare\StorageBundle\Storage; use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Contracts\Translation\TranslatorInterface; @@ -206,6 +208,36 @@ class Validator return true; } + public function injectFile() + { + $args = func_get_args(); + if (count($args) === 0) return false; + if (is_object($args[0])) $entity = array_shift($args); + else $entity = $this->entity; + + if (is_null($entity) || count($args) != 2) return false; + $attribute = $args[0]; + $storage = $args[1]; + + // handle + if (!$this->hasFile($attribute)) + { + $this->error($attribute, 'required'); + return false; + } + + if ($storage instanceof Storage) + { + try { + return $storage->store($entity, $attribute, $this->getFile($attribute)); + } + catch(MimeTypeException $exception) { + $this->error($attribute, 'file_mime_type'); + } + } + return false; + } + // public function post() diff --git a/composer.json b/composer.json index 7a855b2..0c51c2b 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,8 @@ "type": "symfony-bundle", "require": { "php": ">=7.1", - "symfony/translation": "^4.2" + "symfony/translation": "^4.2", + "bluesquare-packages/symfony-storage": "dev-master" }, "autoload": { "psr-4": {