Skip to content

Commit

Permalink
refactor: composition with NonStrictFormatRules
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Jan 6, 2022
1 parent c49622f commit f18cb61
Showing 1 changed file with 34 additions and 104 deletions.
138 changes: 34 additions & 104 deletions system/Validation/StrictRules/FormatRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,23 @@

namespace CodeIgniter\Validation\StrictRules;

use DateTime;
use CodeIgniter\Validation\FormatRules as NonStrictFormatRules;

/**
* Format validation Rules.
*/
class FormatRules
{
/**
* @var NonStrictFormatRules
*/
private $nonStrictFormatRules;

public function __construct()
{
$this->nonStrictFormatRules = new NonStrictFormatRules();
}

/**
* Alpha
*
Expand All @@ -31,7 +41,7 @@ public function alpha($str = null): bool
return false;
}

return ctype_alpha($str);
return $this->nonStrictFormatRules->alpha($str);
}

/**
Expand All @@ -51,8 +61,7 @@ public function alpha_space(?string $value = null): bool
return false;
}

// @see https://regex101.com/r/LhqHPO/1
return (bool) preg_match('/\A[A-Z ]+\z/i', $value);
return $this->nonStrictFormatRules->alpha_space($value);
}

/**
Expand All @@ -74,8 +83,7 @@ public function alpha_dash($str = null): bool
return false;
}

// @see https://regex101.com/r/XfVY3d/1
return preg_match('/\A[a-z0-9_-]+\z/i', $str) === 1;
return $this->nonStrictFormatRules->alpha_dash($str);
}

/**
Expand Down Expand Up @@ -103,8 +111,7 @@ public function alpha_numeric_punct($str)
return false;
}

// @see https://regex101.com/r/6N8dDY/1
return preg_match('/\A[A-Z0-9 ~!#$%\&\*\-_+=|:.]+\z/i', $str) === 1;
return $this->nonStrictFormatRules->alpha_numeric_punct($str);
}

/**
Expand All @@ -122,7 +129,7 @@ public function alpha_numeric($str = null): bool
return false;
}

return ctype_alnum($str);
return $this->nonStrictFormatRules->alpha_numeric($str);
}

/**
Expand All @@ -140,8 +147,7 @@ public function alpha_numeric_space($str = null): bool
return false;
}

// @see https://regex101.com/r/0AZDME/1
return (bool) preg_match('/\A[A-Z0-9 ]+\z/i', $str ?? '');
return $this->nonStrictFormatRules->alpha_numeric_space($str);
}

/**
Expand All @@ -151,7 +157,7 @@ public function alpha_numeric_space($str = null): bool
*/
public function string($str = null): bool
{
return is_string($str);
return $this->nonStrictFormatRules->string($str);
}

/**
Expand All @@ -169,8 +175,7 @@ public function decimal($str = null): bool
return false;
}

// @see https://regex101.com/r/HULifl/2/
return (bool) preg_match('/\A[-+]?\d{0,}\.?\d+\z/', $str ?? '');
return $this->nonStrictFormatRules->decimal($str);
}

/**
Expand All @@ -188,7 +193,7 @@ public function hex($str = null): bool
return false;
}

return ctype_xdigit($str);
return $this->nonStrictFormatRules->hex($str);
}

/**
Expand All @@ -206,7 +211,7 @@ public function integer($str = null): bool
return false;
}

return (bool) preg_match('/\A[\-+]?\d+\z/', $str);
return $this->nonStrictFormatRules->integer($str);
}

/**
Expand All @@ -224,7 +229,7 @@ public function is_natural($str = null): bool
return false;
}

return ctype_digit($str);
return $this->nonStrictFormatRules->is_natural($str);
}

/**
Expand All @@ -242,7 +247,7 @@ public function is_natural_no_zero($str = null): bool
return false;
}

return $str !== '0' && ctype_digit($str);
return $this->nonStrictFormatRules->is_natural_no_zero($str);
}

/**
Expand All @@ -260,8 +265,7 @@ public function numeric($str = null): bool
return false;
}

// @see https://regex101.com/r/bb9wtr/2
return (bool) preg_match('/\A[\-+]?\d*\.?\d+\z/', $str ?? '');
return $this->nonStrictFormatRules->numeric($str);
}

/**
Expand All @@ -275,11 +279,7 @@ public function regex_match($str, string $pattern): bool
return false;
}

if (strpos($pattern, '/') !== 0) {
$pattern = "/{$pattern}/";
}

return (bool) preg_match($pattern, $str ?? '');
return $this->nonStrictFormatRules->regex_match($str, $pattern);
}

/**
Expand All @@ -296,7 +296,7 @@ public function timezone($str = null): bool
return false;
}

return in_array($str, timezone_identifiers_list(), true);
return $this->nonStrictFormatRules->timezone($str);
}

/**
Expand All @@ -313,7 +313,7 @@ public function valid_base64($str = null): bool
return false;
}

return base64_encode(base64_decode($str, true)) === $str;
return $this->nonStrictFormatRules->valid_base64($str);
}

/**
Expand All @@ -327,9 +327,7 @@ public function valid_json($str = null): bool
return false;
}

json_decode($str);

return json_last_error() === JSON_ERROR_NONE;
return $this->nonStrictFormatRules->valid_json($str);
}

/**
Expand All @@ -343,12 +341,7 @@ public function valid_email($str = null): bool
return false;
}

// @see https://regex101.com/r/wlJG1t/1/
if (function_exists('idn_to_ascii') && defined('INTL_IDNA_VARIANT_UTS46') && preg_match('#\A([^@]+)@(.+)\z#', $str ?? '', $matches)) {
$str = $matches[1] . '@' . idn_to_ascii($matches[2], 0, INTL_IDNA_VARIANT_UTS46);
}

return (bool) filter_var($str, FILTER_VALIDATE_EMAIL);
return $this->nonStrictFormatRules->valid_email($str);
}

/**
Expand All @@ -365,19 +358,7 @@ public function valid_emails($str = null): bool
return false;
}

foreach (explode(',', $str) as $email) {
$email = trim($email);

if ($email === '') {
return false;
}

if ($this->valid_email($email) === false) {
return false;
}
}

return true;
return $this->nonStrictFormatRules->valid_emails($str);
}

/**
Expand All @@ -392,25 +373,7 @@ public function valid_ip($ip = null, ?string $which = null): bool
return false;
}

if (empty($ip)) {
return false;
}

switch (strtolower($which ?? '')) {
case 'ipv4':
$which = FILTER_FLAG_IPV4;
break;

case 'ipv6':
$which = FILTER_FLAG_IPV6;
break;

default:
$which = 0;
}

return filter_var($ip, FILTER_VALIDATE_IP, $which) !== false
|| (! ctype_print($ip) && filter_var(inet_ntop($ip), FILTER_VALIDATE_IP, $which) !== false);
return $this->nonStrictFormatRules->valid_ip($ip, $which);
}

/**
Expand All @@ -427,21 +390,7 @@ public function valid_url($str = null): bool
return false;
}

if (empty($str)) {
return false;
}

if (preg_match('/^(?:([^:]*)\:)?\/\/(.+)$/', $str, $matches)) {
if (! in_array($matches[1], ['http', 'https'], true)) {
return false;
}

$str = $matches[2];
}

$str = 'http://' . $str;

return filter_var($str, FILTER_VALIDATE_URL) !== false;
return $this->nonStrictFormatRules->valid_url($str);
}

/**
Expand All @@ -456,19 +405,7 @@ public function valid_url_strict($str = null, ?string $validSchemes = null): boo
return false;
}

if (empty($str)) {
return false;
}

// parse_url() may return null and false
$scheme = strtolower((string) parse_url($str, PHP_URL_SCHEME));
$validSchemes = explode(
',',
strtolower($validSchemes ?? 'http,https')
);

return in_array($scheme, $validSchemes, true)
&& filter_var($str, FILTER_VALIDATE_URL) !== false;
return $this->nonStrictFormatRules->valid_url_strict($str, $validSchemes);
}

/**
Expand All @@ -482,13 +419,6 @@ public function valid_date($str = null, ?string $format = null): bool
return false;
}

if (empty($format)) {
return strtotime($str) !== false;
}

$date = DateTime::createFromFormat($format, $str);
$errors = DateTime::getLastErrors();

return $date !== false && $errors !== false && $errors['warning_count'] === 0 && $errors['error_count'] === 0;
return $this->nonStrictFormatRules->valid_date($str, $format);
}
}

0 comments on commit f18cb61

Please sign in to comment.