|
1
|
|
|
<?php |
|
2
|
|
|
|
|
|
|
|
|
|
3
|
|
|
namespace Dallgoot\Yaml; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Define Regex patterns as constants |
|
7
|
|
|
*/ |
|
|
|
|
|
|
8
|
|
|
class Regex |
|
9
|
|
|
{ |
|
10
|
|
|
|
|
11
|
|
|
const NULL = "null"; |
|
12
|
|
|
const FALSE = "false"; |
|
13
|
|
|
const TRUE = "true"; |
|
14
|
|
|
const AN = "[\w ]+"; |
|
15
|
|
|
const NUM = "-?[\d.e]+"; |
|
16
|
|
|
const SIMPLE = "(?P<sv>".self::NULL."|". |
|
17
|
|
|
self::FALSE."|". |
|
18
|
|
|
self::TRUE."|". |
|
19
|
|
|
self::AN."|". |
|
20
|
|
|
self::NUM.")"; |
|
21
|
|
|
private const seqForMap = "(?P<seq>\[(?:(?:(?P>sv)|(?P>seq)|(?P>map)),?\s*)+\])"; |
|
|
|
|
|
|
22
|
|
|
private const mapForSeq = "(?P<map>{\s*(?:".self::AN."\s*:\s*(?:(?P>sv)|(?P>seq)|(?P>map)),?\s*)+})"; |
|
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
const MAPPING = "/(?P<map>{\s*(?:".self::AN."\s*:\s*(?:".self::SIMPLE."|".self::seqForMap."|(?P>map)),?\s*)+})/i"; |
|
25
|
|
|
const SEQUENCE = "/(?P<seq>\[(?:(?:".self::SIMPLE."|".self::mapForSeq."|(?P>seq)),?\s*)+\])/i"; |
|
26
|
|
|
|
|
27
|
|
|
const KEY = '/^([[:alnum:]_][[:alnum:]_ -]*[ \t]*)(?::[ \t](.*)|:)$/'; |
|
28
|
|
|
const ITEM = '/^-([ \t]+(.*))?$/'; |
|
29
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
public static function isDate($v):bool |
|
|
|
|
|
|
32
|
|
|
{ |
|
33
|
|
|
$d = "\\d{4}([-\\/])\\d{2}\\1\\d{2}"; |
|
34
|
|
|
$h = "\\d{2}(:)\\d{2}\\2\\d{2}"; |
|
35
|
|
|
$date = "/^$d$/";// 2002-12-14, 2002/12/14 |
|
36
|
|
|
$canonical = "/^$d(?:t| )$h\\.\\dz?$/im";// 2001-12-15T02:59:43.1Z |
|
37
|
|
|
$spaced = "/^$d(?:t| )$h\\.\\d{2} [-+]\\d$/im";// 2001-12-14 21:59:43.10 -5 |
|
38
|
|
|
$iso8601 = "/^$d(?:t| )$h\\.\\d{2}[-+]\\d{2}\\2\\d{2}/im";// 2001-12-14t21:59:43.10-05:00 |
|
39
|
|
|
$matchDate = preg_match($date, $v); |
|
40
|
|
|
$matchCanonical = preg_match($canonical, $v); |
|
41
|
|
|
$matchSpaced = preg_match($spaced, $v); |
|
42
|
|
|
$matchIso = preg_match($iso8601, $v); |
|
43
|
|
|
if (is_bool($matchDate) || is_bool($matchCanonical) || is_bool($matchSpaced) || is_bool($matchIso)) |
|
|
|
|
|
|
44
|
|
|
throw new \Exception("Regex date error"); |
|
45
|
|
|
|
|
46
|
|
|
return $matchDate || $matchCanonical || $matchSpaced || $matchIso; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public static function isNumber(string $var):bool |
|
|
|
|
|
|
50
|
|
|
{ |
|
51
|
|
|
//TODO: https://secure.php.net/manual/en/function.is-numeric.php |
|
52
|
|
|
return (bool) preg_match("/^((0o\d+)|(0x[\da-f]+)|([\d.]+e[-+]\d{1,2})|([-+]?(\d*\.?\d+)))$/i", $var); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public static function isProperlyQuoted(String $var):bool |
|
|
|
|
|
|
56
|
|
|
{ |
|
57
|
|
|
return (bool) preg_match("/(['".'"]).*?(?<![\\\\])\1$/ms', $var); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|