/
Max.zep
140 lines (121 loc) · 3.47 KB
/
Max.zep
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/**
* This file is part of the Phalcon Framework.
*
* (c) Phalcon Team <team@phalcon.io>
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/
namespace Phalcon\Validation\Validator\File\Resolution;
use Phalcon\Messages\Message;
use Phalcon\Validation;
use Phalcon\Validation\Validator\File\AbstractFile;
/**
* Checks if a file has the rigth resolution
*
* ```php
* use Phalcon\Validation;
* use Phalcon\Validation\Validator\File\Resolution\Max;
*
* $validator = new Validation();
*
* $validator->add(
* "file",
* new Max(
* [
* "resolution" => "800x600",
* "message" => "Max resolution of :field is :resolution",
* "included" => true,
* ]
* )
* );
*
* $validator->add(
* [
* "file",
* "anotherFile",
* ],
* new Max(
* [
* "resolution" => [
* "file" => "800x600",
* "anotherFile" => "1024x768",
* ],
* "included" => [
* "file" => false,
* "anotherFile" => true,
* ],
* "message" => [
* "file" => "Max resolution of file is 800x600",
* "anotherFile" => "Max resolution of file is 1024x768",
* ],
* ]
* )
* );
* ```
*/
class Max extends AbstractFile
{
protected template = "File :field exceeds the maximum resolution of :resolution";
/**
* Constructor
*
* @param array options = [
* 'message' => '',
* 'template' => '',
* 'resolution' => '1000x1000',
* 'included' => false
* ]
*/
public function __construct(array! options = [])
{
parent::__construct(options);
}
/**
* Executes the validation
*/
public function validate(<Validation> validation, var field) -> bool
{
var height, maxHeight, maxWidth, resolution, resolutionArray,
tmp, value, width, replacePairs, included = false, result;
// Check file upload
if this->checkUpload(validation, field) === false {
return false;
}
let value = validation->getValue(field),
tmp = getimagesize(value["tmp_name"]),
width = tmp[0],
height = tmp[1];
let resolution = this->getOption("resolution");
if typeof resolution == "array" {
let resolution = resolution[field];
}
let resolutionArray = explode("x", resolution),
maxWidth = resolutionArray[0],
maxHeight = resolutionArray[1];
let included = this->getOption("included");
if typeof included == "array" {
let included = (bool) included[field];
} else {
let included = (bool) included;
}
if included {
let result = width >= maxWidth || height >= maxHeight;
} else {
let result = width > maxWidth || height > maxHeight;
}
if typeof resolution == "array" {
let resolution = resolution[field];
}
if result {
let replacePairs = [
":resolution" : resolution
];
validation->appendMessage(
this->messageFactory(validation, field, replacePairs)
);
return false;
}
return true;
}
}