Test Failed
Push — master ( aec97b...d7d4d0 )
by Alex
02:17
created

PatternTrait   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 5
Bugs 2 Features 0
Metric Value
wmc 14
c 5
b 2
f 0
lcom 1
cbo 1
dl 0
loc 90
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setPatternFacet() 0 14 4
A checkRegexValidPattern() 0 4 1
A checkPattern() 0 11 4
A matchesRegexPattern() 0 5 2
A processRegex() 0 20 3
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);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $newPatternToAdd. This often makes code more readable.
Loading history...
23
        if (!$this->checkRegexValidPattern($newPatternToAdd)) {
24
            $newPatternToAdd = '/' . $newPatternToAdd . '/';
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $newPatternToAdd. This often makes code more readable.
Loading history...
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);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $patternToProcess. This often makes code more readable.
Loading history...
47
        $patternToProcess = str_replace('\s', '[\x{20}\t\n\r]', $patternToProcess);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $patternToProcess. This often makes code more readable.
Loading history...
48
        $patternToProcess = str_replace('\I', '[^\i]', $patternToProcess);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $patternToProcess. This often makes code more readable.
Loading history...
49
        $patternToProcess = str_replace('\i', self::$Letter . '|_|:', $patternToProcess);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $patternToProcess. This often makes code more readable.
Loading history...
50
        $patternToProcess = str_replace('\c', self::$NameChar, $patternToProcess);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $patternToProcess. This often makes code more readable.
Loading history...
51
        $patternToProcess = str_replace('\D', '[^\d]', $patternToProcess);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $patternToProcess. This often makes code more readable.
Loading history...
52
        $patternToProcess = str_replace('\d', '\p{Nd}', $patternToProcess);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $patternToProcess. This often makes code more readable.
Loading history...
53
        $patternToProcess = str_replace('\W', '[^\w]', $patternToProcess);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $patternToProcess. This often makes code more readable.
Loading history...
54
        $patternToProcess = str_replace('\w', '[\x{0000}-\x{10FFFF}]-[\p{P}\p{Z}\p{C}] ', $patternToProcess);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $patternToProcess. This often makes code more readable.
Loading history...
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