summaryrefslogtreecommitdiff
path: root/src/Http/Validation/Validates.php
blob: 2e3a1a730d51a36c1462a631f85f4cad8144e85c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php

namespace Engelsystem\Http\Validation;

use InvalidArgumentException;

class Validates
{
    /**
     * @param mixed $value
     * @return bool
     */
    public function accepted($value): bool
    {
        return in_array($value, ['true', '1', 'y', 'yes', 'on', 1, true], true);
    }

    /**
     * @param string $value
     * @param array  $parameters ['min', 'max']
     * @return bool
     */
    public function between($value, $parameters): bool
    {
        $this->validateParameterCount(2, $parameters, __FUNCTION__);
        $size = $this->getSize($value);

        return $size >= $parameters[0] && $size <= $parameters[1];
    }

    /**
     * @param mixed $value
     * @return bool
     */
    public function bool($value): bool
    {
        return in_array($value, ['1', 1, true, '0', 0, false], true);
    }

    /**
     * @param mixed $value
     * @param array $parameters ['1,2,3,56,7']
     * @return bool
     */
    public function in($value, $parameters): bool
    {
        $this->validateParameterCount(1, $parameters, __FUNCTION__);

        return in_array($value, explode(',', $parameters[0]));
    }

    /**
     * @param mixed $value
     * @return bool
     */
    public function int($value): bool
    {
        return filter_var($value, FILTER_VALIDATE_INT) !== false;
    }

    /**
     * @param string $value
     * @param array  $parameters ['max']
     * @return bool
     */
    public function max($value, $parameters): bool
    {
        $this->validateParameterCount(1, $parameters, __FUNCTION__);
        $size = $this->getSize($value);

        return $size <= $parameters[0];
    }

    /**
     * @param string $value
     * @param array  $parameters ['min']
     * @return bool
     */
    public function min($value, $parameters)
    {
        $this->validateParameterCount(1, $parameters, __FUNCTION__);
        $size = $this->getSize($value);

        return $size >= $parameters[0];
    }

    /**
     * @param mixed $value
     * @param array $parameters ['1,2,3,56,7']
     * @return bool
     */
    public function notIn($value, $parameters): bool
    {
        $this->validateParameterCount(1, $parameters, __FUNCTION__);

        return !$this->in($value, $parameters);
    }

    /**
     * @param mixed $value
     * @return bool
     */
    public function numeric($value): bool
    {
        return is_numeric($value);
    }

    /**
     * @param mixed $value
     * @return bool
     */
    public function required($value): bool
    {
        if (
            is_null($value)
            || (is_string($value) && trim($value) === '')
        ) {
            return false;
        }

        return true;
    }

    /**
     * @param mixed $value
     * @return int|float
     */
    protected function getSize($value)
    {
        if (is_numeric($value)) {
            return $value;
        }

        return mb_strlen($value);
    }

    /**
     * @param int    $count
     * @param array  $parameters
     * @param string $rule
     *
     * @throws InvalidArgumentException
     */
    protected function validateParameterCount(int $count, array $parameters, string $rule)
    {
        if (count($parameters) < $count) {
            throw new InvalidArgumentException(sprintf(
                'The rule "%s" requires at least %d parameters',
                $rule,
                $count
            ));
        }
    }
}