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

StringVal   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 0
dl 0
loc 23
c 0
b 0
f 0
ccs 6
cts 6
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 10 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