StringVal::__invoke()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 2
nop 2
dl 0
loc 9
ccs 6
cts 6
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Mbright\Validation\Rule\Sanitize;
4
5
class StringVal implements SanitizeRuleInterface
6
{
7
    /** @var null */
8
    protected $find;
9
10
    /** @var null */
11
    protected $replace;
12
13
    /**
14
     * @param string|array $find Find this/these in the value.
15
     * @param string|array $replace Replace with this/these in the value.
16
     *
17
     */
18 9
    public function __construct($find = null, $replace = null)
19
    {
20 9
        $this->find = $find;
21 9
        $this->replace = $replace;
22 9
    }
23
24
    /**
25
     * Forces the value to a string, optionally applying `str_replace()`.
26
     *
27
     * @param object $subject The subject to be filtered.
28
     * @param string $field The subject field name.
29
     *
30
     * @return bool True if the value was sanitized, false if not.
31
     */
32 9
    public function __invoke($subject, string $field): bool
33
    {
34 9
        $value = (string) $subject->$field;
35 9
        if ($this->find || $this->replace) {
36 9
            $value = str_replace($this->find, $this->replace, $value);
37
        }
38 9
        $subject->$field = $value;
39
40 9
        return true;
41
    }
42
}
43