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
|
|
|
trait ValidationTrait |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @param $string |
16
|
|
|
* |
17
|
|
|
* @throws \GloBee\PaymentApi\Exceptions\Validation\InvalidArgumentException |
18
|
|
|
*/ |
19
|
13 |
|
protected function validateString($field, $string) |
20
|
|
|
{ |
21
|
13 |
|
if (!is_string($string)) { |
22
|
6 |
|
throw new InvalidArgumentException($field, 'string', $string); |
23
|
|
|
} |
24
|
12 |
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param string $customerEmail |
28
|
|
|
* |
29
|
|
|
* @throws \GloBee\PaymentApi\Exceptions\Validation\InvalidArgumentException |
30
|
|
|
* @throws \GloBee\PaymentApi\Exceptions\Validation\InvalidEmailException |
31
|
|
|
*/ |
32
|
7 |
|
protected function validateEmail($field, $customerEmail) |
33
|
|
|
{ |
34
|
7 |
|
$this->validateString($field, $customerEmail); |
35
|
7 |
|
if (filter_var($customerEmail, FILTER_VALIDATE_EMAIL) === false) { |
36
|
2 |
|
throw new InvalidEmailException($field, $customerEmail); |
37
|
|
|
} |
38
|
5 |
|
} |
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
|
5 |
|
protected function validateStringLength($field, $string, $length) |
48
|
|
|
{ |
49
|
5 |
|
$this->validateString($field, $string); |
50
|
4 |
|
if (strlen($string) !== $length) { |
51
|
1 |
|
throw new ValidationException([], 'Currency must be a 3 character string.'); |
52
|
|
|
} |
53
|
3 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param $value |
57
|
|
|
* |
58
|
|
|
* @throws \GloBee\PaymentApi\Exceptions\Validation\InvalidArgumentException |
59
|
|
|
*/ |
60
|
7 |
|
protected function validateNumeric($field, $value) |
61
|
|
|
{ |
62
|
7 |
|
if (!is_numeric($value)) { |
63
|
1 |
|
throw new InvalidArgumentException($field, 'number', gettype($value)); |
64
|
|
|
} |
65
|
6 |
|
} |
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
|
7 |
|
protected function validateNumberAboveMinimum($field, $value, $minimum) |
76
|
|
|
{ |
77
|
7 |
|
$this->validateNumeric($field, $value); |
78
|
6 |
|
if ($value <= $minimum) { |
79
|
1 |
|
throw new BelowMinimumException($field, $value, $minimum); |
80
|
|
|
} |
81
|
5 |
|
} |
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 |
|
protected function validateUrl($field, $url) |
91
|
|
|
{ |
92
|
5 |
|
$this->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 $value |
100
|
|
|
* @param $validOptions |
101
|
|
|
* |
102
|
|
|
* @throws \GloBee\PaymentApi\Exceptions\Validation\InvalidSelectionException |
103
|
|
|
*/ |
104
|
3 |
|
protected function validateOptions($field, $value, $validOptions) |
105
|
|
|
{ |
106
|
3 |
|
if (!in_array($value, $validOptions, true)) { |
107
|
1 |
|
throw new InvalidSelectionException($field, $value, $validOptions); |
108
|
|
|
} |
109
|
2 |
|
} |
110
|
|
|
} |
111
|
|
|
|