StrlenMin::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 3
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Mbright\Validation\Rule\Sanitize;
4
5
use Mbright\Validation\Rule\AbstractStringCase;
6
7
class StrlenMin extends AbstractStringCase implements SanitizeRuleInterface
8
{
9
    /** @var int */
10
    protected $min;
11
12
    /** @var string */
13
    protected $padString;
14
15
    /** @var int */
16
    protected $padType;
17
18
19
    /**
20
     * @param int $min The minimum length.
21
     * @param string $pad_string Pad using this string.
22
     * @param int $pad_type A `STR_PAD_*` constant.
23
     */
24 21
    public function __construct(int $min, string $padString = ' ', int $padType = STR_PAD_RIGHT)
25
    {
26 21
        $this->min = $min;
27 21
        $this->padString = $padString;
28 21
        $this->padType = $padType;
29 21
    }
30
31
    /**
32
     * Sanitizes a string to a minimum length by padding it.
33
     *
34
     * @param object $subject The subject to be filtered.
35
     * @param string $field The subject field name.
36
     *
37
     * @return bool True if the value was sanitized, false if not.
38
     */
39 21
    public function __invoke($subject, string $field): bool
40
    {
41 21
        $value = $subject->$field;
42 21
        if (!is_scalar($value)) {
43 3
            return false;
44
        }
45
46 18
        if ($this->strlen($value) < $this->min) {
47 6
            $subject->$field = $this->strpad($value, $this->min, $this->padString, $this->padType);
48
        }
49
50 18
        return true;
51
    }
52
}
53