@@ -6,77 +6,77 @@ |
||
| 6 | 6 | class ImageValidatorServiceProvider extends ServiceProvider |
| 7 | 7 | { |
| 8 | 8 | |
| 9 | - /** |
|
| 10 | - * Indicates if loading of the provider is deferred. |
|
| 11 | - * |
|
| 12 | - * @var bool |
|
| 13 | - */ |
|
| 14 | - protected $defer = false; |
|
| 9 | + /** |
|
| 10 | + * Indicates if loading of the provider is deferred. |
|
| 11 | + * |
|
| 12 | + * @var bool |
|
| 13 | + */ |
|
| 14 | + protected $defer = false; |
|
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * @var array |
|
| 18 | - */ |
|
| 19 | - protected $rules = [ |
|
| 20 | - 'image_size', |
|
| 21 | - 'image_aspect', |
|
| 22 | - ]; |
|
| 16 | + /** |
|
| 17 | + * @var array |
|
| 18 | + */ |
|
| 19 | + protected $rules = [ |
|
| 20 | + 'image_size', |
|
| 21 | + 'image_aspect', |
|
| 22 | + ]; |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * Bootstrap the application events. |
|
| 26 | - * |
|
| 27 | - * @return void |
|
| 28 | - */ |
|
| 29 | - public function boot() |
|
| 30 | - { |
|
| 31 | - $this->loadTranslationsFrom(__DIR__ . '/../lang', 'image-validator'); |
|
| 24 | + /** |
|
| 25 | + * Bootstrap the application events. |
|
| 26 | + * |
|
| 27 | + * @return void |
|
| 28 | + */ |
|
| 29 | + public function boot() |
|
| 30 | + { |
|
| 31 | + $this->loadTranslationsFrom(__DIR__ . '/../lang', 'image-validator'); |
|
| 32 | 32 | |
| 33 | - $messages = trans('image-validator::validation'); |
|
| 33 | + $messages = trans('image-validator::validation'); |
|
| 34 | 34 | |
| 35 | - $this->app->bind(ImageValidator::class, function($app) use ($messages) { |
|
| 36 | - $validator = new ImageValidator($app['translator'], [], [], $messages); |
|
| 35 | + $this->app->bind(ImageValidator::class, function($app) use ($messages) { |
|
| 36 | + $validator = new ImageValidator($app['translator'], [], [], $messages); |
|
| 37 | 37 | |
| 38 | - if (isset($app['validation.presence'])) { |
|
| 39 | - $validator->setPresenceVerifier($app['validation.presence']); |
|
| 40 | - } |
|
| 38 | + if (isset($app['validation.presence'])) { |
|
| 39 | + $validator->setPresenceVerifier($app['validation.presence']); |
|
| 40 | + } |
|
| 41 | 41 | |
| 42 | - return $validator; |
|
| 43 | - }); |
|
| 42 | + return $validator; |
|
| 43 | + }); |
|
| 44 | 44 | |
| 45 | - $this->addNewRules(); |
|
| 46 | - } |
|
| 45 | + $this->addNewRules(); |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * Get the list of new rules being added to the validator. |
|
| 50 | - * |
|
| 51 | - * @return array |
|
| 52 | - */ |
|
| 53 | - public function getRules() |
|
| 54 | - { |
|
| 55 | - return $this->rules; |
|
| 56 | - } |
|
| 48 | + /** |
|
| 49 | + * Get the list of new rules being added to the validator. |
|
| 50 | + * |
|
| 51 | + * @return array |
|
| 52 | + */ |
|
| 53 | + public function getRules() |
|
| 54 | + { |
|
| 55 | + return $this->rules; |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - /** |
|
| 59 | - * Add new rules to the validator. |
|
| 60 | - */ |
|
| 61 | - protected function addNewRules() |
|
| 62 | - { |
|
| 63 | - foreach ($this->getRules() as $rule) { |
|
| 64 | - $this->extendValidator($rule); |
|
| 65 | - } |
|
| 66 | - } |
|
| 58 | + /** |
|
| 59 | + * Add new rules to the validator. |
|
| 60 | + */ |
|
| 61 | + protected function addNewRules() |
|
| 62 | + { |
|
| 63 | + foreach ($this->getRules() as $rule) { |
|
| 64 | + $this->extendValidator($rule); |
|
| 65 | + } |
|
| 66 | + } |
|
| 67 | 67 | |
| 68 | - /** |
|
| 69 | - * Extend the validator with new rules. |
|
| 70 | - * |
|
| 71 | - * @param string $rule |
|
| 72 | - * @return void |
|
| 73 | - */ |
|
| 74 | - protected function extendValidator($rule) |
|
| 75 | - { |
|
| 76 | - $method = studly_case($rule); |
|
| 77 | - $translation = $this->app['translator']->trans('image-validator::validation.' . $rule); |
|
| 78 | - $this->app['validator']->extend($rule, 'Cviebrock\ImageValidator\ImageValidator@validate' . $method, |
|
| 79 | - $translation); |
|
| 80 | - $this->app['validator']->replacer($rule, 'Cviebrock\ImageValidator\ImageValidator@replace' . $method); |
|
| 81 | - } |
|
| 68 | + /** |
|
| 69 | + * Extend the validator with new rules. |
|
| 70 | + * |
|
| 71 | + * @param string $rule |
|
| 72 | + * @return void |
|
| 73 | + */ |
|
| 74 | + protected function extendValidator($rule) |
|
| 75 | + { |
|
| 76 | + $method = studly_case($rule); |
|
| 77 | + $translation = $this->app['translator']->trans('image-validator::validation.' . $rule); |
|
| 78 | + $this->app['validator']->extend($rule, 'Cviebrock\ImageValidator\ImageValidator@validate' . $method, |
|
| 79 | + $translation); |
|
| 80 | + $this->app['validator']->replacer($rule, 'Cviebrock\ImageValidator\ImageValidator@replace' . $method); |
|
| 81 | + } |
|
| 82 | 82 | } |
@@ -9,243 +9,243 @@ |
||
| 9 | 9 | class ImageValidator extends Validator |
| 10 | 10 | { |
| 11 | 11 | |
| 12 | - /** |
|
| 13 | - * Creates a new instance of ImageValidator |
|
| 14 | - * |
|
| 15 | - * @param \Illuminate\Contracts\Translation\Translator $translator |
|
| 16 | - * @param array $data |
|
| 17 | - * @param array $rules |
|
| 18 | - * @param array $messages |
|
| 19 | - * @param array $customAttributes |
|
| 20 | - */ |
|
| 21 | - public function __construct( |
|
| 22 | - Translator $translator, |
|
| 23 | - array $data, |
|
| 24 | - array $rules, |
|
| 25 | - array $messages = [], |
|
| 26 | - array $customAttributes = [] |
|
| 27 | - ) { |
|
| 28 | - parent::__construct($translator, $data, $rules, $messages, $customAttributes); |
|
| 29 | - } |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * Usage: image_size:width[,height] |
|
| 33 | - * |
|
| 34 | - * @param $attribute string |
|
| 35 | - * @param $value string|array |
|
| 36 | - * @param $parameters array |
|
| 37 | - * @return boolean |
|
| 38 | - */ |
|
| 39 | - public function validateImageSize($attribute, $value, $parameters) |
|
| 40 | - { |
|
| 41 | - $image = $this->getImagePath($value); |
|
| 42 | - |
|
| 43 | - // Get the image dimension info, or fail. |
|
| 44 | - |
|
| 45 | - $image_size = @getimagesize($image); |
|
| 46 | - if ($image_size === false) { |
|
| 47 | - return false; |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - // If only one dimension rule is passed, assume it applies to both height and width. |
|
| 51 | - |
|
| 52 | - if (!isset($parameters[1])) { |
|
| 53 | - $parameters[1] = $parameters[0]; |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - // Parse the parameters. Options are: |
|
| 57 | - // |
|
| 58 | - // "300" or "=300" - dimension must be exactly 300 pixels |
|
| 59 | - // "<300" - dimension must be less than 300 pixels |
|
| 60 | - // "<=300" - dimension must be less than or equal to 300 pixels |
|
| 61 | - // ">300" - dimension must be greater than 300 pixels |
|
| 62 | - // ">=300" - dimension must be greater than or equal to 300 pixels |
|
| 63 | - |
|
| 64 | - $width_check = $this->checkDimension($parameters[0], $image_size[0]); |
|
| 65 | - $height_check = $this->checkDimension($parameters[1], $image_size[1]); |
|
| 66 | - |
|
| 67 | - return $width_check['pass'] && $height_check['pass']; |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * Build the error message for validation failures. |
|
| 72 | - * |
|
| 73 | - * @param string $message |
|
| 74 | - * @param string $attribute |
|
| 75 | - * @param string $rule |
|
| 76 | - * @param array $parameters |
|
| 77 | - * @return string |
|
| 78 | - */ |
|
| 79 | - public function replaceImageSize($message, $attribute, $rule, $parameters) |
|
| 80 | - { |
|
| 81 | - $width = $height = $this->checkDimension($parameters[0]); |
|
| 82 | - if (isset($parameters[1])) { |
|
| 83 | - $height = $this->checkDimension($parameters[1]); |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - return str_replace( |
|
| 87 | - [':width', ':height'], |
|
| 88 | - [$width['message'], $height['message']], |
|
| 89 | - $message |
|
| 90 | - ); |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * Usage: image_aspect:ratio |
|
| 95 | - * |
|
| 96 | - * @param $attribute string |
|
| 97 | - * @param $value string|array |
|
| 98 | - * @param $parameters array |
|
| 99 | - * @return boolean |
|
| 100 | - * @throws \RuntimeException |
|
| 101 | - */ |
|
| 102 | - public function validateImageAspect($attribute, $value, $parameters) |
|
| 103 | - { |
|
| 104 | - $image = $this->getImagePath($value); |
|
| 105 | - |
|
| 106 | - // Get the image dimension info, or fail. |
|
| 107 | - |
|
| 108 | - $image_size = @getimagesize($image); |
|
| 109 | - if ($image_size === false) { |
|
| 110 | - return false; |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - $image_width = $image_size[0]; |
|
| 114 | - $image_height = $image_size[1]; |
|
| 115 | - |
|
| 116 | - // Parse the parameter(s). Options are: |
|
| 117 | - // |
|
| 118 | - // "0.75" - one param: a decimal ratio (width/height) |
|
| 119 | - // "3,4" - two params: width, height |
|
| 120 | - // |
|
| 121 | - // If the first value is prefixed with "~", the orientation doesn't matter, i.e.: |
|
| 122 | - // |
|
| 123 | - // "~3,4" - would accept either "3:4" or "4:3" images |
|
| 124 | - |
|
| 125 | - $both_orientations = false; |
|
| 126 | - |
|
| 127 | - if (substr($parameters[0], 0, 1) === '~') { |
|
| 128 | - $parameters[0] = substr($parameters[0], 1); |
|
| 129 | - $both_orientations = true; |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - if (count($parameters) === 1) { |
|
| 133 | - $aspect_width = $parameters[0]; |
|
| 134 | - $aspect_height = 1; |
|
| 135 | - } else { |
|
| 136 | - $aspect_width = (int) $parameters[0]; |
|
| 137 | - $aspect_height = (int) $parameters[1]; |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - if ($aspect_width === 0 || $aspect_height === 0) { |
|
| 141 | - throw new RuntimeException('Aspect is zero or infinite: ' . $parameters[0]); |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - $check = ($image_width * $aspect_height) / $aspect_width; |
|
| 145 | - |
|
| 146 | - if ((int) round($check) === $image_height) { |
|
| 147 | - return true; |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - if ($both_orientations) { |
|
| 151 | - $check = ($image_width * $aspect_width) / $aspect_height; |
|
| 152 | - if ((int) round($check) === $image_height) { |
|
| 153 | - return true; |
|
| 154 | - } |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - return false; |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - /** |
|
| 161 | - * Build the error message for validation failures. |
|
| 162 | - * |
|
| 163 | - * @param string $message |
|
| 164 | - * @param string $attribute |
|
| 165 | - * @param string $rule |
|
| 166 | - * @param array $parameters |
|
| 167 | - * @return string |
|
| 168 | - */ |
|
| 169 | - public function replaceImageAspect($message, $attribute, $rule, $parameters) |
|
| 170 | - { |
|
| 171 | - return str_replace(':aspect', implode(':', $parameters), $message); |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - /** |
|
| 175 | - * Parse the dimension rule and check if the dimension passes the rule. |
|
| 176 | - * |
|
| 177 | - * @param string $rule |
|
| 178 | - * @param integer $dimension |
|
| 179 | - * @return array |
|
| 180 | - * @throws \RuntimeException |
|
| 181 | - */ |
|
| 182 | - protected function checkDimension($rule, $dimension = 0) |
|
| 183 | - { |
|
| 184 | - |
|
| 185 | - $dimension = (int) $dimension; |
|
| 186 | - |
|
| 187 | - if ($rule === '*') { |
|
| 188 | - $message = $this->translator->trans('image-validator::validation.anysize'); |
|
| 189 | - $pass = true; |
|
| 190 | - } else if (preg_match('/^(\d+)\-(\d+)$/', $rule, $matches)) { |
|
| 191 | - $size1 = (int) $matches[1]; |
|
| 192 | - $size2 = (int) $matches[2]; |
|
| 193 | - $message = $this->translator->trans('image-validator::validation.between', compact('size1', 'size2')); |
|
| 194 | - $pass = ($dimension >= $size1) && ($dimension <= $size2); |
|
| 195 | - } else if (preg_match('/^([<=>]*)(\d+)$/', $rule, $matches)) { |
|
| 196 | - |
|
| 197 | - $size = (int) $matches[2]; |
|
| 198 | - |
|
| 199 | - switch ($matches[1]) { |
|
| 200 | - case '>': |
|
| 201 | - $message = $this->translator->trans('image-validator::validation.greaterthan', compact('size')); |
|
| 202 | - $pass = $dimension > $size; |
|
| 203 | - break; |
|
| 204 | - case '>=': |
|
| 205 | - $message = $this->translator->trans('image-validator::validation.greaterthanorequal', |
|
| 206 | - compact('size')); |
|
| 207 | - $pass = $dimension >= $size; |
|
| 208 | - break; |
|
| 209 | - case '<': |
|
| 210 | - $message = $this->translator->trans('image-validator::validation.lessthan', compact('size')); |
|
| 211 | - $pass = $dimension < $size; |
|
| 212 | - break; |
|
| 213 | - case '<=': |
|
| 214 | - $message = $this->translator->trans('image-validator::validation.lessthanorequal', compact('size')); |
|
| 215 | - $pass = $dimension <= $size; |
|
| 216 | - break; |
|
| 217 | - case '=': |
|
| 218 | - case '': |
|
| 219 | - $message = $this->translator->trans('image-validator::validation.equal', compact('size')); |
|
| 220 | - $pass = $dimension == $size; |
|
| 221 | - break; |
|
| 222 | - default: |
|
| 223 | - throw new RuntimeException('Unknown image size validation rule: ' . $rule); |
|
| 224 | - } |
|
| 225 | - } else { |
|
| 226 | - throw new RuntimeException('Unknown image size validation rule: ' . $rule); |
|
| 227 | - } |
|
| 228 | - |
|
| 229 | - return compact('message', 'pass'); |
|
| 230 | - } |
|
| 231 | - |
|
| 232 | - /** |
|
| 233 | - * @param mixed $value |
|
| 234 | - * @return string |
|
| 235 | - */ |
|
| 236 | - protected function getImagePath($value) |
|
| 237 | - { |
|
| 238 | - // if were passed an instance of UploadedFile, return the path |
|
| 239 | - if ($value instanceof UploadedFile) { |
|
| 240 | - return $value->getPathname(); |
|
| 241 | - } |
|
| 242 | - |
|
| 243 | - // if we're passed a PHP file upload array, return the "tmp_name" |
|
| 244 | - if (is_array($value) && array_get($value, 'tmp_name') !== null) { |
|
| 245 | - return $value['tmp_name']; |
|
| 246 | - } |
|
| 247 | - |
|
| 248 | - // fallback: we were likely passed a path already |
|
| 249 | - return $value; |
|
| 250 | - } |
|
| 12 | + /** |
|
| 13 | + * Creates a new instance of ImageValidator |
|
| 14 | + * |
|
| 15 | + * @param \Illuminate\Contracts\Translation\Translator $translator |
|
| 16 | + * @param array $data |
|
| 17 | + * @param array $rules |
|
| 18 | + * @param array $messages |
|
| 19 | + * @param array $customAttributes |
|
| 20 | + */ |
|
| 21 | + public function __construct( |
|
| 22 | + Translator $translator, |
|
| 23 | + array $data, |
|
| 24 | + array $rules, |
|
| 25 | + array $messages = [], |
|
| 26 | + array $customAttributes = [] |
|
| 27 | + ) { |
|
| 28 | + parent::__construct($translator, $data, $rules, $messages, $customAttributes); |
|
| 29 | + } |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * Usage: image_size:width[,height] |
|
| 33 | + * |
|
| 34 | + * @param $attribute string |
|
| 35 | + * @param $value string|array |
|
| 36 | + * @param $parameters array |
|
| 37 | + * @return boolean |
|
| 38 | + */ |
|
| 39 | + public function validateImageSize($attribute, $value, $parameters) |
|
| 40 | + { |
|
| 41 | + $image = $this->getImagePath($value); |
|
| 42 | + |
|
| 43 | + // Get the image dimension info, or fail. |
|
| 44 | + |
|
| 45 | + $image_size = @getimagesize($image); |
|
| 46 | + if ($image_size === false) { |
|
| 47 | + return false; |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + // If only one dimension rule is passed, assume it applies to both height and width. |
|
| 51 | + |
|
| 52 | + if (!isset($parameters[1])) { |
|
| 53 | + $parameters[1] = $parameters[0]; |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + // Parse the parameters. Options are: |
|
| 57 | + // |
|
| 58 | + // "300" or "=300" - dimension must be exactly 300 pixels |
|
| 59 | + // "<300" - dimension must be less than 300 pixels |
|
| 60 | + // "<=300" - dimension must be less than or equal to 300 pixels |
|
| 61 | + // ">300" - dimension must be greater than 300 pixels |
|
| 62 | + // ">=300" - dimension must be greater than or equal to 300 pixels |
|
| 63 | + |
|
| 64 | + $width_check = $this->checkDimension($parameters[0], $image_size[0]); |
|
| 65 | + $height_check = $this->checkDimension($parameters[1], $image_size[1]); |
|
| 66 | + |
|
| 67 | + return $width_check['pass'] && $height_check['pass']; |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * Build the error message for validation failures. |
|
| 72 | + * |
|
| 73 | + * @param string $message |
|
| 74 | + * @param string $attribute |
|
| 75 | + * @param string $rule |
|
| 76 | + * @param array $parameters |
|
| 77 | + * @return string |
|
| 78 | + */ |
|
| 79 | + public function replaceImageSize($message, $attribute, $rule, $parameters) |
|
| 80 | + { |
|
| 81 | + $width = $height = $this->checkDimension($parameters[0]); |
|
| 82 | + if (isset($parameters[1])) { |
|
| 83 | + $height = $this->checkDimension($parameters[1]); |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + return str_replace( |
|
| 87 | + [':width', ':height'], |
|
| 88 | + [$width['message'], $height['message']], |
|
| 89 | + $message |
|
| 90 | + ); |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * Usage: image_aspect:ratio |
|
| 95 | + * |
|
| 96 | + * @param $attribute string |
|
| 97 | + * @param $value string|array |
|
| 98 | + * @param $parameters array |
|
| 99 | + * @return boolean |
|
| 100 | + * @throws \RuntimeException |
|
| 101 | + */ |
|
| 102 | + public function validateImageAspect($attribute, $value, $parameters) |
|
| 103 | + { |
|
| 104 | + $image = $this->getImagePath($value); |
|
| 105 | + |
|
| 106 | + // Get the image dimension info, or fail. |
|
| 107 | + |
|
| 108 | + $image_size = @getimagesize($image); |
|
| 109 | + if ($image_size === false) { |
|
| 110 | + return false; |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + $image_width = $image_size[0]; |
|
| 114 | + $image_height = $image_size[1]; |
|
| 115 | + |
|
| 116 | + // Parse the parameter(s). Options are: |
|
| 117 | + // |
|
| 118 | + // "0.75" - one param: a decimal ratio (width/height) |
|
| 119 | + // "3,4" - two params: width, height |
|
| 120 | + // |
|
| 121 | + // If the first value is prefixed with "~", the orientation doesn't matter, i.e.: |
|
| 122 | + // |
|
| 123 | + // "~3,4" - would accept either "3:4" or "4:3" images |
|
| 124 | + |
|
| 125 | + $both_orientations = false; |
|
| 126 | + |
|
| 127 | + if (substr($parameters[0], 0, 1) === '~') { |
|
| 128 | + $parameters[0] = substr($parameters[0], 1); |
|
| 129 | + $both_orientations = true; |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + if (count($parameters) === 1) { |
|
| 133 | + $aspect_width = $parameters[0]; |
|
| 134 | + $aspect_height = 1; |
|
| 135 | + } else { |
|
| 136 | + $aspect_width = (int) $parameters[0]; |
|
| 137 | + $aspect_height = (int) $parameters[1]; |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + if ($aspect_width === 0 || $aspect_height === 0) { |
|
| 141 | + throw new RuntimeException('Aspect is zero or infinite: ' . $parameters[0]); |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + $check = ($image_width * $aspect_height) / $aspect_width; |
|
| 145 | + |
|
| 146 | + if ((int) round($check) === $image_height) { |
|
| 147 | + return true; |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + if ($both_orientations) { |
|
| 151 | + $check = ($image_width * $aspect_width) / $aspect_height; |
|
| 152 | + if ((int) round($check) === $image_height) { |
|
| 153 | + return true; |
|
| 154 | + } |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + return false; |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + /** |
|
| 161 | + * Build the error message for validation failures. |
|
| 162 | + * |
|
| 163 | + * @param string $message |
|
| 164 | + * @param string $attribute |
|
| 165 | + * @param string $rule |
|
| 166 | + * @param array $parameters |
|
| 167 | + * @return string |
|
| 168 | + */ |
|
| 169 | + public function replaceImageAspect($message, $attribute, $rule, $parameters) |
|
| 170 | + { |
|
| 171 | + return str_replace(':aspect', implode(':', $parameters), $message); |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + /** |
|
| 175 | + * Parse the dimension rule and check if the dimension passes the rule. |
|
| 176 | + * |
|
| 177 | + * @param string $rule |
|
| 178 | + * @param integer $dimension |
|
| 179 | + * @return array |
|
| 180 | + * @throws \RuntimeException |
|
| 181 | + */ |
|
| 182 | + protected function checkDimension($rule, $dimension = 0) |
|
| 183 | + { |
|
| 184 | + |
|
| 185 | + $dimension = (int) $dimension; |
|
| 186 | + |
|
| 187 | + if ($rule === '*') { |
|
| 188 | + $message = $this->translator->trans('image-validator::validation.anysize'); |
|
| 189 | + $pass = true; |
|
| 190 | + } else if (preg_match('/^(\d+)\-(\d+)$/', $rule, $matches)) { |
|
| 191 | + $size1 = (int) $matches[1]; |
|
| 192 | + $size2 = (int) $matches[2]; |
|
| 193 | + $message = $this->translator->trans('image-validator::validation.between', compact('size1', 'size2')); |
|
| 194 | + $pass = ($dimension >= $size1) && ($dimension <= $size2); |
|
| 195 | + } else if (preg_match('/^([<=>]*)(\d+)$/', $rule, $matches)) { |
|
| 196 | + |
|
| 197 | + $size = (int) $matches[2]; |
|
| 198 | + |
|
| 199 | + switch ($matches[1]) { |
|
| 200 | + case '>': |
|
| 201 | + $message = $this->translator->trans('image-validator::validation.greaterthan', compact('size')); |
|
| 202 | + $pass = $dimension > $size; |
|
| 203 | + break; |
|
| 204 | + case '>=': |
|
| 205 | + $message = $this->translator->trans('image-validator::validation.greaterthanorequal', |
|
| 206 | + compact('size')); |
|
| 207 | + $pass = $dimension >= $size; |
|
| 208 | + break; |
|
| 209 | + case '<': |
|
| 210 | + $message = $this->translator->trans('image-validator::validation.lessthan', compact('size')); |
|
| 211 | + $pass = $dimension < $size; |
|
| 212 | + break; |
|
| 213 | + case '<=': |
|
| 214 | + $message = $this->translator->trans('image-validator::validation.lessthanorequal', compact('size')); |
|
| 215 | + $pass = $dimension <= $size; |
|
| 216 | + break; |
|
| 217 | + case '=': |
|
| 218 | + case '': |
|
| 219 | + $message = $this->translator->trans('image-validator::validation.equal', compact('size')); |
|
| 220 | + $pass = $dimension == $size; |
|
| 221 | + break; |
|
| 222 | + default: |
|
| 223 | + throw new RuntimeException('Unknown image size validation rule: ' . $rule); |
|
| 224 | + } |
|
| 225 | + } else { |
|
| 226 | + throw new RuntimeException('Unknown image size validation rule: ' . $rule); |
|
| 227 | + } |
|
| 228 | + |
|
| 229 | + return compact('message', 'pass'); |
|
| 230 | + } |
|
| 231 | + |
|
| 232 | + /** |
|
| 233 | + * @param mixed $value |
|
| 234 | + * @return string |
|
| 235 | + */ |
|
| 236 | + protected function getImagePath($value) |
|
| 237 | + { |
|
| 238 | + // if were passed an instance of UploadedFile, return the path |
|
| 239 | + if ($value instanceof UploadedFile) { |
|
| 240 | + return $value->getPathname(); |
|
| 241 | + } |
|
| 242 | + |
|
| 243 | + // if we're passed a PHP file upload array, return the "tmp_name" |
|
| 244 | + if (is_array($value) && array_get($value, 'tmp_name') !== null) { |
|
| 245 | + return $value['tmp_name']; |
|
| 246 | + } |
|
| 247 | + |
|
| 248 | + // fallback: we were likely passed a path already |
|
| 249 | + return $value; |
|
| 250 | + } |
|
| 251 | 251 | } |