StrlenBetween   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 1
dl 0
loc 36
ccs 9
cts 9
cp 1
rs 10
c 0
b 0
f 0

1 Method

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