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
|
|
|
* @param mixed $ProcessMultiCharacterEscape |
16
|
|
|
*/ |
17
|
|
|
protected function setPatternFacet($regexPatternToAdd, $ProcessMultiCharacterEscape = true) |
|
|
|
|
18
|
|
|
{ |
19
|
|
|
if (null == self::$Letter) { |
20
|
|
|
self::init(); |
21
|
|
|
} |
22
|
|
|
$regexPatternToAdd = $this->ProcessRegex($regexPatternToAdd, $ProcessMultiCharacterEscape); |
|
|
|
|
23
|
|
|
if (!$this->checkRegexValidPattern($regexPatternToAdd)) { |
24
|
|
|
$regexPatternToAdd = '/' . $regexPatternToAdd . '/'; |
25
|
|
|
if (!$this->checkRegexValidPattern($regexPatternToAdd)) { |
26
|
|
|
throw new \InvalidArgumentException('Invalid regex pattern provided: ' . get_class($this)); |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
$this->pattern[] = $regexPatternToAdd; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
private function ProcessRegex($regexPattern, $ProcessMultiCharacterEscape) |
|
|
|
|
33
|
|
|
{ |
34
|
|
|
if (!$ProcessMultiCharacterEscape) { |
35
|
|
|
return $regexPattern; |
36
|
|
|
} |
37
|
|
|
if (null == self::$NameChar) { |
38
|
|
|
init(); |
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
.