Skip to content

Commit

Permalink
Add port validation rule for #68
Browse files Browse the repository at this point in the history
  • Loading branch information
lancepioch committed May 20, 2024
1 parent 53a5ff6 commit ae399f9
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions app/Rules/Port.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace App\Rules;

use Closure;
use Illuminate\Contracts\Validation\ValidationRule;

class Port implements ValidationRule
{
/**
* Run the validation rule.
*
* @param \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail
*/
public function validate(string $attribute, mixed $value, Closure $fail): void
{
if (!is_numeric($value)) {
$fail('The :attribute must be numeric.');
}

$value = intval($value);
if (floatval($value) !== (float) $value) {
$fail('The :attribute must be an integer.');
}

if ($value < 0) {
$fail('The :attribute must be greater or equal to 0.');
}

if ($value > 65535) {
$fail('The :attribute must be less or equal to 65535.');
}
}
}

0 comments on commit ae399f9

Please sign in to comment.