StrlenBetween::__invoke()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 9
cts 9
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
nc 5
nop 6
crap 4
1
<?php
2
/**
3
 *
4
 * This file is part of Aura for PHP.
5
 *
6
 * @license http://opensource.org/licenses/bsd-license.php BSD
7
 *
8
 */
9
namespace Aura\Filter\Rule\Sanitize;
10
11
use Aura\Filter\Rule\AbstractStrlen;
12
13
/**
14
 *
15
 * Sanitizes a string to a length range by padding or chopping it.
16
 *
17
 * @package Aura.Filter
18
 *
19
 */
20
class StrlenBetween extends AbstractStrlen
21
{
22
    /**
23
     *
24
     * Sanitizes a string to a length range by padding or chopping it.
25
     *
26
     * @param object $subject The subject to be filtered.
27
     *
28
     * @param string $field The subject field name.
29
     *
30
     * @param int $min The minimum length.
31
     *
32
     * @param int $max The maximum length.
33
     *
34
     * @param string $pad_string Pad using this string.
35
     *
36
     * @param int $pad_type A `STR_PAD_*` constant.
37
     *
38
     * @return bool True if the value was sanitized, false if not.
39
     *
40
     */
41 13
    public function __invoke($subject, $field, $min, $max, $pad_string = ' ', $pad_type = STR_PAD_RIGHT)
42
    {
43 13
        $value = $subject->$field;
44 13
        if (! is_scalar($value)) {
45 1
            return false;
46
        }
47 12
        if ($this->strlen($value) < $min) {
48 2
            $subject->$field = $this->strpad($value, $min, $pad_string, $pad_type);
49
        }
50 12
        if ($this->strlen($value) > $max) {
51 4
            $subject->$field = $this->substr($value, 0, $max);
52
        }
53 12
        return true;
54
    }
55
}
56