Between::__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 4
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
/**
12
 *
13
 * Validates that a value is within a given range.
14
 *
15
 * @package Aura.Filter
16
 *
17
 */
18
class Between
19
{
20
    /**
21
     *
22
     * If the value is less than min, will set the min value,
23
     * and if value is greater than max, set the max value.
24
     *
25
     * @param object $subject The subject to be filtered.
26
     *
27
     * @param string $field The subject field name.
28
     *
29
     * @param mixed $min The minimum valid value.
30
     *
31
     * @param mixed $max The maximum valid value.
32
     *
33
     * @return bool True if the value was sanitized, false if not.
34
     *
35
     */
36 8
    public function __invoke($subject, $field, $min, $max)
37
    {
38 8
        $value = $subject->$field;
39 8
        if (! is_scalar($value)) {
40 1
            return false;
41
        }
42 7
        if ($value < $min) {
43 2
            $subject->$field = $min;
44
        }
45 7
        if ($value > $max) {
46 2
            $subject->$field = $max;
47
        }
48 7
        return true;
49
    }
50
}
51