Skip to content

Commit

Permalink
Merge pull request #12 from BlakvGhost/update-max-length-algo-to-work…
Browse files Browse the repository at this point in the history
…-with-file-and-array-type

Update max length algo to work with file and array type
  • Loading branch information
BlakvGhost authored Nov 22, 2023
2 parents 79a7978 + 266981e commit d57663e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ PHPValidator provides a variety of predefined rules that you can use for data va
- Validates that a field's value is not in a specified set.

```php
'value' => 'not_in:["foo", "bar"]'
'value' => 'not_in:foo,bar'
```
20. **Required With Rule**
- Requires the field to be present if another specified field is present.
Expand Down
25 changes: 24 additions & 1 deletion src/Rules/MaxLengthRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,30 @@ public function passes(string $field, $value, array $data): bool
$maxLength = $this->parameters[0] ?? 0;

// Check if the value is a string and its length is within the specified maximum.
return is_string($value) && mb_strlen($value) <= $maxLength;
if (is_string($value)) {
$value = mb_strlen($value);
}

if (is_array($value)) {
$value = count($value);
}

if (is_file($value)) {

if (isset($_FILES[$value]) && $_FILES[$value]["error"] == 0) {
// Get the file size in bytes
$size = $_FILES[$value]["size"];

// Convert bytes to kilobytes
$size_kb = $size / 1024; // kilobytes

$value = $size_kb;
}

return false;
}

return $value <= $maxLength;
}

/**
Expand Down

0 comments on commit d57663e

Please sign in to comment.