Strlen::__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 5
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 an exact length by padding or chopping it.
16
 *
17
 * @package Aura.Filter
18
 *
19
 */
20
class Strlen extends AbstractStrlen
21
{
22
    /**
23
     *
24
     * Sanitizes a string to an exact length 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 $len The string length.
31
     *
32
     * @param string $pad_string Pad using this string.
33
     *
34
     * @param int $pad_type A `STR_PAD_*` constant.
35
     *
36
     * @return bool True if the value was sanitized, false if not.
37
     *
38
     */
39 9
    public function __invoke($subject, $field, $len, $pad_string = ' ', $pad_type = STR_PAD_RIGHT)
40
    {
41 9
        $value = $subject->$field;
42 9
        if (! is_scalar($value)) {
43 3
            return false;
44
        }
45 8
        if ($this->strlen($value) < $len) {
46 2
            $subject->$field = $this->strpad($value, $len, $pad_string, $pad_type);
47
        }
48 8
        if ($this->strlen($value) > $len) {
49 4
            $subject->$field = $this->substr($value, 0, $len);
50
        }
51 8
        return true;
52
    }
53
}
54