Passed
Push — main ( 577c50...0a0e8d )
by Breno
01:38
created

NumberBetween::evaluate()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 3
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 7
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace BrenoRoosevelt\Validation\Rules\Comparison;
5
6
use Attribute;
7
use BrenoRoosevelt\Validation\AbstractValidation;
8
9
#[Attribute(Attribute::TARGET_PROPERTY)]
10
class NumberBetween extends AbstractValidation
11
{
12
    const MESSAGE = 'the value should be between `%s` and `%s` (%s inclusive)';
13
14
    public function __construct(
15
        private int|float $min,
16
        private int|float $max,
17
        private $boundaries = true,
18
        ?string $message = null
19
    ) {
20
        $this->message = $message ?? sprintf(
21
            self::MESSAGE,
22
            $this->min,
23
                $this->max,
24
                !$this->boundaries ? 'not' : ''
25
            );
26
        parent::__construct($message);
27
    }
28
29
    protected function evaluate($input, array $context = []): bool
30
    {
31
        if ($this->boundaries) {
32
            return $input >= $this->min && $input <= $this->max;
33
        }
34
35
        return $input > $this->min && $input < $this->max;
36
    }
37
}
38