|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace HustleWorks\Chute; |
|
4
|
|
|
|
|
5
|
|
|
use HustleWorks\Chute\DTO\ImageFile; |
|
6
|
|
|
use HustleWorks\Chute\DTO\ImageRuleConfiguration; |
|
7
|
|
|
use HustleWorks\Chute\Contracts\ImageValidatorInterface; |
|
8
|
|
|
|
|
9
|
|
|
class ImageValidator implements ImageValidatorInterface |
|
10
|
|
|
{ |
|
11
|
|
|
private $reasons_to_fail = []; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @param ImageFile $file |
|
15
|
|
|
* @param ImageRuleConfiguration $rules |
|
16
|
|
|
* @return \HustleWorks\Chute\ServiceResponse |
|
17
|
|
|
*/ |
|
18
|
|
|
public function validate(ImageFile $file, ImageRuleConfiguration $rules): ServiceResponse |
|
19
|
|
|
{ |
|
20
|
|
|
$this->_checkWidth($file, $rules); |
|
21
|
|
|
$this->_checkHeight($file, $rules); |
|
22
|
|
|
$this->_checkType($file, $rules); |
|
23
|
|
|
|
|
24
|
|
|
return new StandardServiceResponse( |
|
25
|
|
|
!sizeof($this->reasons_to_fail), |
|
26
|
|
|
[], |
|
27
|
|
|
sizeof($this->reasons_to_fail) ? "Message was invalid for the following reasons: " . implode(', ', $this->reasons_to_fail) : "Valid image." |
|
28
|
|
|
); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
private function _checkWidth(ImageFile $file, ImageRuleConfiguration $rules) |
|
32
|
|
|
{ |
|
33
|
|
|
if ($file->width < $rules->min_width) { |
|
34
|
|
|
$this->reasons_to_fail[] = 'image too narrow'; |
|
35
|
|
|
} elseif ($file->width > ($rules->max_width ?? PHP_INT_MAX)) { |
|
36
|
|
|
$this->reasons_to_fail[] = 'image too wide'; |
|
37
|
|
|
} |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
private function _checkHeight(ImageFile $file, ImageRuleConfiguration $rules) |
|
41
|
|
|
{ |
|
42
|
|
|
if ($file->height < $rules->min_height) { |
|
43
|
|
|
$this->reasons_to_fail[] = 'image too short'; |
|
44
|
|
|
} elseif ($file->height > ($rules->max_height ?? PHP_INT_MAX)) { |
|
45
|
|
|
$this->reasons_to_fail[] = 'image too tall'; |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
private function _checkType(ImageFile $file, ImageRuleConfiguration $rules) |
|
50
|
|
|
{ |
|
51
|
|
|
if ($rules->mime_types and !in_array($file->mime_type, $rules->mime_types)) { |
|
|
|
|
|
|
52
|
|
|
$this->reasons_to_fail[] = 'file type not allowed'; |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
} |
PHP has two types of connecting operators (logical operators, and boolean operators):
and&&or||The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&, or||.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
dieintroduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrowat this point:These limitations lead to logical operators rarely being of use in current PHP code.