|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Author: Nil Portugués Calderó <[email protected]> |
|
4
|
|
|
* Date: 9/25/14 |
|
5
|
|
|
* Time: 10:53 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\Validation\FileUpload; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class FileUploadException |
|
15
|
|
|
* @package NilPortugues\Validator\Validation\FileUploadAttribute |
|
16
|
|
|
*/ |
|
17
|
|
|
class FileUploadException extends \Exception |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var array |
|
21
|
|
|
*/ |
|
22
|
|
|
private $errorMessages = [ |
|
23
|
|
|
UPLOAD_ERR_INI_SIZE => "FileUpload::UPLOAD_ERR_INI_SIZE", |
|
24
|
|
|
UPLOAD_ERR_FORM_SIZE => "FileUpload::UPLOAD_ERR_FORM_SIZE", |
|
25
|
|
|
UPLOAD_ERR_PARTIAL => "FileUpload::UPLOAD_ERR_PARTIAL", |
|
26
|
|
|
UPLOAD_ERR_NO_FILE => "FileUpload::UPLOAD_ERR_NO_FILE", |
|
27
|
|
|
UPLOAD_ERR_NO_TMP_DIR => "FileUpload::UPLOAD_ERR_NO_TMP_DIR", |
|
28
|
|
|
UPLOAD_ERR_CANT_WRITE => "FileUpload::UPLOAD_ERR_CANT_WRITE", |
|
29
|
|
|
UPLOAD_ERR_EXTENSION => "FileUpload::UPLOAD_ERR_EXTENSION", |
|
30
|
|
|
]; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param string $uploadName |
|
34
|
|
|
*/ |
|
35
|
|
|
public function __construct($uploadName) |
|
|
|
|
|
|
36
|
|
|
{ |
|
37
|
|
|
$error = 4; |
|
38
|
|
|
|
|
39
|
|
|
if (!empty($_FILES[$uploadName]['error']) && \is_int($_FILES[$uploadName]['error'])) { |
|
40
|
|
|
$error = $_FILES[$uploadName]['error']; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
if (!empty($_FILES[$uploadName]['error']) |
|
44
|
|
|
&& \is_array($_FILES[$uploadName]['error']) |
|
45
|
|
|
&& \is_int($_FILES[$uploadName]['error'][0]) |
|
46
|
|
|
) { |
|
47
|
|
|
$error = \reset($_FILES[$uploadName]['error']); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
$this->message = $this->errorMessages[$error]; |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: