Passed
Push — feature/post-pay ( faf3dd...ce3905 )
by Remco
04:38
created

DataHelper::validate_an()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.9765

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 2
dl 0
loc 12
ccs 3
cts 8
cp 0.375
crap 2.9765
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Data helper
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2018 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.0.2
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
	 * @throws InvalidArgumentException Throws invalid argument exception when string is longer then max length.
30
	 */
31 2
	public static function validate_an( $value, $max ) {
32 2
		if ( strlen( $value ) > $max ) {
33
			throw new InvalidArgumentException(
34
				sprintf(
35
					'Value "%s" can not be longer then `%d`.',
36
					$value,
37
					$max
38
				)
39
			);
40
		}
41
42 2
		return true;
43
	}
44
45
	/**
46
	 * Validate null or AN..$max.
47
	 *
48
	 * @param string|null $value Value to validate.
49
	 * @param int         $max   Max length of value.
50
	 * @throws InvalidArgumentException Throws invalid argument exception when value is not null and longer then max length.
51
	 */
52 1
	public static function validate_null_or_an( $value, $max ) {
53 1
		if ( null === $value ) {
54
			return true;
55
		}
56
57 1
		return self::validate_an( $value, $max );
58
	}
59
}
60