Regex::__invoke()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 4
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\Sanitize;
10
11
/**
12
 *
13
 * Applies `preg_replace()` to the value.
14
 *
15
 * @package Aura.Filter
16
 *
17
 */
18
class Regex
19
{
20
    /**
21
     *
22
     * Applies `preg_replace()` to the value.
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 pattern to apply.
29
     *
30
     * @param string $replace Replace the found pattern with this string.
31
     *
32
     * @return bool True if the value was sanitized, false if not.
33
     *
34
     */
35 2
    public function __invoke($subject, $field, $expr, $replace)
36
    {
37 2
        $value = $subject->$field;
38 2
        if (! is_scalar($value)) {
39 1
            return false;
40
        }
41 1
        $subject->$field = preg_replace($expr, $replace, $value);
42 1
        return true;
43
    }
44
}
45