RegexTrait   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 3
Bugs 0 Features 3
Metric Value
dl 0
loc 91
rs 10
c 3
b 0
f 3
wmc 11
lcom 0
cbo 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
C validateRegex() 0 73 11
addError() 0 1 ?
1
<?php namespace CMPayments\SchemaValidator\Validators;
2
3
use CMPayments\SchemaValidator\BaseValidator;
4
use CMPayments\SchemaValidator\Exceptions\ValidateException;
5
6
/**
7
 * Class RegexTrait
8
 *
9
 * @package CMPayments\SchemaValidator\Validators
10
 * @Author  Rob Theeuwes <[email protected]>
11
 * @Author  Boy Wijnmaalen <[email protected]>
12
 */
13
trait RegexTrait
14
{
15
    /**
16
     * Validates $data against a specific $schema->regex
17
     *
18
     * @param $data
19
     * @param $schema
20
     * @param $path
21
     */
22
    public function validateRegex($data, $schema, $path)
23
    {
24
        if (!isset($schema->pattern)) {
25
26
            return;
27
        }
28
29
        $pattern = '/' . trim($schema->pattern, '/') . '/';
30
31
        if (!in_array($schema->type, [BaseValidator::NUMBER, BaseValidator::STRING]) && !is_scalar($data)) {
32
33
            $this->addError(ValidateException::ERROR_USER_REGEX_DATA_NOT_SCALAR, [$data]);
34
        }
35
36
        /**
37
         * Use try catch to be able to handle malformed regex
38
         */
39
        try {
40
41
            // must suppress the output... !@#$%^&*(
42
            $result = @preg_match($pattern, $data);
43
44
            // successful preg_match but no match
45
            if ($result === 0) {
46
47
                $this->addError(ValidateException::ERROR_USER_REGEX_NO_MATCH, [$data, $path]);
48
            } elseif ($result === false) {
49
50
                // preg_match resulted in an error, there are multiple causes why preg_match would result in an error
51
                $pregErrors = [
52
                    PREG_INTERNAL_ERROR        => ValidateException::PREG_INTERNAL_ERROR,
53
                    PREG_BACKTRACK_LIMIT_ERROR => ValidateException::PREG_BACKTRACK_LIMIT_ERROR,
54
                    PREG_RECURSION_LIMIT_ERROR => ValidateException::PREG_RECURSION_LIMIT_ERROR,
55
                    PREG_BAD_UTF8_ERROR        => ValidateException::PREG_BAD_UTF8_ERROR,
56
                    PREG_BAD_UTF8_OFFSET_ERROR => ValidateException::PREG_BAD_UTF8_OFFSET_ERROR
57
                ];
58
59
                // when dealing with a > PHP 7 environment another preg_last_error() error became available, if so add it to the list
60
                if (PHP_VERSION_ID > 70000) {
61
62
                    $pregErrors[PREG_JIT_STACKLIMIT_ERROR] = ValidateException::PREG_JIT_STACK_LIMIT_ERROR;
63
                }
64
65
                // check for preg_match error occurred
66
                if (isset($pregErrors[preg_last_error()])) {
67
68
                    $this->addError(ValidateException::ERROR_USER_REGEX_PREG_LAST_ERROR_OCCURRED, [$schema->pattern, $data, $pregErrors[preg_last_error()]]);
69
                } elseif (($error = error_get_last()) !== null) {
70
71
                    // if the string 'preg_match()' is part of the error message we need to strip it
72
                    // because the error message is returned to the user and we don't want to reveal
73
                    // anything to the user about how we are matching patterns on a string.
74
                    // HHVM and PHP return different kind of error messages..
75
                    // HHVM does not prepend the string 'preg_match()' to the error message where PHP does prepend 'preg_match()'..
76
                    $this->addError(
77
                        ValidateException::ERROR_USER_REGEX_ERROR_LAST_ERROR_OCCURRED,
78
                        [
79
                            $schema->pattern,
80
                            $data,
81
                            ((strpos($error['message'], 'preg_match()') !== false) ? substr($error['message'], strlen('preg_match(): ')) : $error['message'])
82
                        ]
83
                    );
84
                } else {
85
86
                    // unknown error..
87
                    $this->addError(ValidateException::ERROR_USER_REGEX_UNKNOWN_ERROR_OCCURRED, [$schema->pattern, $data]);
88
                }
89
            }
90
        } catch (\Exception $ex) {
91
92
            $this->addError(ValidateException::ERROR_USER_REGEX_GENERAL_ERROR_OCCURRED, [$schema->pattern, $data]);
93
        }
94
    }
95
96
    /**
97
     * @param int   $code
98
     * @param array $args
99
     *
100
     * @return mixed
101
     */
102
    abstract public function addError($code, array $args = []);
103
}
104