| 1 |
<?php |
| 2 |
|
| 3 |
namespace Rakit\Validation\Rules; |
| 4 |
|
| 5 |
use Rakit\Validation\Rule; |
| 6 |
|
| 7 |
class Url extends Rule |
| 8 |
{ |
| 9 |
|
| 10 |
/** @var string */ |
| 11 |
protected $message = "The :attribute is not valid url"; |
| 12 |
|
| 13 |
/** |
| 14 |
* Given $params and assign $this->params |
| 15 |
* |
| 16 |
* @param array $params |
| 17 |
* @return self |
| 18 |
*/ |
| 19 |
public function fillParameters(array $params): Rule |
| 20 |
{ |
| 21 |
if (count($params) == 1 and is_array($params[0])) { |
| 22 |
$params = $params[0]; |
| 23 |
} |
| 24 |
return $this->forScheme($params); |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Given $schemes and assign $this->params |
| 29 |
* |
| 30 |
* @param array $schemes |
| 31 |
* @return self |
| 32 |
*/ |
| 33 |
public function forScheme($schemes): Rule |
| 34 |
{ |
| 35 |
$this->params['schemes'] = (array) $schemes; |
| 36 |
return $this; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Check the $value is valid |
| 41 |
* |
| 42 |
* @param mixed $value |
| 43 |
* @return bool |
| 44 |
*/ |
| 45 |
public function check($value): bool |
| 46 |
{ |
| 47 |
$schemes = $this->parameter('schemes'); |
| 48 |
|
| 49 |
if (!$schemes) { |
| 50 |
return $this->validateCommonScheme($value); |
| 51 |
} else { |
| 52 |
foreach ($schemes as $scheme) { |
| 53 |
$method = 'validate' . ucfirst($scheme) .'Scheme'; |
| 54 |
if (method_exists($this, $method)) { |
| 55 |
if ($this->{$method}($value)) { |
| 56 |
return true; |
| 57 |
} |
| 58 |
} elseif ($this->validateCommonScheme($value, $scheme)) { |
| 59 |
return true; |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
return false; |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Validate $value is valid URL format |
| 69 |
* |
| 70 |
* @param mixed $value |
| 71 |
* @return bool |
| 72 |
*/ |
| 73 |
public function validateBasic($value): bool |
| 74 |
{ |
| 75 |
return filter_var($value, FILTER_VALIDATE_URL) !== false; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Validate $value is correct $scheme format |
| 80 |
* |
| 81 |
* @param mixed $value |
| 82 |
* @param null $scheme |
| 83 |
* @return bool |
| 84 |
*/ |
| 85 |
public function validateCommonScheme($value, $scheme = null): bool |
| 86 |
{ |
| 87 |
if (!$scheme) { |
| 88 |
return $this->validateBasic($value) && (bool) preg_match("/^\w+:\/\//i", $value); |
| 89 |
} else { |
| 90 |
return $this->validateBasic($value) && (bool) preg_match("/^{$scheme}:\/\//", $value); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Validate the $value is mailto scheme format |
| 96 |
* |
| 97 |
* @param mixed $value |
| 98 |
* @return bool |
| 99 |
*/ |
| 100 |
public function validateMailtoScheme($value): bool |
| 101 |
{ |
| 102 |
return $this->validateBasic($value) && preg_match("/^mailto:/", $value); |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Validate the $value is jdbc scheme format |
| 107 |
* |
| 108 |
* @param mixed $value |
| 109 |
* @return bool |
| 110 |
*/ |
| 111 |
public function validateJdbcScheme($value): bool |
| 112 |
{ |
| 113 |
return (bool) preg_match("/^jdbc:\w+:\/\//", $value); |
| 114 |
} |
| 115 |
} |
| 116 |
|