Passed
Push — master ( 817cab...57b58d )
by Paul
09:30 queued 02:21
created

ValidationRules::validateRequired()   A

Complexity

Conditions 6
Paths 14

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 6

Importance

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