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 $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 . '/'; |
|
|
|
|
25
|
|
|
if (!$this->checkRegexValidPattern($newPatternToAdd)) { |
26
|
|
|
throw new \InvalidArgumentException('Invalid regex pattern provided: ' . get_class($this)); |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
$this->pattern[] = $newPatternToAdd; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param string $patternToProcess |
34
|
|
|
* |
35
|
|
|
* @return string |
36
|
|
|
*/ |
37
|
|
|
private function processRegex($patternToProcess, $processMultiCharacterEscape) |
38
|
|
|
{ |
39
|
|
|
if (!$processMultiCharacterEscape) { |
40
|
|
|
return $patternToProcess; |
41
|
|
|
} |
42
|
|
|
if (null == self::$NameChar) { |
43
|
|
|
init(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$patternToProcess = str_replace('\S', '[^\s]', $patternToProcess); |
|
|
|
|
47
|
|
|
$patternToProcess = str_replace('\s', '[\x{20}\t\n\r]', $patternToProcess); |
|
|
|
|
48
|
|
|
$patternToProcess = str_replace('\I', '[^\i]', $patternToProcess); |
|
|
|
|
49
|
|
|
$patternToProcess = str_replace('\i', self::$Letter . '|_|:', $patternToProcess); |
|
|
|
|
50
|
|
|
$patternToProcess = str_replace('\c', self::$NameChar, $patternToProcess); |
|
|
|
|
51
|
|
|
$patternToProcess = str_replace('\D', '[^\d]', $patternToProcess); |
|
|
|
|
52
|
|
|
$patternToProcess = str_replace('\d', '\p{Nd}', $patternToProcess); |
|
|
|
|
53
|
|
|
$patternToProcess = str_replace('\W', '[^\w]', $patternToProcess); |
|
|
|
|
54
|
|
|
$patternToProcess = str_replace('\w', '[\x{0000}-\x{10FFFF}]-[\p{P}\p{Z}\p{C}] ', $patternToProcess); |
|
|
|
|
55
|
|
|
return $patternToProcess; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param string $pattern |
60
|
|
|
*/ |
61
|
|
|
private function checkRegexValidPattern($pattern) |
62
|
|
|
{ |
63
|
|
|
return !(false === @preg_match($pattern, null)); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param string $v |
68
|
|
|
*/ |
69
|
|
|
private function checkPattern($v) |
70
|
|
|
{ |
71
|
|
|
if (!empty($this->pattern)) { |
72
|
|
|
foreach ($this->pattern as $pattern) { |
73
|
|
|
if (!$this->matchesRegexPattern($pattern, $v)) { |
74
|
|
|
throw new \InvalidArgumentException('Assigned value for ' . get_class($this) . |
75
|
|
|
' does not match pattern ' . $pattern); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Checks a pattern against a string. |
83
|
|
|
* |
84
|
|
|
* @param string $pattern the regex pattern |
85
|
|
|
* @param string $string the string to check |
86
|
|
|
* @return bool true if string matches pattern |
87
|
|
|
*/ |
88
|
|
|
private function matchesRegexPattern($pattern, $string) |
89
|
|
|
{ |
90
|
|
|
$matches = null; |
91
|
|
|
return (1 == preg_match($pattern, $string, $matches) && $string == $matches[0]); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|