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
|
|
|
public $pattern = array(); |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @param string $newPatternToAdd |
15
|
|
|
* @param mixed $processMultiCharacterEscape |
16
|
|
|
*/ |
17
|
|
|
protected function setPatternFacet($newPatternToAdd, $processMultiCharacterEscape = true) |
18
|
|
|
{ |
19
|
|
|
if (null == self::$Letter) { |
20
|
|
|
self::init(); |
21
|
|
|
} |
22
|
|
|
$newPatternToAdd = $this->processRegex($newPatternToAdd, $processMultiCharacterEscape); |
|
|
|
|
23
|
|
|
if (!$this->checkRegexValidPattern($newPatternToAdd)) { |
24
|
|
|
$newPatternToAdd = '/' . $newPatternToAdd . '/u'; |
|
|
|
|
25
|
|
|
if (!$this->checkRegexValidPattern($newPatternToAdd)) { |
26
|
|
|
throw new \InvalidArgumentException('Invalid regex pattern provided: ' . get_class($this) . |
27
|
|
|
'pattern is: ' . $newPatternToAdd); |
28
|
|
|
} |
29
|
|
|
} |
30
|
|
|
$this->pattern[] = $newPatternToAdd; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param string $patternToProcess |
35
|
|
|
* @param bool $processMultiCharacterEscape |
36
|
|
|
* |
37
|
|
|
* @return string |
38
|
|
|
*/ |
39
|
|
|
private function processRegex($patternToProcess, $processMultiCharacterEscape) |
40
|
|
|
{ |
41
|
|
|
if (!$processMultiCharacterEscape) { |
42
|
|
|
return $patternToProcess; |
43
|
|
|
} |
44
|
|
|
if (null == self::$NameChar) { |
45
|
|
|
init(); |
46
|
|
|
} |
47
|
|
|
$patternToProcess = $this->processRegexSlashS($patternToProcess); |
|
|
|
|
48
|
|
|
$patternToProcess = $this->processRegexSlashI($patternToProcess); |
|
|
|
|
49
|
|
|
$patternToProcess = $this->processRegexSlashC($patternToProcess); |
|
|
|
|
50
|
|
|
$patternToProcess = $this->processRegexSlashD($patternToProcess); |
|
|
|
|
51
|
|
|
$patternToProcess = $this->processRegexSlashW($patternToProcess); |
|
|
|
|
52
|
|
|
return $patternToProcess; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param string $patternToProcess |
57
|
|
|
* |
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
|
View Code Duplication |
private function processRegexSlashS($patternToProcess) |
|
|
|
|
61
|
|
|
{ |
62
|
|
|
// Any character except those matched by '\s'. |
63
|
|
|
$patternToProcess = str_replace('\S', '[^\s]', $patternToProcess); |
|
|
|
|
64
|
|
|
// Whitespace, specifically '' (space), '\t' (tab), '\n' (newline) and '\r' (return). |
|
|
|
|
65
|
|
|
$patternToProcess = str_replace('\s', '([\x{20}\t\n\r])', $patternToProcess); |
|
|
|
|
66
|
|
|
return $patternToProcess; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param string $patternToProcess |
71
|
|
|
* |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
|
|
private function processRegexSlashI($patternToProcess) |
75
|
|
|
{ |
76
|
|
|
// Any character except those matched by '\i'. |
77
|
|
|
$patternToProcess = str_replace('\I', '[^\i]', $patternToProcess); |
|
|
|
|
78
|
|
|
// The first character in an XML identifier. Specifically, any letter, the character '_', or the character ':', |
79
|
|
|
// See the XML Recommendation for the complex specification of a letter. This character represents a subset of |
80
|
|
|
// letter that might appear in '\c'. |
81
|
|
|
$patternToProcess = str_replace('\i-[:]', self::$Letter . '|_| ', $patternToProcess); |
|
|
|
|
82
|
|
|
$patternToProcess = str_replace('\i', '(' . self::$Letter . '|_|:)', $patternToProcess); |
|
|
|
|
83
|
|
|
return $patternToProcess; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param string $patternToProcess |
88
|
|
|
* |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
|
|
private function processRegexSlashC($patternToProcess) |
92
|
|
|
{ |
93
|
|
|
// Any character except those matched by '\c'. |
94
|
|
|
$patternToProcess = str_replace('\C', '[^\c]', $patternToProcess); |
|
|
|
|
95
|
|
|
// Any character that might appear in the built-in NMTOKEN datatype. |
96
|
|
|
// See the XML Recommendation for the complex specification of a NameChar. |
97
|
|
|
$patternToProcess = str_replace('\c-[:]', self::$Letter . '|' . self::$Digit . '|.|-|_|' . self::$CombiningChar . '|' |
|
|
|
|
98
|
|
|
. self::$Extender, $patternToProcess); |
99
|
|
|
$patternToProcess = str_replace('\c', '(' . self::$NameChar . ')', $patternToProcess); |
|
|
|
|
100
|
|
|
return $patternToProcess; |
101
|
|
|
|
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param string $patternToProcess |
106
|
|
|
* |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
|
View Code Duplication |
private function processRegexSlashD($patternToProcess) |
|
|
|
|
110
|
|
|
{ |
111
|
|
|
// Any character except those matched by '\d'. |
112
|
|
|
$patternToProcess = str_replace('\D', '[^(\d)]', $patternToProcess); |
|
|
|
|
113
|
|
|
// Any Decimal digit. A shortcut for '\p{Nd}'. |
114
|
|
|
$patternToProcess = str_replace('\d', '\p{Nd)', $patternToProcess); |
|
|
|
|
115
|
|
|
return $patternToProcess; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param string $patternToProcess |
120
|
|
|
* |
121
|
|
|
* @return string |
122
|
|
|
*/ |
123
|
|
View Code Duplication |
private function processRegexSlashW($patternToProcess) |
|
|
|
|
124
|
|
|
{ |
125
|
|
|
// Any character except those matched by '\w'. |
126
|
|
|
$patternToProcess = str_replace('\W', '[^\w]', $patternToProcess); |
|
|
|
|
127
|
|
|
// Any character that might appear in a word. A shortcut for '[#X0000-#x10FFFF]-[\p{P}\p{Z}\p{C}]' |
128
|
|
|
// (all characters except the set of "punctuation", "separator", and "other" characters). |
129
|
|
|
$patternToProcess = str_replace('\w', '([\x{0000}-\x{10FFFF}]-[\p{P}\p{Z}\p{C}])', |
|
|
|
|
130
|
|
|
$patternToProcess); |
131
|
|
|
return $patternToProcess; |
132
|
|
|
|
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param string $pattern |
137
|
|
|
* |
138
|
|
|
* @return bool |
139
|
|
|
*/ |
140
|
|
|
private function checkRegexValidPattern($pattern) |
141
|
|
|
{ |
142
|
|
|
return !(false === @preg_match($pattern, null)); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param string $v |
147
|
|
|
*/ |
148
|
|
|
private function checkPattern($v) |
149
|
|
|
{ |
150
|
|
|
if (!empty($this->pattern)) { |
151
|
|
|
foreach ($this->pattern as $pattern) { |
152
|
|
|
if (!$this->matchesRegexPattern($pattern, $v)) { |
153
|
|
|
throw new \InvalidArgumentException('Assigned value for ' . get_class($this) . |
154
|
|
|
' does not match pattern ' . $pattern); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Checks a pattern against a string. |
162
|
|
|
* |
163
|
|
|
* @param string $pattern the regex pattern |
164
|
|
|
* @param string $string the string to check |
165
|
|
|
* @return bool true if string matches pattern |
166
|
|
|
*/ |
167
|
|
|
private function matchesRegexPattern($pattern, $string) |
168
|
|
|
{ |
169
|
|
|
$matches = null; |
170
|
|
|
return (1 == preg_match($pattern, $string, $matches) && $string == $matches[0]); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|