Regex::__invoke()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 3
crap 2
1
<?php
2
/**
3
 *
4
 * This file is part of Aura for PHP.
5
 *
6
 * @license http://opensource.org/licenses/bsd-license.php BSD
7
 *
8
 */
9
namespace Aura\Filter\Rule\Validate;
10
11
/**
12
 *
13
 * Validates the value against a regular expression.
14
 *
15
 * @package Aura.Filter
16
 *
17
 */
18
class Regex
19
{
20
    /**
21
     *
22
     * Validates the value against a regular expression.
23
     *
24
     * @param object $subject The subject to be filtered.
25
     *
26
     * @param string $field The subject field name.
27
     *
28
     * @param string $expr The regular expression to validate against.
29
     *
30
     * @return bool True if the value matches the expression, false if not.
31
     *
32
     */
33 12
    public function __invoke($subject, $field, $expr)
34
    {
35 12
        $value = $subject->$field;
36 12
        if (! is_scalar($value)) {
37 1
            return false;
38
        }
39 11
        return (bool) preg_match($expr, $value);
40
    }
41
}
42