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
![]() |
|||
15 | * @param $string |
||
0 ignored issues
–
show
|
|||
16 | * |
||
17 | * @throws \GloBee\PaymentApi\Exceptions\Validation\InvalidArgumentException |
||
0 ignored issues
–
show
|
|||
18 | */ |
||
0 ignored issues
–
show
|
|||
19 | 20 | public static function validateString($field, $string) |
|
0 ignored issues
–
show
|
|||
20 | { |
||
21 | 20 | if (!is_string($string)) { |
|
22 | 6 | throw new InvalidArgumentException($field, 'string', $string); |
|
23 | } |
||
24 | 19 | } |
|
25 | |||
26 | /** |
||
0 ignored issues
–
show
|
|||
27 | * @param string $customerEmail |
||
0 ignored issues
–
show
|
|||
28 | * |
||
29 | * @throws \GloBee\PaymentApi\Exceptions\Validation\InvalidArgumentException |
||
0 ignored issues
–
show
|
|||
30 | * @throws \GloBee\PaymentApi\Exceptions\Validation\InvalidEmailException |
||
0 ignored issues
–
show
|
|||
31 | */ |
||
0 ignored issues
–
show
|
|||
32 | 18 | public static function validateEmail($field, $customerEmail) |
|
0 ignored issues
–
show
|
|||
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
|
|||
41 | * @param string $string |
||
0 ignored issues
–
show
|
|||
42 | * @param int $length |
||
0 ignored issues
–
show
|
|||
43 | * |
||
44 | * @throws \GloBee\PaymentApi\Exceptions\Validation\InvalidArgumentException |
||
0 ignored issues
–
show
|
|||
45 | * @throws \GloBee\PaymentApi\Exceptions\Validation\ValidationException |
||
0 ignored issues
–
show
|
|||
46 | */ |
||
0 ignored issues
–
show
|
|||
47 | 20 | public static function validateStringLength($field, $string, $length) |
|
0 ignored issues
–
show
|
|||
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
|
|||
56 | * @param $value |
||
0 ignored issues
–
show
|
|||
57 | * |
||
58 | * @throws \GloBee\PaymentApi\Exceptions\Validation\InvalidArgumentException |
||
0 ignored issues
–
show
|
|||
59 | */ |
||
0 ignored issues
–
show
|
|||
60 | 22 | public static function validateNumeric($field, $value) |
|
0 ignored issues
–
show
|
|||
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
|
|||
68 | * @param string $field |
||
0 ignored issues
–
show
|
|||
69 | * @param $value |
||
0 ignored issues
–
show
|
|||
70 | * @param $minimum |
||
0 ignored issues
–
show
|
|||
71 | * |
||
72 | * @throws \GloBee\PaymentApi\Exceptions\Validation\BelowMinimumException |
||
0 ignored issues
–
show
|
|||
73 | * @throws \GloBee\PaymentApi\Exceptions\Validation\InvalidArgumentException |
||
0 ignored issues
–
show
|
|||
74 | */ |
||
0 ignored issues
–
show
|
|||
75 | 22 | public static function validateNumberAboveMinimum($field, $value, $minimum) |
|
0 ignored issues
–
show
|
|||
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
|
|||
85 | * @param string $url |
||
0 ignored issues
–
show
|
|||
86 | * |
||
87 | * @throws \GloBee\PaymentApi\Exceptions\Validation\InvalidUrlException |
||
0 ignored issues
–
show
|
|||
88 | * @throws \GloBee\PaymentApi\Exceptions\Validation\InvalidArgumentException |
||
0 ignored issues
–
show
|
|||
89 | */ |
||
0 ignored issues
–
show
|
|||
90 | 5 | public static function validateUrl($field, $url) |
|
0 ignored issues
–
show
|
|||
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
|
|||
99 | * @param $field |
||
0 ignored issues
–
show
|
|||
100 | * @param $value |
||
0 ignored issues
–
show
|
|||
101 | * @param $validOptions |
||
0 ignored issues
–
show
|
|||
102 | * |
||
103 | * @throws \GloBee\PaymentApi\Exceptions\Validation\InvalidSelectionException |
||
0 ignored issues
–
show
|
|||
104 | */ |
||
0 ignored issues
–
show
|
|||
105 | 1 | public static function validateOptions($field, $value, $validOptions) |
|
0 ignored issues
–
show
|
|||
106 | { |
||
107 | 1 | if (!in_array($value, $validOptions, true)) { |
|
108 | 1 | throw new InvalidSelectionException($field, $value, $validOptions); |
|
109 | } |
||
110 | } |
||
111 | } |
||
112 |