Ranges   A
last analyzed

Complexity

Total Complexity 25

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 25
eloc 29
dl 0
loc 132
ccs 41
cts 41
cp 1
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A enum() 0 5 1
A floatBetweenExclusive() 0 9 4
A floatBetween() 0 9 4
A intBetween() 0 9 4
A typeEnum() 0 5 1
A stringOneOf() 0 9 3
A stringWithLengthBetween() 0 9 4
A intBetweenExclusive() 0 9 4
1
<?php
2
/**
3
 * Copyright 2017 NanoSector
4
 *
5
 * You should have received a copy of the MIT license with the project.
6
 * See the LICENSE file for more information.
7
 */
8
9
namespace ValidationClosures;
10
11
12
use Closure;
13
use InvalidArgumentException;
14
15
class Ranges
16
{
17
	/**
18
	 * @param int $minimumLength
19
	 * @param int $maximumLength
20
	 *
21
	 * @return Closure
22
	 */
23 8
	public static function stringWithLengthBetween(int $minimumLength, int $maximumLength): Closure
24
	{
25 8
		if ($maximumLength < 0 || $maximumLength < 1) {
26 2
            throw new InvalidArgumentException('Minimum length cannot be below 0, maximum length cannot be below 1');
27
        }
28
29 6
		return static function ($value) use ($minimumLength, $maximumLength)
30
		{
31 6
			return Types::string()($value) && static::intBetween($minimumLength, $maximumLength)(strlen($value));
32 6
		};
33
	}
34
35
	/**
36
	 * @param int $minimum
37
	 * @param int $maximum
38
	 *
39
	 * @return Closure
40
	 */
41 8
	public static function intBetween(int $minimum, int $maximum): Closure
42
	{
43 8
        if ($maximum <= $minimum) {
44 2
            throw new InvalidArgumentException('Maximum can not be lesser than or equal to minimum.');
45
        }
46
        
47 8
		return static function ($value) use ($minimum, $maximum)
48
		{
49 8
			return Types::int()($value) && ($value >= $minimum && $value <= $maximum);
50 8
		};
51
	}
52
53
    /**
54
     * @param int $minimum
55
     * @param int $maximum
56
     *
57
     * @return Closure
58
     */
59 2
    public static function intBetweenExclusive(int $minimum, int $maximum): Closure
60
    {
61 2
        if ($maximum <= $minimum) {
62 2
            throw new InvalidArgumentException('Maximum can not be lesser than or equal to minimum.');
63
        }
64
        
65 2
        return static function ($value) use ($minimum, $maximum)
66
        {
67 2
            return Types::int()($value) && ($value > $minimum && $value < $maximum);
68 2
        };
69
	}
70
71
	/**
72
	 * @param float $minimum
73
	 * @param float $maximum
74
	 *
75
	 * @return Closure
76
	 */
77 2
	public static function floatBetween(float $minimum, float $maximum): Closure
78
	{
79 2
        if ($maximum <= $minimum) {
80 2
            throw new InvalidArgumentException('Maximum can not be lesser than or equal to minimum.');
81
        }
82
        
83 2
		return static function ($value) use ($minimum, $maximum)
84
		{
85 2
			return Types::float()($value) && ($value >= $minimum && $value <= $maximum);
86 2
		};
87
	}
88
89
    /**
90
     * @param float $minimum
91
     * @param float $maximum
92
     *
93
     * @return Closure
94
     */
95 2
    public static function floatBetweenExclusive(float $minimum, float $maximum): Closure
96
    {
97 2
        if ($maximum <= $minimum) {
98 2
            throw new InvalidArgumentException('Maximum can not be lesser than or equal to minimum.');
99
        }
100
        
101 2
        return static function ($value) use ($minimum, $maximum)
102
        {
103 2
            return Types::float()($value) && ($value > $minimum && $value < $maximum);
104 2
        };
105
	}
106
107
	/**
108
	 * @param array ...$allowedValues
109
	 *
110
	 * @return Closure
111
	 */
112
	public static function enum(...$allowedValues): Closure
113
	{
114 2
		return static function ($value) use ($allowedValues)
115
		{
116 2
			return in_array($value, $allowedValues, true);
117 2
		};
118
	}
119
120
	/**
121
	 * @param array ...$allowedTypes
122
	 *
123
	 * @return Closure
124
	 */
125
	public static function typeEnum(...$allowedTypes): Closure
126
	{
127 2
		return static function ($value) use ($allowedTypes)
128
		{
129 2
			return in_array(gettype($value), $allowedTypes, true);
130 2
		};
131
	}
132
133
	/**
134
	 * @param array ...$allowedValues
135
	 *
136
	 * @return Closure
137
	 */
138 6
	public static function stringOneOf(...$allowedValues): Closure
139
	{
140 6
		if (!Utils::validateArray(Types::string(), $allowedValues)) {
141 2
            throw new InvalidArgumentException('Ranges::stringOneOf expects arguments of type string only');
142
        }
143
144 4
		return static function ($value) use ($allowedValues)
145
		{
146 4
			return Types::string() && in_array($value, $allowedValues, true);
147 4
		};
148
	}
149
}