Strlen   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
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 34
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 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