Completed
Push — master ( c61969...df1bf5 )
by Marcus
02:35
created

StringVal::__invoke()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

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