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 |
|
/** |
46
|
|
|
* HTML tags are not allowed. |
47
|
|
|
* |
48
|
|
|
* @link https://stackoverflow.com/questions/5732758/detect-html-tags-in-a-string |
49
|
|
|
*/ |
50
|
|
|
if ( wp_strip_all_tags( $value ) !== $value ) { |
51
|
|
|
throw new InvalidArgumentException( |
52
|
|
|
sprintf( |
53
|
|
|
'HTML tags are not allowed: `%s`.', |
54
|
|
|
$value |
55
|
|
|
) |
56
|
|
|
); |
57
|
|
|
} |
58
|
1 |
|
|
59
|
1 |
|
return true; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
1 |
|
* Validate null or AN..$max. |
64
|
|
|
* |
65
|
|
|
* @param string|null $value Value to validate. |
66
|
|
|
* @param int $max Max length of value. |
67
|
|
|
* |
68
|
|
|
* @return bool |
69
|
|
|
* |
70
|
|
|
* @throws InvalidArgumentException Throws invalid argument exception when value is not null and longer then max length. |
71
|
|
|
*/ |
72
|
|
|
public static function validate_null_or_an( $value, $max ) { |
73
|
|
|
if ( null === $value ) { |
74
|
5 |
|
return true; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return self::validate_an( $value, $max ); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Sanitize string to the specified length. |
82
|
|
|
* |
83
|
5 |
|
* @param string $string String. |
84
|
|
|
* @param int $length Length. |
85
|
|
|
* |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
|
|
public static function sanitize_an( $string, $length ) { |
89
|
|
|
/** |
90
|
|
|
* HTML tags are not allowed. |
91
|
|
|
* |
92
|
|
|
* @link https://stackoverflow.com/questions/5732758/detect-html-tags-in-a-string |
93
|
|
|
*/ |
94
|
|
|
$sanitized = wp_strip_all_tags( $string ); |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* In version `2.1.6` of this library we used the `mb_strimwidth` |
98
|
|
|
* function, unfortunately this function is not alwys available. |
99
|
|
|
* Therefor we now use the `mb_substr`, WordPress is shipped |
100
|
|
|
* with a compat function. |
101
|
|
|
* |
102
|
|
|
* @link https://github.com/WordPress/WordPress/blob/5.0/wp-includes/compat.php#L44-L217 |
103
|
|
|
*/ |
104
|
|
|
$sanitized = mb_substr( $sanitized, 0, $length ); |
105
|
|
|
|
106
|
|
|
return $sanitized; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|