Passed
Push — master ( 8a0962...507d84 )
by Paul
03:52
created

ValidationRules::validateNumber()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Modules\Validator;
4
5
use BadMethodCallException;
6
use InvalidArgumentException;
7
8
/**
9
 * @see \Illuminate\Validation\Validator (5.3)
10
 */
11
trait ValidationRules
12
{
13
	/**
14
	 * Get the size of an attribute.
15
	 * @param string $attribute
16
	 * @param mixed $value
17
	 * @return mixed
18
	 */
19
	abstract protected function getSize( $attribute, $value );
20
21
	/**
22
	 * Replace all placeholders.
23
	 * @param string $message
24
	 * @return string
25
	 */
26
	protected function replace( $message, array $parameters )
27
	{
28
		if( strpos( $message, '%s' ) === false ) {
29
			return $message;
30
		}
31
		return preg_replace_callback( '/(%s)/', function() use( &$parameters ) {
32
			foreach( $parameters as $key => $value ) {
33
				return array_shift( $parameters );
34
			}
35
		}, $message );
36
	}
37
38
	/**
39
	 * Validate that an attribute was "accepted".
40
	 * This validation rule implies the attribute is "required".
41
	 * @param string $attribute
42
	 * @param mixed $value
43
	 * @return bool
44
	 */
45 1
	public function validateAccepted( $value )
46
	{
47 1
		$acceptable = ['yes', 'on', '1', 1, true, 'true'];
48 1
		return $this->validateRequired( $value ) && in_array( $value, $acceptable, true );
49
	}
50
51
	/**
52
	 * Validate the size of an attribute is between a set of values.
53
	 * @param string $attribute
54
	 * @param mixed $value
55
	 * @return bool
56
	 */
57 1
	public function validateBetween( $value, $attribute, array $parameters )
58
	{
59 1
		$this->requireParameterCount( 2, $parameters, 'between' );
60 1
		$size = $this->getSize( $attribute, $value );
61 1
		return $size >= $parameters[0] && $size <= $parameters[1];
62
	}
63
64
	/**
65
	 * Validate that an attribute is a valid e-mail address.
66
	 * @param mixed $value
67
	 * @return bool
68
	 */
69 1
	public function validateEmail( $value )
70
	{
71 1
		return filter_var( $value, FILTER_VALIDATE_EMAIL ) !== false;
72
	}
73
74
	/**
75
	 * Validate the size of an attribute is less than a maximum value.
76
	 * @param string $attribute
77
	 * @param mixed $value
78
	 * @return bool
79
	 */
80
	public function validateMax( $value, $attribute, array $parameters )
81
	{
82
		$this->requireParameterCount( 1, $parameters, 'max' );
83
		return $this->getSize( $attribute, $value ) <= $parameters[0];
84
	}
85
86
	/**
87
	 * Validate the size of an attribute is greater than a minimum value.
88
	 * @param string $attribute
89
	 * @param mixed $value
90
	 * @return bool
91
	 */
92
	public function validateMin( $value, $attribute, array $parameters )
93
	{
94
		$this->requireParameterCount( 1, $parameters, 'min' );
95
		return $this->getSize( $attribute, $value ) >= $parameters[0];
96
	}
97
98
	/**
99
	 * Validate that an attribute is numeric.
100
	 * @param mixed $value
101
	 * @return bool
102
	 */
103 1
	public function validateNumber( $value )
104
	{
105 1
		return is_numeric( $value );
106
	}
107
108
	/**
109
	 * Validate that a required attribute exists.
110
	 * @param mixed $value
111
	 * @return bool
112
	 */
113 1
	public function validateRequired( $value )
114
	{
115 1
		return is_null( $value )
116 1
			|| ( is_string( $value ) && trim( $value ) === '' )
117 1
			|| ( is_array( $value ) && count( $value ) < 1 )
118
			? false
119 1
			: true;
120
	}
121
122
	/**
123
	 * Require a certain number of parameters to be present.
124
	 * @param int $count
125
	 * @param string $rule
126
	 * @return void
127
	 * @throws InvalidArgumentException
128
	 */
129 1
	protected function requireParameterCount( $count, array $parameters, $rule )
130
	{
131 1
		if( count( $parameters ) < $count ) {
132
			throw new InvalidArgumentException( "Validation rule $rule requires at least $count parameters." );
133
		}
134 1
	}
135
}
136