Failed Conditions
Push — develop ( fc7b68...4eeca8 )
by Reüel
05:13
created

Util::string_equals()   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
namespace Pronamic\WordPress\Pay\Gateways\Buckaroo;
4
5
use Pronamic\WordPress\Pay\Payments\Payment;
6
7
/**
8
 * Title: Buckaroo utility class
9
 * Description:
10
 * Copyright: 2005-2020 Pronamic
11
 * Company: Pronamic
12
 *
13
 * @author Remco Tolsma
14
 * @version 2.0.0
15
 * @since 1.0.0
16
 */
17
class Util {
18
	/**
19
	 * Get invoice number.
20
	 *
21
	 * @param string $invoice_number
22
	 * @param Payment $payment
23
	 *
24
	 * @return string
25
	 */
26
	public static function get_invoice_number( $invoice_number, Payment $payment ) {
27
		// Replacements definition
28
		$replacements = array(
29
			'{order_id}'   => $payment->get_order_id(),
30
			'{payment_id}' => $payment->get_id(),
31
		);
32
33
		// Find and replace
34
		$invoice_number = str_replace(
35
			array_keys( $replacements ),
36
			array_values( $replacements ),
37
			$invoice_number,
38
			$count
39
		);
40
41
		// Make sure there is an dynamic part in the order ID
42
		if ( 0 === $count ) {
43
			$invoice_number .= $payment->get_id();
44
		}
45
46
		return $invoice_number;
47
	}
48
49
	/**
50
	 * Buckaroo check if the specified string is the specified key
51
	 *
52
	 * @param string $string
53
	 * @param string $value
54
	 *
55
	 * @return boolean true if match, false otherwise
56
	 */
57 8
	public static function string_equals( $string, $value ) {
58 8
		return 0 === strcasecmp( $string, $value );
59
	}
60
61
	/**
62
	 * Buckaroo check if the key starts with an prefix
63
	 *
64
	 * @param string $string
65
	 * @param string $prefix
66
	 *
67
	 * @return boolean true if match, false otherwise
68
	 */
69 5
	public static function string_starts_with( $string, $prefix ) {
70 5
		$string = substr( $string, 0, strlen( $prefix ) );
71
72 5
		return 0 === strcasecmp( $string, $prefix );
73
	}
74
75
	/**
76
	 * URL decode array
77
	 *
78
	 * @param array $data
79
	 *
80
	 * @return array
81
	 */
82 1
	public static function urldecode( array $data ) {
83 1
		return array_map( 'urldecode', $data );
84
	}
85
86
	/**
87
	 * Transform flat Buckaroo response into multidimensional array.
88
	 *
89
	 * @since 1.2.4
90
	 *
91
	 * @param array $response
92
	 *
93
	 * @return array
94
	 */
95 1
	public static function transform_flat_response( $response = array() ) {
96 1
		$return = array();
97
98 1
		if ( is_array( $response ) ) {
0 ignored issues
show
introduced by
The condition is_array($response) is always true.
Loading history...
99 1
			foreach ( $response as $flat_key => $value ) {
100 1
				unset( $response[ $flat_key ] );
101
102 1
				$is_brq = ( 'BRQ_' === substr( $flat_key, 0, 4 ) );
103
104
				// Remove 'BRQ_' from flat key (first part key will be prefixed with 'BRQ_')
105 1
				if ( $is_brq ) {
106 1
					$flat_key = substr_replace( $flat_key, '', 0, 4 );
107
				}
108
109 1
				$parts = explode( '_', $flat_key );
110
111
				// Prefix first key with BRQ_
112 1
				if ( $is_brq && count( $parts ) > 0 ) {
113 1
					$parts[0] = sprintf( 'BRQ_%s', $parts[0] );
114
				}
115
116 1
				$item =& $return;
117
118
				// Define key parts as array and set current item
119 1
				foreach ( $parts as $key ) {
120 1
					if ( ! isset( $item[ $key ] ) ) {
121 1
						$item[ $key ] = array();
122
					}
123
124 1
					$item =& $item[ $key ];
125
				}
126
127
				// Set value of item
128 1
				$item = $value;
129
			}
130
		}
131
132 1
		return $return;
133
	}
134
}
135