Code Duplication    Length = 31-36 lines in 3 locations

src/Rule/Sanitize/Max.php 1 location

@@ 5-38 (lines=34) @@
2
3
namespace Mbright\Validation\Rule\Sanitize;
4
5
class Max implements SanitizeRuleInterface
6
{
7
    /** @var int */
8
    protected $max;
9
10
    /**
11
     * @param int $max
12
     */
13
    public function __construct(int $max)
14
    {
15
        $this->max = $max;
16
    }
17
18
    /**
19
     * Sanitizes to maximum value if value is greater than max.
20
     *
21
     * @param object $subject The subject to be filtered.
22
     * @param string $field The subject field name.
23
     *
24
     * @return bool True if the value was sanitized, false if not.
25
     */
26
    public function __invoke($subject, string $field): bool
27
    {
28
        $value = $subject->$field;
29
        if (!is_scalar($value)) {
30
            return false;
31
        }
32
        if ($value > $this->max) {
33
            $subject->$field = $this->max;
34
        }
35
36
        return true;
37
    }
38
}
39

src/Rule/Sanitize/Min.php 1 location

@@ 5-35 (lines=31) @@
2
3
namespace Mbright\Validation\Rule\Sanitize;
4
5
class Min implements SanitizeRuleInterface
6
{
7
    /** @var int */
8
    protected $min;
9
10
    public function __construct(int $min)
11
    {
12
        $this->min = $min;
13
    }
14
15
    /**
16
     * Sanitizes to minimum value if value is less than min.
17
     *
18
     * @param object $subject The subject to be filtered.
19
     * @param string $field The subject field name.
20
     *
21
     * @return bool True if the value was sanitized, false if not.
22
     */
23
    public function __invoke($subject, string $field): bool
24
    {
25
        $value = $subject->$field;
26
        if (!is_scalar($value)) {
27
            return false;
28
        }
29
        if ($value < $this->min) {
30
            $subject->$field = $this->min;
31
        }
32
33
        return true;
34
    }
35
}
36

src/Rule/Validate/Between.php 1 location

@@ 5-40 (lines=36) @@
2
3
namespace Mbright\Validation\Rule\Validate;
4
5
class Between implements ValidateRuleInterface
6
{
7
    /** @var int */
8
    protected $min;
9
    
10
    /** @var int */
11
    protected $max;
12
13
    /**
14
     * @param int $min minimum 'floor' value
15
     * @param int $max maximum 'ceiling' value
16
     */
17
    public function __construct(int $min, int $max)
18
    {
19
        $this->min = $min;
20
        $this->max = $max;
21
    }
22
23
    /**
24
     * Validates that a field's value is between the given min and max.
25
     *
26
     * @param $subject
27
     * @param string $field
28
     *
29
     * @return bool
30
     */
31
    public function __invoke($subject, string $field): bool
32
    {
33
        $value = $subject->$field;
34
        if (!is_scalar($value)) {
35
            return false;
36
        }
37
38
        return ($value >= $this->min && $value <= $this->max);
39
    }
40
}
41