Failed Conditions
Push — develop ( 8b4b14...bae442 )
by Remco
04:18
created

DataHelper::validate_null_or_an()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 2
b 0
f 0
nc 2
nop 2
dl 0
loc 6
ccs 3
cts 4
cp 0.75
crap 2.0625
rs 10
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
/**
14
 * Data helper
15
 *
16
 * @link    https://github.com/wp-pay-gateways/ideal-basic/blob/2.0.0/src/DataHelper.php
17
 * @author  Remco Tolsma
18
 * @version 2.1.8
19
 * @since   2.0.2
20
 */
21
class DataHelper {
22
	/**
23
	 * Strictly alphanumerical (letters and numbers only).
24
	 *
25
	 * The OmniKassa 2.0 document is not very clear about spaces, these are not allowd in AN (Strictly).
26
	 * If a space is used in a AN (Strictly) field this will result in for examploe the following error:
27
	 * `merchantOrderId should only contain alphanumeric characters`.
28
	 *
29
	 * @var array
30
	 */
31
	private static $characters_ans = array( 'A-Z', 'a-z', '0-9' );
32
33
	/**
34
	 * Validate AN..$max.
35
	 *
36
	 * @param string $value Value to validate.
37
	 * @param int    $max   Max length of value.
38
	 * @return true
39
	 * @throws \InvalidArgumentException Throws invalid argument exception when string is longer then max length.
40
	 */
41 11
	public static function validate_an( $value, $max ) {
42 11
		if ( \mb_strlen( $value, 'UTF-8' ) > $max ) {
43 1
			throw new \InvalidArgumentException(
44 1
				\sprintf(
45 1
					'Value "%s" can not be longer then `%d`.',
46 1
					$value,
47 1
					$max
48
				)
49
			);
50
		}
51
52
		/**
53
		 * HTML tags are not allowed.
54
		 *
55
		 * @link https://stackoverflow.com/questions/5732758/detect-html-tags-in-a-string
56
		 */
57
58
		// phpcs:ignore WordPress.WP.AlternativeFunctions.strip_tags_strip_tags -- We don't want the `trim` in `wp_strip_all_tags`.
59 10
		if ( \strip_tags( $value ) !== $value ) {
60 2
			throw new \InvalidArgumentException(
61 2
				\sprintf(
62 2
					'HTML tags are not allowed: `%s`.',
63 2
					$value
64
				)
65
			);
66
		}
67
68 8
		return true;
69
	}
70
71
	/**
72
	 * Validate AN(Strictly)..Max nn.
73
	 *
74
	 * @param string $value Value to validate.
75
	 * @param int    $max   Max length of value.
76
	 * @return true
77
	 * @throws \InvalidArgumentException Throws invalid argument exception when string is not alphanumeric characters.
78
	 */
79 5
	public static function validate_ans( $value, $max ) {
80 5
		$pattern = '#[^' . \implode( self::$characters_ans ) . ']#';
81
82 5
		$result = \preg_match( $pattern, $value );
83
84 5
		if ( false === $result ) {
85
			throw new \Exception(
86
				'PCRE regex execution error.',
87
				\preg_last_error()
88
			);
89
		}
90
91 5
		if ( 1 === $result ) {
92 1
			throw new \InvalidArgumentException(
93 1
				\sprintf(
94 1
					'Only value that consists strictly of alphanumeric characters are allowed: `%s`.',
95 1
					$value
96
				)
97
			);
98
		}
99
100 4
		return self::validate_an( $value, $max );
101
	}
102
103
	/**
104
	 * Validate null or AN..$max.
105
	 *
106
	 * @param string|null $value Value to validate.
107
	 * @param int         $max   Max length of value.
108
	 * @return true
109
	 * @throws \InvalidArgumentException Throws invalid argument exception when value is not null and longer then max length.
110
	 */
111 1
	public static function validate_null_or_an( $value, $max ) {
112 1
		if ( null === $value ) {
113
			return true;
114
		}
115
116 1
		return self::validate_an( $value, $max );
117
	}
118
119
	/**
120
	 * Sanitize string to the specified length.
121
	 *
122
	 * @param string $string String.
123
	 * @param int    $length Length.
124
	 * @return string
125
	 */
126 8
	public static function sanitize_an( $string, $length ) {
127
		/**
128
		 * HTML tags are not allowed.
129
		 *
130
		 * @link https://stackoverflow.com/questions/5732758/detect-html-tags-in-a-string
131
		 */
132
133
		// phpcs:ignore WordPress.WP.AlternativeFunctions.strip_tags_strip_tags -- We don't want the `trim` in `wp_strip_all_tags`.
134 8
		$sanitized = \strip_tags( $string );
135
136
		/**
137
		 * In version `2.1.6` of this library we used the `mb_strimwidth`
138
		 * function, unfortunately this function is not alwys available.
139
		 * Therefor we now use the `mb_substr`, WordPress is shipped
140
		 * with a compat function.
141
		 *
142
		 * @link https://github.com/WordPress/WordPress/blob/5.0/wp-includes/compat.php#L44-L217
143
		 */
144 8
		$sanitized = \mb_substr( $sanitized, 0, $length );
145
146 8
		return $sanitized;
147
	}
148
}
149