|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright 2019 Amin Yazdanpanah<http://www.aminyazdanpanah.com>. |
|
5
|
|
|
* |
|
6
|
|
|
* Licensed under the MIT License; |
|
7
|
|
|
* you may not use this file except in compliance with the License. |
|
8
|
|
|
* You may obtain a copy of the License at |
|
9
|
|
|
* |
|
10
|
|
|
* https://opensource.org/licenses/MIT |
|
11
|
|
|
* |
|
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15
|
|
|
* See the License for the specific language governing permissions and |
|
16
|
|
|
* limitations under the License. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
namespace AYazdanpanah\SaveUploadedFiles; |
|
20
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
use AYazdanpanah\SaveUploadedFiles\Exception\Exception; |
|
23
|
|
|
|
|
24
|
|
|
abstract class Validate implements ValidatorInterface |
|
25
|
|
|
{ |
|
26
|
|
|
private $file_size; |
|
27
|
|
|
private $file_extension; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @throws Exception |
|
31
|
|
|
*/ |
|
32
|
|
|
public function validate() |
|
33
|
|
|
{ |
|
34
|
|
|
if ($this->getMaxSize() <= $this->getFileSize()) { |
|
35
|
|
|
throw new Exception("The file size is " . intval($this->getFileSize() / 1024) . "KB! It must not be greater than " . intval($this->getMaxSize() / 1024) . "KB"); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
if ($this->getMinSize() >= $this->getFileSize()) { |
|
39
|
|
|
throw new Exception("The file size is " . intval($this->getFileSize() / 1024) . "KB! It must be at least " . intval($this->getMinSize() / 1024) . "KB"); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
if (!in_array(strtolower($this->getFileExtension()), $this->getTypes()) && !in_array("*", $this->getTypes())) { |
|
43
|
|
|
throw new Exception("Sorry, the \"" . $this->getFileExtension() . "\" files are not allowed! Only " . implode(", ", $this->getTypes()) . " files are allowed. "); |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param mixed $file_size |
|
49
|
|
|
* @return Validate |
|
50
|
|
|
*/ |
|
51
|
|
|
public function setFileSize($file_size) |
|
52
|
|
|
{ |
|
53
|
|
|
$this->file_size = $file_size; |
|
54
|
|
|
return $this; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @return mixed |
|
59
|
|
|
*/ |
|
60
|
|
|
public function getFileSize() |
|
61
|
|
|
{ |
|
62
|
|
|
return $this->file_size; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param mixed $file_extension |
|
67
|
|
|
* @return Validate |
|
68
|
|
|
*/ |
|
69
|
|
|
public function setFileExtension($file_extension) |
|
70
|
|
|
{ |
|
71
|
|
|
$this->file_extension = $file_extension; |
|
72
|
|
|
return $this; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @return mixed |
|
77
|
|
|
*/ |
|
78
|
|
|
public function getFileExtension() |
|
79
|
|
|
{ |
|
80
|
|
|
return $this->file_extension; |
|
81
|
|
|
} |
|
82
|
|
|
} |