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