| 1 |
<?php |
| 2 |
/** |
| 3 |
* RegEx service interface |
| 4 |
* |
| 5 |
* @package SeQura/WC |
| 6 |
* @subpackage SeQura/WC/Services |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace SeQura\WC\Services\Regex; |
| 10 |
|
| 11 |
/** |
| 12 |
* Define methods to get regular expressions to validate data |
| 13 |
*/ |
| 14 |
class Regex implements Interface_Regex { |
| 15 |
|
| 16 |
/** |
| 17 |
* Get regular expression to validate an IPv4 or IPv6 address |
| 18 |
*/ |
| 19 |
public function ip( bool $include_slashes = true ): string { |
| 20 |
return $this->maybe_strip_slashes( |
| 21 |
'/^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/', |
| 22 |
$include_slashes |
| 23 |
); |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Get regular expression to validate a date or duration following ISO 8601 |
| 28 |
*/ |
| 29 |
public function date_or_duration( bool $include_slashes = true ): string { |
| 30 |
return $this->maybe_strip_slashes( |
| 31 |
'/^((?:\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|1\d|2[0-8]))|(?:\d{4}-(?:0[13-9]|1[0-2])-(?:29|30))|(?:\d{4}-(?:0[13578]|1[012])-(?:31))|(?:\d{2}(?:[02468][048]|[13579][26])-(?:02)-29)|(P(?:\d+Y)?(?:\d+M)?(?:\d+W)?(?:\d+D)?(?:T(?:\d+H)?(?:\d+M)?(?:\d+S)?)?))$/', |
| 32 |
$include_slashes |
| 33 |
); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Maybe strip slashes from a regex |
| 38 |
*/ |
| 39 |
private function maybe_strip_slashes( string $regex, bool $include_slashes ): string { |
| 40 |
return $include_slashes ? $regex : substr( $regex, 1, -1 ); |
| 41 |
} |
| 42 |
} |
| 43 |
|