Skip to content

Commit 8042ebc

Browse files
committed
Ensure that Validators' methods accept int and string
1 parent 5cba85e commit 8042ebc

4 files changed

Lines changed: 42 additions & 21 deletions

File tree

‎src/FilesValidator.php‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,14 @@ public static function uploaded(string $field, array $data = []) : bool
7070
*
7171
* @return bool
7272
*/
73-
public static function maxSize(string $field, array $data, int $kilobytes) : bool
73+
public static function maxSize(string $field, array $data, int | string $kilobytes) : bool
7474
{
7575
$uploaded = static::uploaded($field);
7676
if (!$uploaded) {
7777
return false;
7878
}
7979
$file = static::getFile($field);
80+
$kilobytes = (int) $kilobytes;
8081
return $file['size'] <= ($kilobytes * 1024);
8182
}
8283

@@ -156,15 +157,15 @@ public static function image(string $field, array $data = []) : bool
156157
*
157158
* @return bool
158159
*/
159-
public static function maxDim(string $field, array $data, int $width, int $height) : bool
160+
public static function maxDim(string $field, array $data, int | string $width, int | string $height) : bool
160161
{
161162
$isImage = static::image($field);
162163
if (!$isImage) {
163164
return false;
164165
}
165166
$file = static::getFile($field);
166167
$sizes = \getimagesize($file['tmp_name']);
167-
return !($sizes === false || $sizes[0] > $width || $sizes[1] > $height);
168+
return !($sizes === false || $sizes[0] > (int) $width || $sizes[1] > (int) $height);
168169
}
169170

170171
/**
@@ -177,15 +178,15 @@ public static function maxDim(string $field, array $data, int $width, int $heigh
177178
*
178179
* @return bool
179180
*/
180-
public static function minDim(string $field, array $data, int $width, int $height) : bool
181+
public static function minDim(string $field, array $data, int | string $width, int | string $height) : bool
181182
{
182183
$isImage = static::image($field);
183184
if (!$isImage) {
184185
return false;
185186
}
186187
$file = static::getFile($field);
187188
$sizes = \getimagesize($file['tmp_name']);
188-
return !($sizes === false || $sizes[0] < $width || $sizes[1] < $height);
189+
return !($sizes === false || $sizes[0] < (int) $width || $sizes[1] < (int) $height);
189190
}
190191

191192
/**
@@ -198,14 +199,14 @@ public static function minDim(string $field, array $data, int $width, int $heigh
198199
*
199200
* @return bool
200201
*/
201-
public static function dim(string $field, array $data, int $width, int $height) : bool
202+
public static function dim(string $field, array $data, int | string $width, int | string $height) : bool
202203
{
203204
$isImage = static::image($field);
204205
if (!$isImage) {
205206
return false;
206207
}
207208
$file = static::getFile($field);
208209
$sizes = \getimagesize($file['tmp_name']);
209-
return !($sizes === false || $sizes[0] !== $width || $sizes[1] !== $height);
210+
return !($sizes === false || $sizes[0] !== (int) $width || $sizes[1] !== (int) $height);
210211
}
211212
}

‎src/Validator.php‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ public static function between(
267267
int | string $max
268268
) : bool {
269269
$data = static::getData($field, $data);
270-
return $data !== null && $data >= $min && $data <= $max;
270+
return $data !== null && $data >= (string) $min && $data <= (string) $max;
271271
}
272272

273273
/**
@@ -299,10 +299,10 @@ public static function notBetween(
299299
*
300300
* @return bool
301301
*/
302-
public static function in(string $field, array $data, string $in, string ...$others) : bool
302+
public static function in(string $field, array $data, int | string $in, int | string ...$others) : bool
303303
{
304304
$data = static::getData($field, $data);
305-
return $data !== null && \in_array($data, [$in, ...$others], true);
305+
return $data !== null && \in_array($data, [$in, ...$others]);
306306
}
307307

308308
/**
@@ -315,7 +315,7 @@ public static function in(string $field, array $data, string $in, string ...$oth
315315
*
316316
* @return bool
317317
*/
318-
public static function notIn(string $field, array $data, string $notIn, string ...$others) : bool
318+
public static function notIn(string $field, array $data, int | string $notIn, int | string ...$others) : bool
319319
{
320320
return !static::in($field, $data, $notIn, ...$others);
321321
}
@@ -435,7 +435,7 @@ public static function greater(
435435
int | string $greaterThan
436436
) : bool {
437437
$data = static::getData($field, $data);
438-
return $data !== null && $data > $greaterThan;
438+
return $data !== null && $data > (string) $greaterThan;
439439
}
440440

441441
/**
@@ -453,7 +453,7 @@ public static function greaterOrEqual(
453453
int | string $greaterThanOrEqualTo
454454
) : bool {
455455
$data = static::getData($field, $data);
456-
return $data !== null && $data >= $greaterThanOrEqualTo;
456+
return $data !== null && $data >= (string) $greaterThanOrEqualTo;
457457
}
458458

459459
/**
@@ -471,7 +471,7 @@ public static function less(
471471
int | string $lessThan
472472
) : bool {
473473
$data = static::getData($field, $data);
474-
return $data !== null && $data < $lessThan;
474+
return $data !== null && $data < (string) $lessThan;
475475
}
476476

477477
/**
@@ -489,7 +489,7 @@ public static function lessOrEqual(
489489
int | string $lessThanOrEqualTo
490490
) : bool {
491491
$data = static::getData($field, $data);
492-
return $data !== null && $data <= $lessThanOrEqualTo;
492+
return $data !== null && $data <= (string) $lessThanOrEqualTo;
493493
}
494494

495495
/**

‎tests/FilesValidatorTest.php‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php declare(strict_types=1);
22
/*
33
* This file is part of Aplus Framework Validation Library.
44
*
@@ -104,7 +104,9 @@ public function testUploaded() : void
104104
public function testMaxSize() : void
105105
{
106106
self::assertFalse(FilesValidatorMock::maxSize('foo[a]', [], 10));
107+
self::assertFalse(FilesValidatorMock::maxSize('foo[a]', [], '10'));
107108
self::assertTrue(FilesValidatorMock::maxSize('foo[a]', [], 40));
109+
self::assertTrue(FilesValidatorMock::maxSize('foo[a]', [], '40'));
108110
self::assertFalse(FilesValidator::maxSize('foo[a]', [], 40));
109111
}
110112

@@ -135,21 +137,27 @@ public function testImage() : void
135137
public function testMaxDimensions() : void
136138
{
137139
self::assertFalse(FilesValidatorMock::maxDim('foo[a]', [], 200, 200));
140+
self::assertFalse(FilesValidatorMock::maxDim('foo[a]', [], '200', '200'));
138141
self::assertTrue(FilesValidatorMock::maxDim('foo[a]', [], 400, 500));
142+
self::assertTrue(FilesValidatorMock::maxDim('foo[a]', [], '400', '500'));
139143
self::assertFalse(FilesValidator::maxDim('foo[a]', [], 400, 500));
140144
}
141145

142146
public function testMinDimensions() : void
143147
{
144148
self::assertFalse(FilesValidatorMock::minDim('foo[a]', [], 500, 500));
149+
self::assertFalse(FilesValidatorMock::minDim('foo[a]', [], '500', '500'));
145150
self::assertTrue(FilesValidatorMock::minDim('foo[a]', [], 300, 400));
151+
self::assertTrue(FilesValidatorMock::minDim('foo[a]', [], '300', '400'));
146152
self::assertFalse(FilesValidator::minDim('foo[a]', [], 300, 500));
147153
}
148154

149155
public function testDimensions() : void
150156
{
151157
self::assertFalse(FilesValidatorMock::dim('foo[a]', [], 500, 500));
158+
self::assertFalse(FilesValidatorMock::dim('foo[a]', [], '500', '500'));
152159
self::assertTrue(FilesValidatorMock::dim('foo[a]', [], 400, 400));
160+
self::assertTrue(FilesValidatorMock::dim('foo[a]', [], '400', '400'));
153161
self::assertFalse(FilesValidator::dim('foo[a]', [], 300, 500));
154162
}
155163
}

‎tests/ValidatorTest.php‎

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php declare(strict_types=1);
22
/*
33
* This file is part of Aplus Framework Validation Library.
44
*
@@ -136,24 +136,30 @@ public function testJson() : void
136136
public function testMaxLength() : void
137137
{
138138
self::assertTrue(Validator::maxLength('alpha', $this->array, 3));
139+
self::assertTrue(Validator::maxLength('alpha', $this->array, '3'));
139140
self::assertTrue(Validator::maxLength('number', $this->array, 3));
140141
self::assertFalse(Validator::maxLength('alphaNumber', $this->array, 3));
142+
self::assertFalse(Validator::maxLength('alphaNumber', $this->array, '3'));
141143
self::assertFalse(Validator::maxLength('unknown', $this->array, 3));
142144
}
143145

144146
public function testMinLength() : void
145147
{
146148
self::assertTrue(Validator::minLength('alpha', $this->array, 3));
149+
self::assertTrue(Validator::minLength('alpha', $this->array, '3'));
147150
self::assertTrue(Validator::minLength('number', $this->array, 3));
148151
self::assertFalse(Validator::minLength('alpha', $this->array, 4));
152+
self::assertFalse(Validator::minLength('alpha', $this->array, '4'));
149153
self::assertFalse(Validator::minLength('unknown', $this->array, 3));
150154
}
151155

152156
public function testLength() : void
153157
{
154158
self::assertTrue(Validator::length('alpha', $this->array, 3));
159+
self::assertTrue(Validator::length('alpha', $this->array, '3'));
155160
self::assertTrue(Validator::length('number', $this->array, 3));
156161
self::assertFalse(Validator::length('alpha', $this->array, 4));
162+
self::assertFalse(Validator::length('alpha', $this->array, '4'));
157163
self::assertFalse(Validator::length('unknown', $this->array, 3));
158164
}
159165

@@ -216,36 +222,40 @@ public function testBetween() : void
216222
{
217223
self::assertTrue(Validator::between('alpha', $this->array, 'a', 'b'));
218224
self::assertTrue(Validator::between('number', $this->array, 120, 123));
225+
self::assertTrue(Validator::between('number', $this->array, '120', '123'));
219226
self::assertFalse(Validator::between('alpha', $this->array, 'b', 'c'));
220227
self::assertFalse(Validator::between('unknown', $this->array, 1, 2));
228+
self::assertFalse(Validator::between('unknown', $this->array, '1', '2'));
221229
}
222230

223231
public function testNotBetween() : void
224232
{
225233
self::assertFalse(Validator::notBetween('alpha', $this->array, 'a', 'b'));
226234
self::assertFalse(Validator::notBetween('number', $this->array, 120, 123));
235+
self::assertFalse(Validator::notBetween('number', $this->array, '120', '123'));
227236
self::assertTrue(Validator::notBetween('alpha', $this->array, 'b', 'c'));
228237
self::assertTrue(Validator::notBetween('unknown', $this->array, 1, 2));
238+
self::assertTrue(Validator::notBetween('unknown', $this->array, '1', '2'));
229239
}
230240

231241
public function testIn() : void
232242
{
233243
self::assertTrue(Validator::in('alpha', $this->array, 'a', 'abc', 'def'));
234-
// @phpstan-ignore-next-line
235244
self::assertTrue(Validator::in('number', $this->array, 120, 123, 456));
245+
self::assertTrue(Validator::in('number', $this->array, '120', '123', '456'));
236246
self::assertFalse(Validator::in('alpha', $this->array, 'b', 'c', 'd'));
237-
// @phpstan-ignore-next-line
238247
self::assertFalse(Validator::in('unknown', $this->array, 1, 2, 3));
248+
self::assertFalse(Validator::in('unknown', $this->array, '1', '2', '3'));
239249
}
240250

241251
public function testNotIn() : void
242252
{
243253
self::assertFalse(Validator::notIn('alpha', $this->array, 'a', 'abc', 'def'));
244-
// @phpstan-ignore-next-line
245254
self::assertFalse(Validator::notIn('number', $this->array, 120, 123, 456));
255+
self::assertFalse(Validator::notIn('number', $this->array, '120', '123', '456'));
246256
self::assertTrue(Validator::notIn('alpha', $this->array, 'b', 'c', 'd'));
247-
// @phpstan-ignore-next-line
248257
self::assertTrue(Validator::notIn('unknown', $this->array, 1, 2, 3));
258+
self::assertTrue(Validator::notIn('unknown', $this->array, '1', '2', '3'));
249259
}
250260

251261
public function testLatin() : void
@@ -262,8 +272,10 @@ public function testIp() : void
262272
self::assertTrue(Validator::ip('ipv4', $this->array));
263273
self::assertTrue(Validator::ip('ipv6', $this->array));
264274
self::assertTrue(Validator::ip('ipv4', $this->array, 4));
275+
self::assertTrue(Validator::ip('ipv4', $this->array, '4'));
265276
self::assertTrue(Validator::ip('ipv6', $this->array, 6));
266277
self::assertFalse(Validator::ip('ipv4', $this->array, 6));
278+
self::assertFalse(Validator::ip('ipv4', $this->array, '6'));
267279
self::assertFalse(Validator::ip('ipv6', $this->array, 4));
268280
self::assertFalse(Validator::ip('unknown', $this->array));
269281
$this->expectException(\InvalidArgumentException::class);

0 commit comments

Comments
 (0)