Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new validators #8

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
150 changes: 150 additions & 0 deletions src/IpAddressValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
<?php

namespace ipl\Validator;

use ipl\I18n\Translation;

class IpAddressValidator extends BaseValidator
{
use Translation;

/** @var bool if true, value must be a valid IPv4 address */
protected $shouldIpv4;

/** @var bool if true, value must be a valid IPv6 address */
protected $shouldIpv6;

/** @var bool if true, value must not be within a private range */
protected $shouldNotInPrivateRange;

/** @var bool if true, value must not be within a reserved range */
protected $shouldNotInReservedRange;

public function __construct(array $options = [])
{
if (isset($options['ipv4'])) {
$this->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;
}
}
112 changes: 112 additions & 0 deletions src/UrlValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php

namespace ipl\Validator;

use ipl\I18n\Translation;

class UrlValidator extends BaseValidator
{
use Translation;

/** @var bool Whether URL must have a path after the domain name to be valid */
protected $pathRequired;

/** @var bool Whether URL must have a query string to be valid */
protected $queryRequired;

/**
* Validates url
*
* Optional options:
*
* - path_required: (bool) If true, URL must have a path after the domain name (like www.example.com/ab1/),
* default false
*
* - query_required: (bool) If true, URL must have a query string (like "example.php?name=Foo&age=37"),
* default false
*
* @param array $options
*/
public function __construct(array $options = [])
{
$this->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;
}
}