forked from phalcon/cphalcon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Equal.zep
108 lines (93 loc) · 2.74 KB
/
Equal.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
/**
* This file is part of the Phalcon Framework.
*
* (c) Phalcon Team <team@phalconphp.com>
*
* 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\FileAbstract;
/**
* Phalcon\Validation\Validator\File\Resolution\Equal
*
* Checks if a file has the rigth resolution
*
* <code>
* use Phalcon\Validation;
* use Phalcon\Validation\Validator\File\Resolution\Equal;
*
* $validator = new Validation();
*
* $validator->add(
* "file",
* new Equal(
* [
* "resolution" => "800x600",
* "message" => "The resolution of the field :field has to be equal :resolution",
* ]
* )
* );
*
* $validator->add(
* [
* "file",
* "anotherFile",
* ],
* new Equal(
* [
* "resolution" => [
* "file" => "800x600",
* "anotherFile" => "1024x768",
* ],
* "message" => [
* "file" => "Equal resolution of file has to be 800x600",
* "anotherFile" => "Equal resolution of file has to be 1024x768",
* ],
* ]
* )
* );
* </code>
*/
class Equal extends FileAbstract
{
protected template = "The resolution of the field :field has to be equal :resolution";
/**
* Executes the validation
*/
public function validate(<Validation> validation, var field) -> bool
{
var height, equalHeight, equalWidth, resolution, resolutionArray,
tmp, value, width, replacePairs;
// Check file upload
if (this->checkUpload(validation, field) === false) {
return false;
}
let tmp = getimagesize(value["tmp_name"]),
width = tmp[0],
height = tmp[1],
value = validation->getValue(field);
let resolution = this->getOption("resolution");
if typeof resolution == "array" {
let resolution = resolution[field];
}
let resolutionArray = explode("x", resolution),
equalWidth = resolutionArray[0],
equalHeight = resolutionArray[1];
if typeof resolution == "array" {
let resolution = resolution[field];
}
if (width != equalWidth || height != equalHeight) {
let replacePairs = [
":resolution" : resolution
];
validation->appendMessage(
this->messageFactory(validation, field, replacePairs)
);
return false;
}
return true;
}
}