Passed
Push — master ( e51dc6...12f2fc )
by Rick
01:45
created

Ranges::intBetweenExclusive()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 10
Ratio 100 %

Code Coverage

Tests 5
CRAP Score 4

Importance

Changes 0
Metric Value
dl 10
loc 10
ccs 5
cts 5
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 5
nc 2
nop 2
crap 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
class Ranges
13
{
14
	/**
15
	 * @param int $minimumLength
16
	 * @param int $maximumLength
17
	 *
18
	 * @return \Closure
19
	 */
20 4
	public static function stringWithLengthBetween(int $minimumLength, int $maximumLength): \Closure
21
	{
22 4
		if ($maximumLength < 0 || $maximumLength < 1)
23 1
			throw new \InvalidArgumentException('Minimum length cannot be below 0, maximum length cannot be below 1');
24
25
		return function ($value) use ($minimumLength, $maximumLength)
26
		{
27 3
			return Types::string()($value) && static::intBetween($minimumLength, $maximumLength)(strlen($value));
28 3
		};
29
	}
30
31
	/**
32
	 * @param int $minimum
33
	 * @param int $maximum
34
	 *
35
	 * @return \Closure
36
	 */
37 4 View Code Duplication
	public static function intBetween(int $minimum, int $maximum): \Closure
38
	{
39 4
        if ($maximum <= $minimum)
40 1
            throw new \InvalidArgumentException('Maximum can not be lesser than or equal to minimum.');
41
        
42
		return function ($value) use ($minimum, $maximum)
43
		{
44 4
			return Types::int()($value) && ($value >= $minimum && $value <= $maximum);
45 4
		};
46
	}
47
48
    /**
49
     * @param int $minimum
50
     * @param int $maximum
51
     *
52
     * @return \Closure
53
     */
54 1 View Code Duplication
    public static function intBetweenExclusive(int $minimum, int $maximum): \Closure
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
55
    {
56 1
        if ($maximum <= $minimum)
57 1
            throw new \InvalidArgumentException('Maximum can not be lesser than or equal to minimum.');
58
        
59
        return function ($value) use ($minimum, $maximum)
60
        {
61 1
            return Types::int()($value) && ($value > $minimum && $value < $maximum);
62 1
        };
63
	}
64
65
	/**
66
	 * @param float $minimum
67
	 * @param float $maximum
68
	 *
69
	 * @return \Closure
70
	 */
71 1 View Code Duplication
	public static function floatBetween(float $minimum, float $maximum): \Closure
72
	{
73 1
        if ($maximum <= $minimum)
74 1
            throw new \InvalidArgumentException('Maximum can not be lesser than or equal to minimum.');
75
        
76
		return function ($value) use ($minimum, $maximum)
77
		{
78 1
			return Types::float()($value) && ($value >= $minimum && $value <= $maximum);
79 1
		};
80
	}
81
82
    /**
83
     * @param float $minimum
84
     * @param float $maximum
85
     *
86
     * @return \Closure
87
     */
88 1 View Code Duplication
    public static function floatBetweenExclusive(float $minimum, float $maximum): \Closure
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
89
    {
90 1
        if ($maximum <= $minimum)
91 1
            throw new \InvalidArgumentException('Maximum can not be lesser than or equal to minimum.');
92
        
93
        return function ($value) use ($minimum, $maximum)
94
        {
95 1
            return Types::float()($value) && ($value > $minimum && $value < $maximum);
96 1
        };
97
	}
98
99
	/**
100
	 * @param array ...$allowedValues
101
	 *
102
	 * @return \Closure
103
	 */
0 ignored issues
show
Documentation introduced by
Consider making the type for parameter $allowedValues a bit more specific; maybe use array[].
Loading history...
104 1
	public static function enum(...$allowedValues): \Closure
105
	{
106
		return function ($value) use ($allowedValues)
107
		{
108 1
			return in_array($value, $allowedValues, true);
109 1
		};
110
	}
111
112
	/**
113
	 * @param array ...$allowedTypes
114
	 *
115
	 * @return \Closure
116
	 */
0 ignored issues
show
Documentation introduced by
Consider making the type for parameter $allowedTypes a bit more specific; maybe use array[].
Loading history...
117 1
	public static function typeEnum(...$allowedTypes): \Closure
118
	{
119
		return function ($value) use ($allowedTypes)
120
		{
121 1
			return in_array(gettype($value), $allowedTypes, true);
122 1
		};
123
	}
124
125
	/**
126
	 * @param array ...$allowedValues
127
	 *
128
	 * @return \Closure
129
	 */
0 ignored issues
show
Documentation introduced by
Consider making the type for parameter $allowedValues a bit more specific; maybe use array[].
Loading history...
130 3
	public static function stringOneOf(...$allowedValues): \Closure
131
	{
132 3
		if (!Utils::validateArray(Types::string(), $allowedValues))
133 1
			throw new \InvalidArgumentException('Ranges::stringOneOf expects arguments of type string only');
134
135 2
		return function ($value) use ($allowedValues)
136
		{
137 2
			return Types::string() && in_array($value, $allowedValues);
138 2
		};
139
	}
140
}