Obfuscate::filterSingle()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 14
cts 14
cp 1
rs 9.6
c 0
b 0
f 0
cc 4
nc 5
nop 2
crap 4
1
<?php
2
declare(strict_types=1);
3
namespace Sirius\Filtration\Filter;
4
5
class Obfuscate extends AbstractFilter
6
{
7
    // how may characters from the benining are left untouched
8
    const OPTION_START_CHARACTERS = 'start_characters';
9
    // how may characters from the end are left untouched
10
    const OPTION_END_CHARACTERS = 'end_characters';
11
    // replacement character
12
    const OPTION_REPLACEMENT_CHAR = 'replacement_char';
13
14
    protected $options = [
15
        self::OPTION_START_CHARACTERS => 0,
16
        self::OPTION_END_CHARACTERS => 0,
17
        self::OPTION_REPLACEMENT_CHAR => '*'
18
    ];
19
20 7
    public function filterSingle($value, string $valueIdentifier = null)
21
    {
22
        // not a string, move along
23 7
        if (! is_string($value)) {
24 1
            return $value;
25
        }
26
        
27 6
        $len = strlen($value);
28 6
        $start = $this->options[self::OPTION_START_CHARACTERS] ?
29 3
            substr($value, 0, $this->options[self::OPTION_START_CHARACTERS]) :
30 6
            '';
31 6
        $end = $this->options[self::OPTION_END_CHARACTERS] ?
32 3
            substr($value, $len - $this->options[self::OPTION_END_CHARACTERS]) :
33 6
            '';
34 6
        $middle = str_repeat(
35 6
            $this->options[self::OPTION_REPLACEMENT_CHAR],
36 6
            $len - $this->options[self::OPTION_START_CHARACTERS] - $this->options[self::OPTION_END_CHARACTERS]
37
        );
38 6
        return $start . $middle . $end;
39
    }
40
}
41