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