Code Duplication    Length = 47-53 lines in 2 locations

src/Rule/Sanitize/StrBetween.php 1 location

@@ 7-59 (lines=53) @@
4
5
use Mbright\Validation\Rule\AbstractStringCase;
6
7
class StrBetween extends AbstractStringCase implements SanitizeRuleInterface
8
{
9
    /** @var int */
10
    protected $min;
11
12
    /** @var int */
13
    protected $max;
14
15
    /** @var string */
16
    protected $padString;
17
18
    /** @var int */
19
    protected $padType;
20
21
    /**
22
     * @param int $min The minimum length.
23
     * @param int $max The maximum length.
24
     * @param string $pad_string Pad using this string.
25
     * @param int $pad_type A `STR_PAD_*` constant.
26
     *
27
     */
28
    public function __construct(int $min, int $max, string $padString = ' ', int $padType = STR_PAD_RIGHT)
29
    {
30
        $this->min = $min;
31
        $this->max = $max;
32
        $this->padString = $padString;
33
        $this->padType = $padType;
34
    }
35
36
    /**
37
     * Sanitizes a string to a length range by padding or chopping it.
38
     *
39
     * @param object $subject The subject to be filtered.
40
     * @param string $field The subject field name.
41
     *
42
     * @return bool True if the value was sanitized, false if not.
43
     */
44
    public function __invoke($subject, string $field): bool
45
    {
46
        $value = $subject->$field;
47
        if (!is_scalar($value)) {
48
            return false;
49
        }
50
        if ($this->strlen($value) < $this->min) {
51
            $subject->$field = $this->strpad($value, $this->min, $this->padString, $this->padType);
52
        }
53
        if ($this->strlen($value) > $this->max) {
54
            $subject->$field = $this->substr($value, 0, $this->max);
55
        }
56
57
        return true;
58
    }
59
}
60

src/Rule/Sanitize/Strlen.php 1 location

@@ 7-53 (lines=47) @@
4
5
use Mbright\Validation\Rule\AbstractStringCase;
6
7
class Strlen extends AbstractStringCase implements SanitizeRuleInterface
8
{
9
    /** @var int */
10
    protected $len;
11
12
    /** @var string */
13
    protected $padString;
14
15
    /** @var int */
16
    protected $padType;
17
18
    /**
19
     * @param int $len The string length.
20
     * @param string $pad_string Pad using this string.
21
     * @param int $pad_type A `STR_PAD_*` constant.
22
     */
23
    public function __construct(int $len, string $padString = ' ', int $padType = STR_PAD_RIGHT)
24
    {
25
        $this->len = $len;
26
        $this->padString = $padString;
27
        $this->padType = $padType;
28
    }
29
30
    /**
31
     * Sanitizes a string to an exact length by padding or chopping it.
32
     *
33
     * @param object $subject The subject to be filtered.
34
     * @param string $field The subject field name.
35
     *
36
     * @return bool True if the value was sanitized, false if not.
37
     */
38
    public function __invoke($subject, string $field): bool
39
    {
40
        $value = $subject->$field;
41
        if (!is_scalar($value)) {
42
            return false;
43
        }
44
        if ($this->strlen($value) < $this->len) {
45
            $subject->$field = $this->strpad($value, $this->len, $this->padString, $this->padType);
46
        }
47
        if ($this->strlen($value) > $this->len) {
48
            $subject->$field = $this->substr($value, 0, $this->len);
49
        }
50
51
        return true;
52
    }
53
}
54