Skip to content

Commit

Permalink
Added isValid()
Browse files Browse the repository at this point in the history
Signed-off-by: Tom Wright <[email protected]>
  • Loading branch information
betterthanclay committed Oct 30, 2023
1 parent 060829d commit d8001ea
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
* Added isValid()

## v0.1.3 (2023-09-26)
* Migrated to use effigy in CI workflow
* Fixed PHP8.1 testing
Expand Down
23 changes: 23 additions & 0 deletions src/Ip.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,29 @@ public static function parse(
return new static($ip, $isV6);
}

/**
* Validate IP or return null
*/
public static function isValid(
Ip|string|int|BigInteger $ip,
): bool {
if ($ip instanceof Ip) {
return true;
}

if (is_string($ip)) {
return filter_var($ip, FILTER_VALIDATE_IP) !== false;
}

if (is_int($ip)) {
$ip = BigInteger::of($ip);
}

return
!$ip->isLessThan(0) &&
!$ip->isGreaterThan(self::V6_MAX);
}

/**
* Init with IP string or int
*
Expand Down

0 comments on commit d8001ea

Please sign in to comment.