Passed
Push — master ( 7cd9b3...185f29 )
by Dedipyaman
02:32
created

Minimum   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 24
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A validate() 0 12 3
1
<?php
2
/*
3
 * This file is part of Phypes <https://github.com/2DSharp/Phypes>.
4
 *
5
 * (c) Dedipyaman Das <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
12
namespace Phypes\Rule\Number;
13
14
15
use Phypes\Error\RuleError;
16
use Phypes\Error\RuleErrorCode;
17
use Phypes\Result\Failure;
18
use Phypes\Result\Result;
19
use Phypes\Result\Success;
20
use Phypes\Rule\Primitive\Numeric;
21
use Phypes\Rule\Rule;
22
23
class Minimum implements Rule
24
{
25
    /**
26
     * @var float $min
27
     */
28
    private $min;
29
30
    public function __construct(float $min)
31
    {
32
        $this->min = $min;
33
    }
34
35
    public function validate($value): Result
36
    {
37
        $numericResult = (new Numeric())->validate($value);
38
39
        if (!$numericResult->isValid())
40
            return new $numericResult;
41
42
        if ($value >= $this->min)
43
            return new Success();
44
45
        return new Failure(new RuleError(RuleErrorCode::LESS_THAN_MINIMUM,
46
            'The supplied number is less than the minimum value'));
47
    }
48
}