|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Data helper |
|
4
|
|
|
* |
|
5
|
|
|
* @author Pronamic <[email protected]> |
|
6
|
|
|
* @copyright 2005-2019 Pronamic |
|
7
|
|
|
* @license GPL-3.0-or-later |
|
8
|
|
|
* @package Pronamic\WordPress\Pay\Gateways\OmniKassa2 |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Pronamic\WordPress\Pay\Gateways\OmniKassa2; |
|
12
|
|
|
|
|
13
|
|
|
use InvalidArgumentException; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Data helper |
|
17
|
|
|
* |
|
18
|
|
|
* @link https://github.com/wp-pay-gateways/ideal-basic/blob/2.0.0/src/DataHelper.php |
|
19
|
|
|
* @author Remco Tolsma |
|
20
|
|
|
* @version 2.1.0 |
|
21
|
|
|
* @since 2.0.2 |
|
22
|
|
|
*/ |
|
23
|
|
|
class DataHelper { |
|
24
|
|
|
/** |
|
25
|
|
|
* Validate AN..$max. |
|
26
|
|
|
* |
|
27
|
|
|
* @param string $value Value to validate. |
|
28
|
|
|
* @param int $max Max length of value. |
|
29
|
|
|
* |
|
30
|
|
|
* @return bool |
|
31
|
|
|
* |
|
32
|
|
|
* @throws InvalidArgumentException Throws invalid argument exception when string is longer then max length. |
|
33
|
|
|
*/ |
|
34
|
4 |
|
public static function validate_an( $value, $max ) { |
|
35
|
4 |
|
if ( mb_strlen( $value, 'UTF-8' ) > $max ) { |
|
36
|
|
|
throw new InvalidArgumentException( |
|
37
|
|
|
sprintf( |
|
38
|
|
|
'Value "%s" can not be longer then `%d`.', |
|
39
|
|
|
$value, |
|
40
|
|
|
$max |
|
41
|
|
|
) |
|
42
|
|
|
); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
4 |
|
return true; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Validate null or AN..$max. |
|
50
|
|
|
* |
|
51
|
|
|
* @param string|null $value Value to validate. |
|
52
|
|
|
* @param int $max Max length of value. |
|
53
|
|
|
* |
|
54
|
|
|
* @return bool |
|
55
|
|
|
* |
|
56
|
|
|
* @throws InvalidArgumentException Throws invalid argument exception when value is not null and longer then max length. |
|
57
|
|
|
*/ |
|
58
|
1 |
|
public static function validate_null_or_an( $value, $max ) { |
|
59
|
1 |
|
if ( null === $value ) { |
|
60
|
|
|
return true; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
1 |
|
return self::validate_an( $value, $max ); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Validate HTML special chars. |
|
68
|
|
|
* |
|
69
|
|
|
* @param string $value Value to validate. |
|
70
|
|
|
* |
|
71
|
|
|
* @return bool |
|
72
|
|
|
* |
|
73
|
|
|
* @throws InvalidArgumentException Throws invalid argument exception when value contains HTML special chars. |
|
74
|
|
|
*/ |
|
75
|
2 |
|
public static function validate_html_special_chars( $value ) { |
|
76
|
2 |
|
$html_special_chars = self::get_html_special_chars(); |
|
77
|
|
|
|
|
78
|
2 |
|
foreach ( $html_special_chars as $char ) { |
|
79
|
2 |
|
if ( false !== mb_strpos( $value, $char ) ) { |
|
80
|
1 |
|
throw new InvalidArgumentException( |
|
81
|
1 |
|
sprintf( |
|
82
|
1 |
|
'Value "%s" can not contain the special HTML character `%s`.', |
|
83
|
1 |
|
$value, |
|
84
|
2 |
|
$char |
|
85
|
|
|
) |
|
86
|
|
|
); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
2 |
|
return true; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Shorten string to the specified length. |
|
95
|
|
|
* |
|
96
|
|
|
* @param string $string String. |
|
97
|
|
|
* @param int $length Length. |
|
98
|
|
|
* |
|
99
|
|
|
* @return string |
|
100
|
|
|
*/ |
|
101
|
5 |
|
public static function shorten( $string, $length ) { |
|
102
|
5 |
|
return mb_strimwidth( $string, 0, $length, '…', 'UTF-8' ); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Get HTML special chars. |
|
107
|
|
|
* |
|
108
|
|
|
* @return array |
|
109
|
|
|
*/ |
|
110
|
6 |
|
private static function get_html_special_chars() { |
|
111
|
|
|
return array( |
|
112
|
|
|
// Ampersand. |
|
113
|
6 |
|
'&', // Ampersand (https://unicode-table.com/en/0026/). |
|
114
|
|
|
'&', // Fullwidth Ampersand (https://unicode-table.com/en/FF06/). |
|
115
|
|
|
// Less-Than. |
|
116
|
|
|
'<', // Less-Than Sign (https://unicode-table.com/en/003C/). |
|
117
|
|
|
'﹤', // Small Less-Than Sign (https://unicode-table.com/en/FE64/). |
|
118
|
|
|
'<', // Fullwidth Less-Than Sign (https://unicode-table.com/en/FF1C/). |
|
119
|
|
|
// Greater-Than. |
|
120
|
|
|
'>', // Greater-Than Sign (https://unicode-table.com/en/003E/). |
|
121
|
|
|
'﹥', // Small Greater-Than Sign (https://unicode-table.com/en/FE65/). |
|
122
|
|
|
'>', // Fullwidth Greater-Than Sign (https://unicode-table.com/en/FF1E/). |
|
123
|
|
|
); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Replace HTML special chars with fullwidth Unicode characters. |
|
128
|
|
|
* |
|
129
|
|
|
* @param string $string String. |
|
130
|
|
|
* |
|
131
|
|
|
* @return string |
|
132
|
|
|
*/ |
|
133
|
4 |
|
public static function replace_html_special_chars( $string ) { |
|
134
|
4 |
|
$replacement = '�'; // Replacement Character » https://unicode-table.com/en/FFFD/. |
|
135
|
|
|
|
|
136
|
4 |
|
return str_replace( self::get_html_special_chars(), $replacement, $string ); |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|