|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Author: Nil Portugués Calderó <[email protected]> |
|
4
|
|
|
* Date: 9/24/14 |
|
5
|
|
|
* Time: 3:04 PM |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace NilPortugues\Validator\Attribute\FileUpload; |
|
12
|
|
|
|
|
13
|
|
|
use NilPortugues\Validator\AbstractValidator; |
|
14
|
|
|
use NilPortugues\Validator\Validator; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Class FileUploadAttribute |
|
18
|
|
|
* @package NilPortugues\Validator\Attribute\FileUploadAttribute |
|
19
|
|
|
*/ |
|
20
|
|
|
class FileUploadAttribute extends AbstractValidator |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @param string $propertyName |
|
24
|
|
|
* @param Validator $validator |
|
25
|
|
|
* @param array $errorMessages |
|
26
|
|
|
* @param array $functionMap |
|
27
|
|
|
*/ |
|
28
|
|
|
public function __construct($propertyName, Validator $validator, array &$errorMessages, array &$functionMap) |
|
29
|
|
|
{ |
|
30
|
|
|
parent::__construct($propertyName, $validator, $errorMessages, $functionMap); |
|
31
|
|
|
|
|
32
|
|
|
$this->addCondition(__METHOD__); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param int $minSize |
|
37
|
|
|
* @param int $maxSize |
|
38
|
|
|
* @param string $format |
|
39
|
|
|
* @param bool $inclusive |
|
40
|
|
|
* |
|
41
|
|
|
* @return $this |
|
42
|
|
|
*/ |
|
43
|
|
|
public function isBetween($minSize, $maxSize, $format = 'B', $inclusive = false) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->addCondition( |
|
46
|
|
|
__METHOD__, |
|
47
|
|
|
[$minSize, $maxSize, $format, $inclusive], |
|
48
|
|
|
[ |
|
49
|
|
|
'min' => $minSize, |
|
50
|
|
|
'max' => $maxSize, |
|
51
|
|
|
'format' => (\strtoupper($format[0]) == 'B') ? '' : \strtoupper($format[0]) |
|
52
|
|
|
] |
|
53
|
|
|
); |
|
54
|
|
|
|
|
55
|
|
|
return $this; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param string[] $allowedTypes |
|
60
|
|
|
* |
|
61
|
|
|
* @return $this |
|
62
|
|
|
*/ |
|
63
|
|
|
public function isMimeType(array $allowedTypes) |
|
64
|
|
|
{ |
|
65
|
|
|
$this->addCondition(__METHOD__, [$allowedTypes], []); |
|
66
|
|
|
|
|
67
|
|
|
return $this; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param AbstractValidator $validator |
|
72
|
|
|
* |
|
73
|
|
|
* @return $this |
|
74
|
|
|
*/ |
|
75
|
|
|
public function hasFileNameFormat(AbstractValidator $validator) |
|
76
|
|
|
{ |
|
77
|
|
|
$this->addCondition(__METHOD__, [$validator]); |
|
78
|
|
|
|
|
79
|
|
|
return $this; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param string $uploadDir |
|
84
|
|
|
* |
|
85
|
|
|
* @return $this |
|
86
|
|
|
*/ |
|
87
|
|
|
public function hasValidUploadDirectory($uploadDir) |
|
88
|
|
|
{ |
|
89
|
|
|
$this->addCondition(__METHOD__, [$uploadDir]); |
|
90
|
|
|
|
|
91
|
|
|
return $this; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @param string $uploadDir |
|
96
|
|
|
* |
|
97
|
|
|
* @return $this |
|
98
|
|
|
*/ |
|
99
|
|
|
public function notOverwritingExistingFile($uploadDir) |
|
100
|
|
|
{ |
|
101
|
|
|
$this->addCondition(__METHOD__, [$uploadDir]); |
|
102
|
|
|
|
|
103
|
|
|
return $this; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @param integer $length |
|
108
|
|
|
* |
|
109
|
|
|
* @return $this |
|
110
|
|
|
*/ |
|
111
|
|
|
public function hasLength($length) |
|
112
|
|
|
{ |
|
113
|
|
|
$this->addCondition(__METHOD__, [$length], ['size' => $length]); |
|
114
|
|
|
|
|
115
|
|
|
return $this; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @return $this |
|
120
|
|
|
*/ |
|
121
|
|
|
public function isImage() |
|
122
|
|
|
{ |
|
123
|
|
|
$this->addCondition(__METHOD__); |
|
124
|
|
|
|
|
125
|
|
|
return $this; |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|