1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
|
3
|
|
|
namespace SlevomatCsobGateway; |
4
|
|
|
|
5
|
|
|
class Validator |
6
|
|
|
{ |
7
|
|
|
|
8
|
|
|
const CART_ITEM_NAME_LENGTH_MAX = 20; |
9
|
|
|
const CART_ITEM_DESCRIPTION_LENGTH_MAX = 40; |
10
|
|
|
|
11
|
|
|
const ORDER_ID_LENGTH_MAX = 10; |
12
|
|
|
const RETURN_URL_LENGTH_MAX = 300; |
13
|
|
|
const DESCRIPTION_LENGTH_MAX = 255; |
14
|
|
|
const MERCHANT_DATA_LENGTH_MAX = 255; |
15
|
|
|
const CUSTOMER_ID_LENGTH_MAX = 50; |
16
|
|
|
const PAY_ID_LENGTH_MAX = 15; |
17
|
|
|
|
18
|
|
|
const TTL_SEC_MIN = 300; |
19
|
|
|
const TTL_SEC_MAX = 1800; |
20
|
|
|
|
21
|
5 |
View Code Duplication |
public static function checkCartItemName(string $name) |
|
|
|
|
22
|
|
|
{ |
23
|
5 |
|
self::checkWhitespaces($name); |
24
|
|
|
|
25
|
5 |
|
if (strlen(utf8_decode($name)) > self::CART_ITEM_NAME_LENGTH_MAX) { |
26
|
2 |
|
throw new \InvalidArgumentException(sprintf('Cart item name can have maximum of %d characters.', self::CART_ITEM_NAME_LENGTH_MAX)); |
27
|
|
|
} |
28
|
5 |
|
} |
29
|
|
|
|
30
|
4 |
View Code Duplication |
public static function checkCartItemDescription(string $description) |
|
|
|
|
31
|
|
|
{ |
32
|
4 |
|
self::checkWhitespaces($description); |
33
|
|
|
|
34
|
4 |
|
if (strlen(utf8_decode($description)) > self::CART_ITEM_DESCRIPTION_LENGTH_MAX) { |
35
|
2 |
|
throw new \InvalidArgumentException(sprintf('Cart item description can have maximum of %d characters.', self::CART_ITEM_DESCRIPTION_LENGTH_MAX)); |
36
|
|
|
} |
37
|
3 |
|
} |
38
|
|
|
|
39
|
5 |
|
public static function checkCartItemQuantity(int $quantity) |
40
|
|
|
{ |
41
|
5 |
|
if ($quantity < 1) { |
42
|
2 |
|
throw new \InvalidArgumentException(sprintf( |
43
|
2 |
|
'Quantity must be greater than 0. %d given.', |
44
|
|
|
$quantity |
45
|
|
|
)); |
46
|
|
|
} |
47
|
4 |
|
} |
48
|
|
|
|
49
|
5 |
|
public static function checkOrderId(string $orderId) |
50
|
|
|
{ |
51
|
5 |
|
self::checkWhitespaces($orderId); |
52
|
|
|
|
53
|
5 |
|
if (!ctype_digit($orderId)) { |
54
|
1 |
|
throw new \InvalidArgumentException(sprintf( |
55
|
1 |
|
'OrderId must be numeric value. %s given.', |
56
|
|
|
$orderId |
57
|
|
|
)); |
58
|
|
|
} |
59
|
|
|
|
60
|
5 |
|
if (strlen($orderId) > self::ORDER_ID_LENGTH_MAX) { |
61
|
1 |
|
throw new \InvalidArgumentException(sprintf('OrderId can have maximum of %d characters.', self::ORDER_ID_LENGTH_MAX)); |
62
|
|
|
} |
63
|
|
|
|
64
|
5 |
|
} |
65
|
|
|
|
66
|
3 |
View Code Duplication |
public static function checkReturnUrl(string $returnUrl) |
|
|
|
|
67
|
|
|
{ |
68
|
3 |
|
self::checkWhitespaces($returnUrl); |
69
|
|
|
|
70
|
3 |
|
if (strlen(utf8_decode($returnUrl)) > self::RETURN_URL_LENGTH_MAX) { |
71
|
1 |
|
throw new \InvalidArgumentException(sprintf('ReturnUrl can have maximum of %d characters.', self::RETURN_URL_LENGTH_MAX)); |
72
|
|
|
} |
73
|
3 |
|
} |
74
|
|
|
|
75
|
5 |
View Code Duplication |
public static function checkDescription(string $description) |
|
|
|
|
76
|
|
|
{ |
77
|
5 |
|
self::checkWhitespaces($description); |
78
|
|
|
|
79
|
5 |
|
if (strlen(utf8_decode($description)) > self::DESCRIPTION_LENGTH_MAX) { |
80
|
1 |
|
throw new \InvalidArgumentException(sprintf('Description can have maximum of %d characters.', self::DESCRIPTION_LENGTH_MAX)); |
81
|
|
|
} |
82
|
5 |
|
} |
83
|
|
|
|
84
|
4 |
View Code Duplication |
public static function checkMerchantData(string $merchantData) |
|
|
|
|
85
|
|
|
{ |
86
|
4 |
|
self::checkWhitespaces($merchantData); |
87
|
|
|
|
88
|
4 |
|
if (strlen(utf8_decode(base64_encode($merchantData))) > self::MERCHANT_DATA_LENGTH_MAX) { |
89
|
1 |
|
throw new \InvalidArgumentException(sprintf('MerchantData can have maximum of %d characters in encoded state.', self::MERCHANT_DATA_LENGTH_MAX)); |
90
|
|
|
} |
91
|
4 |
|
} |
92
|
|
|
|
93
|
5 |
View Code Duplication |
public static function checkCustomerId(string $customerId) |
|
|
|
|
94
|
|
|
{ |
95
|
5 |
|
self::checkWhitespaces($customerId); |
96
|
|
|
|
97
|
5 |
|
if (strlen(utf8_decode($customerId)) > self::CUSTOMER_ID_LENGTH_MAX) { |
98
|
1 |
|
throw new \InvalidArgumentException(sprintf('CustomerId can have maximum of %d characters.', self::CUSTOMER_ID_LENGTH_MAX)); |
99
|
|
|
} |
100
|
5 |
|
} |
101
|
|
|
|
102
|
19 |
View Code Duplication |
public static function checkPayId(string $payId) |
|
|
|
|
103
|
|
|
{ |
104
|
19 |
|
self::checkWhitespaces($payId); |
105
|
|
|
|
106
|
19 |
|
if (strlen(utf8_decode($payId)) > self::PAY_ID_LENGTH_MAX) { |
107
|
1 |
|
throw new \InvalidArgumentException(sprintf('PayId can have maximum of %d characters.', self::PAY_ID_LENGTH_MAX)); |
108
|
|
|
} |
109
|
19 |
|
} |
110
|
|
|
|
111
|
31 |
|
private static function checkWhitespaces(string $argument) |
112
|
|
|
{ |
113
|
31 |
|
$charlist = preg_quote(" \t\n\r\0\x0B\xC2\xA0", '#'); |
114
|
31 |
|
preg_replace('#^[' . $charlist . ']+|[' . $charlist . ']+\z#u', '', $argument); |
115
|
|
|
|
116
|
31 |
|
if ($argument !== preg_replace('#^[' . $charlist . ']+|[' . $charlist . ']+\z#u', '', $argument)) { |
117
|
1 |
|
throw new \InvalidArgumentException('Argument starts or ends with whitespace.'); |
118
|
|
|
} |
119
|
31 |
|
} |
120
|
|
|
|
121
|
3 |
|
public static function checkTtlSec(int $ttlSec) |
122
|
|
|
{ |
123
|
3 |
|
if ($ttlSec < self::TTL_SEC_MIN || $ttlSec > self::TTL_SEC_MAX) { |
124
|
1 |
|
throw new \InvalidArgumentException(sprintf('TTL sec is out of range (%d - %d). Current value is %d.', self::TTL_SEC_MIN, self::TTL_SEC_MAX, $ttlSec)); |
125
|
|
|
} |
126
|
3 |
|
} |
127
|
|
|
|
128
|
|
|
} |
129
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.