Passed
Push — main ( da5fd2...0b1603 )
by Breno
02:13
created

NumberBetween::evaluate()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 3
c 0
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\Number;
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` (%sincluding the boundaries)';
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 =
21
            $message ??
22
            sprintf(
23
                self::MESSAGE,
24
                $this->min,
25
                $this->max,
26
                !$this->boundaries ? 'not ' : ''
27
            );
28
        parent::__construct($message);
29
    }
30
31
    protected function evaluate($input, array $context = []): bool
32
    {
33
        if ($this->boundaries) {
34
            return $input >= $this->min && $input <= $this->max;
35
        }
36
37
        return $input > $this->min && $input < $this->max;
38
    }
39
}
40