Passed
Push — master ( 95cb8b...ff74ae )
by Rick
02:47
created

Ranges::enum()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
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
	public static function intBetween(int $minimum, int $maximum): \Closure
38
	{
39
		return function ($value) use ($minimum, $maximum)
40
		{
41 4
			return Types::int()($value) && ($value >= $minimum && $value <= $maximum);
42 4
		};
43
	}
44
45
	/**
46
	 * @param array ...$allowedValues
47
	 *
48
	 * @return \Closure
49
	 */
0 ignored issues
show
Documentation introduced by
Consider making the type for parameter $allowedValues a bit more specific; maybe use array[].
Loading history...
50 1
	public static function enum(...$allowedValues): \Closure
51
	{
52
		return function ($value) use ($allowedValues)
53
		{
54 1
			return in_array($value, $allowedValues, true);
55 1
		};
56
	}
57
58
	/**
59
	 * @param array ...$allowedTypes
60
	 *
61
	 * @return \Closure
62
	 */
0 ignored issues
show
Documentation introduced by
Consider making the type for parameter $allowedTypes a bit more specific; maybe use array[].
Loading history...
63 1
	public static function typeEnum(...$allowedTypes): \Closure
64
	{
65
		return function ($value) use ($allowedTypes)
66
		{
67 1
			return in_array(gettype($value), $allowedTypes, true);
68 1
		};
69
	}
70
71
	/**
72
	 * @param array ...$allowedValues
73
	 *
74
	 * @return \Closure
75
	 */
0 ignored issues
show
Documentation introduced by
Consider making the type for parameter $allowedValues a bit more specific; maybe use array[].
Loading history...
76 3
	public static function stringOneOf(...$allowedValues): \Closure
77
	{
78 3
		if (!Utils::validateArray(Types::string(), $allowedValues))
79 1
			throw new \InvalidArgumentException('Ranges::stringOneOf expects arguments of type string only');
80
81 2
		return function ($value) use ($allowedValues)
82
		{
83 2
			return Types::string() && in_array($value, $allowedValues);
84 2
		};
85
	}
86
}