Passed
Pull Request — master (#1)
by
unknown
01:47
created

UnsignedInt::filter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 4
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace TraderInteractive\Filter;
4
5
use TraderInteractive\Exceptions\FilterException;
6
7
/**
8
 * A collection of filters for unsigned integers.
9
 */
10
final class UnsignedInt
11
{
12
    /**
13
     * Filters $value to an unsigned integer strictly.
14
     *
15
     * @see \TraderInteractive\Filter\Ints::filter()
16
     *
17
     * @param mixed $value     The value to be checked.
18
     * @param bool  $allowNull Indicates if the value can be null.
19
     * @param int   $minValue  Indicates the minimum acceptable value.
20
     * @param int   $maxValue  Indicates the maximum acceptable value.
21
     *
22
     * @return int|null
23
     *
24
     * @throws \InvalidArgumentException if minvalue is not greater or equal to zero.
25
     * @throws FilterException on failure to filter.
26
     */
27
    public static function filter($value, bool $allowNull = false, int $minValue = null, int $maxValue = PHP_INT_MAX)
28
    {
29
        return Ints::filter($value, $allowNull, self::validateMinValue($minValue), $maxValue);
30
    }
31
32
    private static function validateMinValue($minValue) : int
33
    {
34
        // IFF a user chooses not to include $minValue, or passes null, mandate it returns to 0.
35
        $minValue = $minValue ?: 0;
36
37
        // Validate that the minimum value is positive.
38
        if ($minValue < 0) {
39
            throw new \InvalidArgumentException("{$minValue} was not greater or equal to zero");
40
        }
41
42
        return $minValue;
43
    }
44
}
45