ValidateRegex::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 3
dl 0
loc 6
rs 10
1
<?php declare(strict_types=1);
2
3
namespace XoopsModules\Xhelp\Validation;
4
5
/*
6
 * You may not change or alter any portion of this comment or credits
7
 * of supporting developers from this source code or any supporting source code
8
 * which is considered copyrighted (c) material of the original comment or credit authors.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
 */
14
15
/**
16
 * @copyright    {@link https://xoops.org/ XOOPS Project}
17
 * @license      {@link https://www.gnu.org/licenses/gpl-2.0.html GNU GPL 2 or later}
18
 * @author       Brian Wahoff <[email protected]>
19
 * @author       Eric Juden <[email protected]>
20
 * @author       XOOPS Development Team
21
 */
22
23
/**
24
 *  ValidatorRegex subclass of Validator
25
 *  Validates an email address
26
 */
27
class ValidateRegex extends Validator
28
{
29
    public $pattern;
30
    public $checkText = '';
31
    public $required;
32
    //! A constructor.
33
34
    /**
35
     * Constructs a new ValidateEmail object subclass or Validator
36
     * @param string $checkText
37
     * @param string $pattern
38
     * @param bool   $required
39
     * @internal param the $email string to validate
40
     */
41
    public function __construct(string $checkText, string $pattern, bool $required)
42
    {
43
        $this->pattern   = $pattern;
44
        $this->checkText = $checkText;
45
        $this->required  = $required;
46
        parent::__construct();
47
    }
48
49
    //! A manipulator
50
51
    /**
52
     * Validates a regular expression
53
     */
54
    public function validate()
55
    {
56
        if (1 == $this->required) {                                // If value is required
57
            if (\is_array($this->checkText) && isset($this->checkText['size'])) {     // If this is a file
0 ignored issues
show
introduced by
The condition is_array($this->checkText) is always false.
Loading history...
58
                if ('' === $this->checkText['name']) {
59
                    $this->setError(\_XHELP_MESSAGE_REQUIRED);           // Return message saying required value
60
                }
61
            } else {                                                    // If not a file
62
                if ('' != $this->pattern) {                               // If regex pattern is not empty
63
                    if (!\preg_match('/' . $this->pattern . '/', (string)$this->checkText)) {  // Check regex against supplied text
64
                        $this->setError(\_XHELP_MESSAGE_INVALID);                              // Return message saying invalid value
65
                    }
66
                } else {
67
                    if (empty($this->checkText)) {                           // If text is not supplied
68
                        $this->setError(\_XHELP_MESSAGE_REQUIRED);           // Return message saying required value
69
                    }
70
                }
71
            }
72
        } else {
73
            if (empty($this->checkText)) {
74
                if ('' != $this->pattern) {
75
                    if (!\preg_match('/' . $this->pattern . '/', $this->checkText)) {
76
                        $this->setError(\_XHELP_MESSAGE_INVALID);
77
                    }
78
                }
79
            }
80
        }
81
    }
82
}
83