diff --git a/src/IpAddressValidator.php b/src/IpAddressValidator.php new file mode 100644 index 0000000..617d934 --- /dev/null +++ b/src/IpAddressValidator.php @@ -0,0 +1,150 @@ +setShouldIpv4($options['ipv4']); + } elseif (isset($options['ipv6'])) { + $this->setShouldIpv6($options['ipv6']); + } + } + + /** + * @return bool + */ + public function getShouldIpv4(): bool + { + return $this->shouldIpv4; + } + + /** + * @param bool $shouldIpv4 + */ + public function setShouldIpv4($shouldIpv4 = true): void + { + $this->shouldIpv4 = (bool) $shouldIpv4; + } + + /** + * @return bool + */ + public function getShouldIpv6(): bool + { + return $this->shouldIpv6; + } + + /** + * @param bool $shouldIpv6 + */ + public function setShouldIpv6($shouldIpv6 = true): void + { + $this->shouldIpv6 = (bool) $shouldIpv6; + } + + /** + * @return bool + */ + public function getShouldNotInPrivateRange(): bool + { + return $this->shouldNotInPrivateRange; + } + + /** + * @param bool $shouldNotInPrivateRange + */ + public function setShouldNotInPrivateRange($shouldNotInPrivateRange = true): void + { + $this->shouldNotInPrivateRange = (bool) $shouldNotInPrivateRange; + } + + /** + * @return bool + */ + public function getShouldNotInReservedRange(): bool + { + return $this->shouldNotInReservedRange; + } + + /** + * @param bool $shouldNotInReservedRange + */ + public function setShouldNotInReservedRange($shouldNotInReservedRange = true): void + { + $this->shouldNotInReservedRange = (bool) $shouldNotInReservedRange; + } + + public function isValid($value) + { + // Multiple isValid() calls must not stack validation messages + $this->clearMessages(); + + if (! filter_var($value, FILTER_VALIDATE_IP)) { + $this->addMessage(sprintf( + $this->translate("%s is not a valid IP address"), + $value + )); + + return false; + } + + if ($this->getShouldIpv4() && ! filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { + $this->addMessage(sprintf( + $this->translate("%s is not a valid IPv4 address"), + $value + )); + + return false; + } + + if ($this->getShouldIpv6() && ! filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { + $this->addMessage(sprintf( + $this->translate("%s is not a valid IPv6 address"), + $value + )); + + return false; + } + + if ($this->getShouldNotInPrivateRange() + && ! filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE)) { + $this->addMessage(sprintf( + $this->translate("Ip address must not be within a private range"), + $value + )); + + return false; + } + + if ($this->getShouldNotInReservedRange() + && ! filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_NO_RES_RANGE)) { + $this->addMessage(sprintf( + $this->translate("Ip address must not be within a reserved range"), + $value + )); + + return false; + } + + return true; + } +} diff --git a/src/UrlValidator.php b/src/UrlValidator.php new file mode 100644 index 0000000..c9d48cf --- /dev/null +++ b/src/UrlValidator.php @@ -0,0 +1,112 @@ +setPathRequired($options['path_required'] ?? false) + ->setQueryRequired($options['query_required'] ?? false); + } + + /** + * Whether path is required + * + * @return bool + */ + public function isPathRequired(): bool + { + return $this->pathRequired; + } + + /** + * Set whether URL must have a path after the domain name to be valid + * + * @param bool $pathRequired + */ + public function setPathRequired($pathRequired = true): self + { + $this->pathRequired = (bool) $pathRequired; + + return $this; + } + + /** + * Whether query is required + * + * @return bool + */ + public function isQueryRequired(): bool + { + return $this->queryRequired; + } + + /** + * Set whether URL must have a query string to be valid + * + * @param bool $queryRequired + */ + public function setQueryRequired($queryRequired = true): void + { + $this->queryRequired = (bool) $queryRequired; + } + + public function isValid($value) + { + // Multiple isValid() calls must not stack validation messages + $this->clearMessages(); + + if (! filter_var($value, FILTER_VALIDATE_URL)) { + $this->addMessage(sprintf( + $this->translate("'%s' is not a valid url"), + $value + )); + + return false; + } + + if ($this->isPathRequired() && ! filter_var($value, FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED)) { + $this->addMessage(sprintf( + $this->translate("Url must contain a path after the domain name"), + $value + )); + + return false; + } + + if ($this->isQueryRequired() && ! filter_var($value, FILTER_VALIDATE_URL, FILTER_FLAG_QUERY_REQUIRED)) { + $this->addMessage(sprintf( + $this->translate("Url must contain a query string"), + $value + )); + + return false; + } + + return true; + } +}