Issues (215)

src/Models/Validator.php (60 issues)

1
<?php
2
3
namespace GloBee\PaymentApi\Models;
4
5
use GloBee\PaymentApi\Exceptions\Validation\BelowMinimumException;
6
use GloBee\PaymentApi\Exceptions\Validation\InvalidArgumentException;
7
use GloBee\PaymentApi\Exceptions\Validation\InvalidEmailException;
8
use GloBee\PaymentApi\Exceptions\Validation\InvalidSelectionException;
9
use GloBee\PaymentApi\Exceptions\Validation\InvalidUrlException;
10
use GloBee\PaymentApi\Exceptions\Validation\ValidationException;
11
12
class Validator
13
{
14
    /**
0 ignored issues
show
Coding Style Documentation introduced by
Doc comment for parameter "$field" missing
Loading history...
Coding Style Documentation introduced by
Doc comment for parameter "$string" missing
Loading history...
15
     * @param $string
0 ignored issues
show
Missing parameter name
Loading history...
16
     *
17
     * @throws \GloBee\PaymentApi\Exceptions\Validation\InvalidArgumentException
0 ignored issues
show
Comment missing for @throws tag in function comment
Loading history...
18
     */
0 ignored issues
show
Coding Style Documentation introduced by
Missing @return tag in function comment
Loading history...
19 20
    public static function validateString($field, $string)
0 ignored issues
show
Type hint "string" missing for
Loading history...
20
    {
21 20
        if (!is_string($string)) {
22 6
            throw new InvalidArgumentException($field, 'string', $string);
23
        }
24 19
    }
25
26
    /**
0 ignored issues
show
Coding Style Documentation introduced by
Doc comment for parameter "$field" missing
Loading history...
27
     * @param string $customerEmail
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
Doc comment for parameter $customerEmail does not match actual variable name $field
Loading history...
28
     *
29
     * @throws \GloBee\PaymentApi\Exceptions\Validation\InvalidArgumentException
0 ignored issues
show
Comment missing for @throws tag in function comment
Loading history...
30
     * @throws \GloBee\PaymentApi\Exceptions\Validation\InvalidEmailException
0 ignored issues
show
Comment missing for @throws tag in function comment
Loading history...
31
     */
0 ignored issues
show
Coding Style Documentation introduced by
Missing @return tag in function comment
Loading history...
32 18
    public static function validateEmail($field, $customerEmail)
0 ignored issues
show
Type hint "string" missing for $customerEmail
Loading history...
33
    {
34 18
        self::validateString($field, $customerEmail);
35 18
        if (filter_var($customerEmail, FILTER_VALIDATE_EMAIL) === false) {
36 2
            throw new InvalidEmailException($field, $customerEmail);
37
        }
38 17
    }
39
40
    /**
0 ignored issues
show
Coding Style Documentation introduced by
Doc comment for parameter "$field" missing
Loading history...
41
     * @param string $string
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
Doc comment for parameter $string does not match actual variable name $field
Loading history...
42
     * @param int    $length
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
Expected "integer" but found "int" for parameter type
Loading history...
Doc comment for parameter $length does not match actual variable name $string
Loading history...
43
     *
44
     * @throws \GloBee\PaymentApi\Exceptions\Validation\InvalidArgumentException
0 ignored issues
show
Comment missing for @throws tag in function comment
Loading history...
45
     * @throws \GloBee\PaymentApi\Exceptions\Validation\ValidationException
0 ignored issues
show
Comment missing for @throws tag in function comment
Loading history...
46
     */
0 ignored issues
show
Coding Style Documentation introduced by
Missing @return tag in function comment
Loading history...
47 20
    public static function validateStringLength($field, $string, $length)
0 ignored issues
show
Type hint "string" missing for $string
Loading history...
Type hint "int" missing for $length
Loading history...
48
    {
49 20
        self::validateString($field, $string);
50 19
        if (strlen($string) !== $length) {
51 1
            throw new ValidationException([], 'Currency must be a 3 character string.');
52
        }
53 18
    }
54
55
    /**
0 ignored issues
show
Coding Style Documentation introduced by
Doc comment for parameter "$field" missing
Loading history...
Coding Style Documentation introduced by
Doc comment for parameter "$value" missing
Loading history...
56
     * @param $value
0 ignored issues
show
Missing parameter name
Loading history...
57
     *
58
     * @throws \GloBee\PaymentApi\Exceptions\Validation\InvalidArgumentException
0 ignored issues
show
Comment missing for @throws tag in function comment
Loading history...
59
     */
0 ignored issues
show
Coding Style Documentation introduced by
Missing @return tag in function comment
Loading history...
60 22
    public static function validateNumeric($field, $value)
0 ignored issues
show
Type hint "value" missing for
Loading history...
61
    {
62 22
        if (!is_numeric($value)) {
63 1
            throw new InvalidArgumentException($field, 'number', gettype($value));
64
        }
65 21
    }
66
67
    /**
0 ignored issues
show
Coding Style Documentation introduced by
Doc comment for parameter "$value" missing
Loading history...
Coding Style Documentation introduced by
Doc comment for parameter "$minimum" missing
Loading history...
68
     * @param string $field
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
Expected 2 spaces after parameter type; 1 found
Loading history...
69
     * @param        $value
0 ignored issues
show
Missing parameter name
Loading history...
70
     * @param        $minimum
0 ignored issues
show
Missing parameter name
Loading history...
71
     *
72
     * @throws \GloBee\PaymentApi\Exceptions\Validation\BelowMinimumException
0 ignored issues
show
Comment missing for @throws tag in function comment
Loading history...
73
     * @throws \GloBee\PaymentApi\Exceptions\Validation\InvalidArgumentException
0 ignored issues
show
Comment missing for @throws tag in function comment
Loading history...
74
     */
0 ignored issues
show
Coding Style Documentation introduced by
Missing @return tag in function comment
Loading history...
75 22
    public static function validateNumberAboveMinimum($field, $value, $minimum)
0 ignored issues
show
Type hint "string" missing for $field
Loading history...
Type hint "value" missing for
Loading history...
Type hint "minimum" missing for
Loading history...
76
    {
77 22
        self::validateNumeric($field, $value);
78 21
        if ($value <= $minimum) {
79 1
            throw new BelowMinimumException($field, $value, $minimum);
80
        }
81 20
    }
82
83
    /**
84
     * @param string $field
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
85
     * @param string $url
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
86
     *
87
     * @throws \GloBee\PaymentApi\Exceptions\Validation\InvalidUrlException
0 ignored issues
show
Comment missing for @throws tag in function comment
Loading history...
88
     * @throws \GloBee\PaymentApi\Exceptions\Validation\InvalidArgumentException
0 ignored issues
show
Comment missing for @throws tag in function comment
Loading history...
89
     */
0 ignored issues
show
Coding Style Documentation introduced by
Missing @return tag in function comment
Loading history...
90 5
    public static function validateUrl($field, $url)
0 ignored issues
show
Type hint "string" missing for $field
Loading history...
Type hint "string" missing for $url
Loading history...
91
    {
92 5
        self::validateString($field, $url);
93 5
        if (filter_var($url, FILTER_VALIDATE_URL) === false) {
94 3
            throw new InvalidUrlException($field, $url);
95
        }
96 2
    }
97
98
    /**
0 ignored issues
show
Coding Style Documentation introduced by
Doc comment for parameter "$field" missing
Loading history...
Coding Style Documentation introduced by
Doc comment for parameter "$value" missing
Loading history...
Coding Style Documentation introduced by
Doc comment for parameter "$validOptions" missing
Loading history...
99
     * @param $field
0 ignored issues
show
Missing parameter name
Loading history...
100
     * @param $value
0 ignored issues
show
Missing parameter name
Loading history...
101
     * @param $validOptions
0 ignored issues
show
Missing parameter name
Loading history...
102
     *
103
     * @throws \GloBee\PaymentApi\Exceptions\Validation\InvalidSelectionException
0 ignored issues
show
Comment missing for @throws tag in function comment
Loading history...
104
     */
0 ignored issues
show
Coding Style Documentation introduced by
Missing @return tag in function comment
Loading history...
105 1
    public static function validateOptions($field, $value, $validOptions)
0 ignored issues
show
Type hint "field" missing for
Loading history...
Type hint "value" missing for
Loading history...
Type hint "validOptions" missing for
Loading history...
106
    {
107 1
        if (!in_array($value, $validOptions, true)) {
108 1
            throw new InvalidSelectionException($field, $value, $validOptions);
109
        }
110
    }
111
}
112