Skip to content

Commit d57663e

Browse files
authored
Merge pull request #12 from BlakvGhost/update-max-length-algo-to-work-with-file-and-array-type
Update max length algo to work with file and array type
2 parents 79a7978 + 266981e commit d57663e

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ PHPValidator provides a variety of predefined rules that you can use for data va
220220
- Validates that a field's value is not in a specified set.
221221

222222
```php
223-
'value' => 'not_in:["foo", "bar"]'
223+
'value' => 'not_in:foo,bar'
224224
```
225225
20. **Required With Rule**
226226
- Requires the field to be present if another specified field is present.

src/Rules/MaxLengthRule.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,30 @@ public function passes(string $field, $value, array $data): bool
5050
$maxLength = $this->parameters[0] ?? 0;
5151

5252
// Check if the value is a string and its length is within the specified maximum.
53-
return is_string($value) && mb_strlen($value) <= $maxLength;
53+
if (is_string($value)) {
54+
$value = mb_strlen($value);
55+
}
56+
57+
if (is_array($value)) {
58+
$value = count($value);
59+
}
60+
61+
if (is_file($value)) {
62+
63+
if (isset($_FILES[$value]) && $_FILES[$value]["error"] == 0) {
64+
// Get the file size in bytes
65+
$size = $_FILES[$value]["size"];
66+
67+
// Convert bytes to kilobytes
68+
$size_kb = $size / 1024; // kilobytes
69+
70+
$value = $size_kb;
71+
}
72+
73+
return false;
74+
}
75+
76+
return $value <= $maxLength;
5477
}
5578

5679
/**

0 commit comments

Comments
 (0)