1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Quantum PHP Framework |
5
|
|
|
* |
6
|
|
|
* An open source software development framework for PHP |
7
|
|
|
* |
8
|
|
|
* @package Quantum |
9
|
|
|
* @author Arman Ag. <[email protected]> |
10
|
|
|
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) |
11
|
|
|
* @link http://quantum.softberg.org/ |
12
|
|
|
* @since 2.9.8 |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Quantum\Libraries\Validation\Traits; |
16
|
|
|
|
17
|
|
|
use Quantum\Libraries\Storage\Exceptions\FileUploadException; |
18
|
|
|
use Quantum\Libraries\Storage\UploadedFile; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Trait File |
22
|
|
|
* @package Quantum\Libraries\Validation\Rules |
23
|
|
|
*/ |
24
|
|
|
trait File |
25
|
|
|
{ |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Validates file size |
29
|
|
|
* @param string $field |
30
|
|
|
* @param UploadedFile $file |
31
|
|
|
* @param int|null $maxSize |
32
|
|
|
* @param int|null $minSize |
33
|
|
|
* @return bool |
34
|
|
|
*/ |
35
|
|
|
public function fileSize(string $field, UploadedFile $file, int $maxSize, ?int $minSize = null): bool |
|
|
|
|
36
|
|
|
{ |
37
|
|
|
$size = $file->getSize(); |
38
|
|
|
$minSize = $minSize ?? 0; |
39
|
|
|
|
40
|
|
|
return $size >= $minSize && $size <= $maxSize; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Validates file mime type |
45
|
|
|
* @param string $field |
46
|
|
|
* @param UploadedFile $file |
47
|
|
|
* @param string ...$mimeTypes |
48
|
|
|
* @return bool |
49
|
|
|
*/ |
50
|
|
|
public function fileMimeType(string $field, UploadedFile $file, string ...$mimeTypes): bool |
|
|
|
|
51
|
|
|
{ |
52
|
|
|
return in_array($file->getMimetype(), $mimeTypes); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Validates file extension |
57
|
|
|
* @param string $field |
58
|
|
|
* @param UploadedFile $file |
59
|
|
|
* @param string ...$extensions |
60
|
|
|
* @return bool |
61
|
|
|
*/ |
62
|
|
|
public function fileExtension(string $field, UploadedFile $file, string ...$extensions): bool |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
return in_array($file->getExtension(), $extensions); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Validates image dimensions |
69
|
|
|
* @param string $field |
70
|
|
|
* @param UploadedFile $file |
71
|
|
|
* @param int|null $width |
72
|
|
|
* @param int|null $height |
73
|
|
|
* @return bool |
74
|
|
|
* @throws FileUploadException |
75
|
|
|
*/ |
76
|
|
|
public function imageDimensions(string $field, UploadedFile $file, ?int $width = null, ?int $height = null): bool |
|
|
|
|
77
|
|
|
{ |
78
|
|
|
$dimensions = $file->getDimensions(); |
79
|
|
|
|
80
|
|
|
if (empty($dimensions)) { |
81
|
|
|
return true; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
if ($width !== null && $dimensions['width'] != $width) { |
85
|
|
|
return false; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
if ($height !== null && $dimensions['height'] != $height) { |
89
|
|
|
return false; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return true; |
93
|
|
|
} |
94
|
|
|
} |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.