|
1
|
|
|
<?php /** MicroFileValidator */ |
|
2
|
|
|
|
|
3
|
|
|
namespace Micro\Validator; |
|
4
|
|
|
|
|
5
|
|
|
use Micro\Form\IFormModel; |
|
6
|
|
|
use Micro\Web\RequestInjector; |
|
7
|
|
|
use Psr\Http\Message\UploadedFileInterface; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* EmailValidator class file. |
|
11
|
|
|
* |
|
12
|
|
|
* @author Oleg Lunegov <[email protected]> |
|
13
|
|
|
* @link https://github.com/linpax/microphp-framework |
|
14
|
|
|
* @copyright Copyright (c) 2013 Oleg Lunegov |
|
15
|
|
|
* @license https://github.com/linpax/microphp-framework/blob/master/LICENSE |
|
16
|
|
|
* @package Micro |
|
17
|
|
|
* @subpackage Validator |
|
18
|
|
|
* @version 1.0 |
|
19
|
|
|
* @since 1.0 |
|
20
|
|
|
*/ |
|
21
|
|
|
class FileValidator extends BaseValidator |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @inheritdoc |
|
25
|
|
|
*/ |
|
26
|
|
|
public function validate(IFormModel $model) |
|
27
|
|
|
{ |
|
28
|
|
|
foreach ($this->elements AS $element) { |
|
29
|
|
|
if (!$model->checkAttributeExists($element)) { |
|
30
|
|
|
/** @var UploadedFileInterface[] $files */ |
|
31
|
|
|
$files = (new RequestInjector)->build()->getUploadedFiles(); |
|
32
|
|
|
if (!empty($this->params['maxFiles']) && (count($files) > $this->params['maxFiles'])) { |
|
33
|
|
|
$this->errors[] = 'Too many files in parameter '.$element; |
|
34
|
|
|
|
|
35
|
|
|
return false; |
|
36
|
|
|
} |
|
37
|
|
|
foreach ($files AS $fContext) { |
|
38
|
|
View Code Duplication |
if (!empty($this->params['types']) && (strpos($this->params['types'], |
|
|
|
|
|
|
39
|
|
|
$fContext->getClientMediaType()) === false) |
|
40
|
|
|
) { |
|
41
|
|
|
$this->errors[] = 'File '.$fContext->getClientFilename().' not allowed type'; |
|
42
|
|
|
|
|
43
|
|
|
return false; |
|
44
|
|
|
} |
|
45
|
|
View Code Duplication |
if (!empty($this->params['minSize']) && ($fContext->getSize() < $this->params['minSize'])) { |
|
|
|
|
|
|
46
|
|
|
$this->errors[] = 'File '.$fContext->getClientFilename().' too small size'; |
|
47
|
|
|
|
|
48
|
|
|
return false; |
|
49
|
|
|
} |
|
50
|
|
View Code Duplication |
if (!empty($this->params['maxSize']) && ($fContext->getSize() > $this->params['maxSize'])) { |
|
|
|
|
|
|
51
|
|
|
$this->errors[] = 'File '.$fContext->getClientFilename().' too many size'; |
|
52
|
|
|
|
|
53
|
|
|
return false; |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
return true; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @inheritdoc |
|
64
|
|
|
*/ |
|
65
|
|
|
public function client(IFormModel $model) |
|
66
|
|
|
{ |
|
67
|
|
|
return ''; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.