|
1
|
|
|
<?php |
|
2
|
|
|
namespace AlgoWeb\xsdTypes\Facets; |
|
3
|
|
|
|
|
4
|
|
|
trait PatternTrait |
|
5
|
|
|
{ |
|
6
|
|
|
use XMLPatterns; |
|
7
|
|
|
/** |
|
8
|
|
|
* @Exclude |
|
9
|
|
|
* @var array defines the exact sequence of characters that are acceptable |
|
10
|
|
|
*/ |
|
11
|
|
|
private $pattern = array(); |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @param string $regexPatternToAdd |
|
15
|
|
|
*/ |
|
16
|
|
|
protected function setPatternFacet($regexPatternToAdd, $ProcessMultiCharacterEscape = true) |
|
|
|
|
|
|
17
|
|
|
{ |
|
18
|
|
|
if(null == self::$Letter){ |
|
19
|
|
|
self::init(); |
|
20
|
|
|
} |
|
21
|
|
|
$regexPatternToAdd = $this->ProcessRegex($regexPatternToAdd, $ProcessMultiCharacterEscape); |
|
|
|
|
|
|
22
|
|
|
if (!$this->checkRegexValidPattern($regexPatternToAdd)) { |
|
23
|
|
|
$regexPatternToAdd = '/' . $regexPatternToAdd . '/'; |
|
24
|
|
|
if (!$this->checkRegexValidPattern($regexPatternToAdd)) { |
|
25
|
|
|
throw new \InvalidArgumentException('Invalid regex pattern provided: ' . get_class($this)); |
|
26
|
|
|
} |
|
27
|
|
|
} |
|
28
|
|
|
$this->pattern[] = $regexPatternToAdd; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
private function ProcessRegex($regexPattern, $ProcessMultiCharacterEscape){ |
|
|
|
|
|
|
32
|
|
|
if(!$ProcessMultiCharacterEscape){ |
|
33
|
|
|
return $regexPattern; |
|
34
|
|
|
} |
|
35
|
|
|
if(null == self::$NameChar){ |
|
36
|
|
|
init(); |
|
37
|
|
|
} |
|
38
|
|
|
/** |
|
39
|
|
|
* |
|
40
|
|
|
*/ |
|
41
|
|
|
$regexPattern = str_replace('\S','[^\s]',$regexPattern); |
|
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* \x{20} matches the character with index 2016 (3210 or 408) literally (case sensitive) |
|
44
|
|
|
* \t matches a tab character (ASCII 9) |
|
45
|
|
|
* \n matches a line-feed (newline) character (ASCII 10) |
|
46
|
|
|
* \r matches a carriage return (ASCII 13) |
|
47
|
|
|
*/ |
|
48
|
|
|
$regexPattern = str_replace('\s','[\x{20}\t\n\r]',$regexPattern); |
|
|
|
|
|
|
49
|
|
|
$regexPattern = str_replace('\I','[^\i]',$regexPattern); |
|
|
|
|
|
|
50
|
|
|
$regexPattern = str_replace('\i',self::$Letter.'|_|:',$regexPattern); |
|
|
|
|
|
|
51
|
|
|
$regexPattern = str_replace('\c',self::$NameChar,$regexPattern); |
|
|
|
|
|
|
52
|
|
|
$regexPattern = str_replace('\D','[^\d]',$regexPattern); |
|
|
|
|
|
|
53
|
|
|
$regexPattern = str_replace('\d','\p{Nd}',$regexPattern); |
|
|
|
|
|
|
54
|
|
|
$regexPattern = str_replace('\W','[^\w]',$regexPattern); |
|
|
|
|
|
|
55
|
|
|
$regexPattern = str_replace('\w','[\x{0000}-\x{10FFFF}]-[\p{P}\p{Z}\p{C}] ',$regexPattern); |
|
|
|
|
|
|
56
|
|
|
return $regexPattern; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param string $pattern |
|
61
|
|
|
*/ |
|
62
|
|
|
private function checkRegexValidPattern($pattern) |
|
63
|
|
|
{ |
|
64
|
|
|
return (false === @preg_match($pattern, null)); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param string $v |
|
69
|
|
|
*/ |
|
70
|
|
|
private function checkPattern($v) |
|
71
|
|
|
{ |
|
72
|
|
|
if (!empty($this->pattern)) { |
|
73
|
|
|
foreach ($this->pattern as $pattern) { |
|
74
|
|
|
if (!$this->matchesRegexPattern($pattern, $v)) { |
|
75
|
|
|
throw new \InvalidArgumentException('Assigned value for ' . get_class($this) . |
|
76
|
|
|
' does not match pattern ' . $pattern); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Checks a pattern against a string. |
|
84
|
|
|
* |
|
85
|
|
|
* @param string $pattern the regex pattern |
|
86
|
|
|
* @param string $string the string to check |
|
87
|
|
|
* @return bool true if string matches pattern |
|
88
|
|
|
*/ |
|
89
|
|
|
private function matchesRegexPattern($pattern, $string) |
|
90
|
|
|
{ |
|
91
|
|
|
$matches = null; |
|
92
|
|
|
return (1 == preg_match($pattern, $string, $matches) && $string == $matches[0]); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
This check marks parameter names that have not been written in camelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes
databaseConnectionString.