Failed Conditions
Push — develop ( ae1b1a...4e19da )
by Remco
04:16 queued 11s
created

DataHelper::validate_an_html_special_chars()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 2
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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 3
	public static function validate_an( $value, $max ) {
35 3
		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 3
		return true;
46
	}
47
48
	/**
49
	 * Validate AN..$max and take HTML special charachters in account.
50
	 *
51
	 * @param string $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 string is longer then max length.
57
	 */
58 1
	public static function validate_an_html_special_chars( $value, $max ) {
59 1
		return self::validate_an( $value, self::length_html_special_chars( $value, $max ) );
60
	}
61
62
	/**
63
	 * 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 1
	public static function validate_null_or_an( $value, $max ) {
73 1
		if ( null === $value ) {
74
			return true;
75
		}
76
77 1
		return self::validate_an( $value, $max );
78
	}
79
80
	/**
81
	 * Shorten string to the specified length.
82
	 *
83
	 * @param string $value  Value.
84
	 * @param int    $length Length.
85
	 *
86
	 * @return string
87
	 */
88
	public static function shorten( $string, $length ) {
89
		return mb_substr( $string, 0, $length, 'UTF-8' );
90
	}
91
92
	/**
93
	 * Shorten string to the specified length and take HTML special charachters in account.
94
	 *
95
	 * @param string $value  Value.
96
	 * @param int    $length Length.
97
	 *
98
	 * @return string
99
	 */
100
	public static function shorten_html_special_chars( $string, $length ) {
101
		return self::shorten( $string, self::length_html_special_chars( $string, $length ) );
102
	}
103
104
	/**
105
	 * Determine length of string and take HTML special charachters in account.
106
	 *
107
	 * @param string $value  Value.
108
	 * @param int    $length Length.
109
	 */
110 1
	private static function length_html_special_chars( $string, $length ) {
111 1
		$string_length = mb_strlen( $string, 'UTF-8' );
112 1
		$html_length   = mb_strlen( htmlspecialchars( $string, ENT_NOQUOTES ), 'UTF-8' );
113
114 1
		$length = $length - ( $html_length - $string_length );
115
116 1
		return $length;
117
	}
118
}
119