1 | <?php |
||
20 | class UploadedFile |
||
21 | { |
||
22 | |||
23 | const REQUIRED = 'required'; |
||
24 | const FILE_EXTENSION = 'fileExtension'; |
||
25 | const FILE_MEDIA = 'fileMedia'; |
||
26 | const SIZE_MAX = 'sizeMax'; |
||
27 | const SIZE_MIN = 'sizeMin'; |
||
28 | |||
29 | protected $rules = array( |
||
30 | self::REQUIRED, |
||
31 | self::FILE_EXTENSION, |
||
32 | self::FILE_MEDIA, |
||
33 | self::SIZE_MAX, |
||
34 | self::SIZE_MIN, |
||
35 | ); |
||
36 | |||
37 | |||
38 | /** |
||
39 | * |
||
40 | * Validates the presenece and properites if a PSR7 FileUploadInterface |
||
41 | * |
||
42 | * valid options: |
||
43 | * - required : bool require file is uploaded |
||
44 | * - fileExtension : array or strign of acceptable file extensions |
||
45 | * - fileMedia : array or strign of acceptable file media types |
||
46 | * - sizeMax : bytes or human readable string |
||
47 | * - sizeMin : bytes or human readable string |
||
48 | * |
||
49 | * @param object $subject The subject to be filtered. |
||
50 | * |
||
51 | * @param string $field The subject field name. |
||
52 | * |
||
53 | * @return bool True if valid, false if not. |
||
54 | * |
||
55 | */ |
||
56 | 25 | public function __invoke($subject, $field, array $options) |
|
76 | |||
77 | /** |
||
78 | * If the file required to be uploaded? |
||
79 | * |
||
80 | * @param array $options the options passed tot the filter |
||
81 | * |
||
82 | * @return bool |
||
83 | * |
||
84 | * @access protected |
||
85 | */ |
||
86 | 25 | protected function isRequired(array $options) |
|
93 | |||
94 | /** |
||
95 | * Is value an instance of a PSR7 UploadedFileInterface? |
||
96 | * |
||
97 | * @param mixed $value value being validated |
||
98 | * |
||
99 | * @return bool |
||
100 | * |
||
101 | * @access protected |
||
102 | */ |
||
103 | 25 | protected function isUploadedFile($value) |
|
107 | |||
108 | /** |
||
109 | * Was a file uploaded ? |
||
110 | * |
||
111 | * @param mixed $value value being validated |
||
112 | * |
||
113 | * @return bool |
||
114 | * |
||
115 | * @access protected |
||
116 | */ |
||
117 | 25 | protected function fileWasUploaded($value) |
|
129 | |||
130 | /** |
||
131 | * Required Rule Test |
||
132 | * |
||
133 | * @param mixed $value value being validated |
||
134 | * @param bool $option if the file is required |
||
135 | * |
||
136 | * @return bool |
||
137 | * |
||
138 | * @access protected |
||
139 | */ |
||
140 | 4 | protected function required($value, $option) |
|
148 | |||
149 | /** |
||
150 | * FileExtension Rule Test |
||
151 | * |
||
152 | * @param mixed $value value being validated |
||
153 | * @param string|array $exts array or string of valid extensions |
||
154 | * |
||
155 | * @return bool |
||
156 | * |
||
157 | * @access protected |
||
158 | */ |
||
159 | 4 | protected function fileExtension($value, $exts) |
|
165 | |||
166 | /** |
||
167 | * FileMedia Rule Test |
||
168 | * |
||
169 | * @param mixed $value value being validated |
||
170 | * @param string|array $medias array or string of valid media types |
||
171 | * |
||
172 | * @return bool |
||
173 | * |
||
174 | * @access protected |
||
175 | */ |
||
176 | 4 | protected function fileMedia($value, $medias) |
|
181 | |||
182 | /** |
||
183 | * SizeMax Rule Test |
||
184 | * |
||
185 | * @param mixed $value value being validated |
||
186 | * @param mixed $max maximum file size |
||
187 | * |
||
188 | * @return bool |
||
189 | * |
||
190 | * @access protected |
||
191 | */ |
||
192 | 4 | protected function sizeMax($value, $max) |
|
197 | |||
198 | /** |
||
199 | * SizeMin |
||
200 | * |
||
201 | * @param mixed $value value being validated |
||
202 | * @param mixed $min minimum file size |
||
203 | * |
||
204 | * @return mixed |
||
205 | * |
||
206 | * @access protected |
||
207 | */ |
||
208 | 4 | protected function sizeMin($value, $min) |
|
213 | |||
214 | /** |
||
215 | * Parse human readable size to bytes |
||
216 | * |
||
217 | * @param mixed $size size indicator |
||
218 | * |
||
219 | * @return double|int |
||
220 | * |
||
221 | * @access public |
||
222 | */ |
||
223 | 9 | public function parseHumanSize($size) |
|
241 | } |
||
242 |