Passed
Push — master ( 42367a...08368b )
by Sebastian
02:40
created

Request_Param_Validator_Regex::getDefaultOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * File containing the {@link Request_Param_Validator_Regex} class.
4
 *
5
 * @package Application Utils
6
 * @subpackage Request
7
 * @see Request_Param_Validator_Regex
8
 */
9
10
declare(strict_types=1);
11
12
namespace AppUtils;
13
14
/**
15
 * Validates a request value using a regex. Returns null if the value does not match.
16
 *
17
 * @package Application Utils
18
 * @subpackage Request
19
 * @author Sebastian Mordziol <[email protected]>
20
 */
21
class Request_Param_Validator_Regex extends Request_Param_Validator
22
{
23
    public function getDefaultOptions() : array
24
    {
25
        return array();
26
    }
27
    
28
    protected function _validate()
29
    {
30
        if(!is_scalar($this->value)) {
31
            return null;
32
        }
33
        
34
        // the only scalar value we do not want to work with
35
        // is a boolan, which is converted to an integer when
36
        // converted to string, which in turn can be validated
37
        // with a regex.
38
        if(is_bool($this->value)) {
39
            return null;
40
        }
41
        
42
        $value = (string)$this->value;
43
        
44
        if(preg_match($this->getStringOption('regex'), $value)) {
45
            return $value;
46
        }
47
        
48
        return null;
49
    }
50
}
51