Test Failed
Pull Request — master (#26)
by Christopher
02:41 queued 17s
created

PatternTrait::processRegex()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 20
rs 9.4285
cc 3
eloc 15
nc 3
nop 2
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);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $regexPatternToAdd. This often makes code more readable.
Loading history...
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
    /**
33
     * @param string $regexPattern
34
     *
35
     * @return string
36
     */
37
    private function processRegex($regexPattern, $processMultiCharacterEscape)
38
    {
39
        if (!$processMultiCharacterEscape) {
40
            return $regexPattern;
41
        }
42
        if (null == self::$NameChar) {
43
            init();
44
        }
45
        
46
        $regexPattern = str_replace('\S', '[^\s]', $regexPattern);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $regexPattern. This often makes code more readable.
Loading history...
47
        $regexPattern = str_replace('\s', '[\x{20}\t\n\r]', $regexPattern);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $regexPattern. This often makes code more readable.
Loading history...
48
        $regexPattern = str_replace('\I', '[^\i]', $regexPattern);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $regexPattern. This often makes code more readable.
Loading history...
49
        $regexPattern = str_replace('\i', self::$Letter . '|_|:', $regexPattern);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $regexPattern. This often makes code more readable.
Loading history...
50
        $regexPattern = str_replace('\c', self::$NameChar, $regexPattern);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $regexPattern. This often makes code more readable.
Loading history...
51
        $regexPattern = str_replace('\D', '[^\d]', $regexPattern);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $regexPattern. This often makes code more readable.
Loading history...
52
        $regexPattern = str_replace('\d', '\p{Nd}', $regexPattern);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $regexPattern. This often makes code more readable.
Loading history...
53
        $regexPattern = str_replace('\W', '[^\w]', $regexPattern);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $regexPattern. This often makes code more readable.
Loading history...
54
        $regexPattern = str_replace('\w', '[\x{0000}-\x{10FFFF}]-[\p{P}\p{Z}\p{C}] ', $regexPattern);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $regexPattern. This often makes code more readable.
Loading history...
55
        return $regexPattern;
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