Passed
Push — develop ( e961ab...6efce9 )
by Reüel
04:57
created

Util::boolean_to_string()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 1
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Util
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2018 Pronamic
7
 * @license   GPL-3.0-or-later
8
 * @package   Pronamic\WordPress\Pay
9
 */
10
11
namespace Pronamic\WordPress\Pay;
12
13
use SimpleXMLElement;
14
use WP_Error;
15
16
/**
17
 * Title: WordPress utility class
18
 *
19
 * @author Remco Tolsma
20
 * @version 1.0
21
 */
22
class Util {
23
	/**
24
	 * Remote get body.
25
	 *
26
	 * @see https://developer.wordpress.org/reference/functions/wp_remote_request/
27
	 *
28
	 * @param string $url                    The URL to use for the remote request.
29
	 * @param int    $required_response_code The required response code.
30
	 * @param array  $args                   The WordPress HTTP API request arguments.
31
	 *
32
	 * @return string|WP_Error
33
	 */
34
	public static function remote_get_body( $url, $required_response_code = 200, array $args = array() ) {
35
		$result = wp_remote_request( $url, $args );
36
37
		if ( ! is_array( $result ) ) {
38
			return $result;
39
		}
40
41
		$response_code = wp_remote_retrieve_response_code( $result );
42
43
		if ( $response_code === $required_response_code ) {
44
			return wp_remote_retrieve_body( $result );
45
		}
46
47
		return new WP_Error(
48
			'wrong_response_code',
49
			sprintf(
50
				__( 'The response code (<code>%1$s<code>) was incorrect, required response code <code>%2$s</code>.', 'pronamic_ideal' ),
51
				$response_code,
52
				$required_response_code
53
			)
54
		);
55
	}
56
57
	/**
58
	 * SimpleXML load string.
59
	 *
60
	 * @param string $string The XML string to convert to a SimpleXMLElement object.
61
	 *
62
	 * @return SimpleXMLElement|WP_Error
63
	 */
64
	public static function simplexml_load_string( $string ) {
65
		$result = false;
66
67
		// Suppress all XML errors.
68
		$use_errors = libxml_use_internal_errors( true );
69
70
		// Load.
71
		$xml = simplexml_load_string( $string );
72
73
		if ( false !== $xml ) {
74
			$result = $xml;
75
		} else {
76
			$error = new WP_Error( 'simplexml_load_error', __( 'Could not load the XML string.', 'pronamic_ideal' ) );
77
78
			foreach ( libxml_get_errors() as $e ) {
79
				$error->add( 'libxml_error', $e->message, $e );
80
			}
81
82
			libxml_clear_errors();
83
84
			$result = $error;
85
		}
86
87
		// Set back to previous value.
88
		libxml_use_internal_errors( $use_errors );
89
90
		return $result;
91
	}
92
93
	/**
94
	 * Amount to cents.
95
	 *
96
	 * @param float $price The amount to convert to cents.
97
	 *
98
	 * @return int
99
	 */
100
	public static function amount_to_cents( $price ) {
101
		return round( $price * 100 );
102
	}
103
104
	/**
105
	 * Cents to amount.
106
	 *
107
	 * @param int $cents The numberof cents to convert to an amount.
108
	 *
109
	 * @return float
110
	 */
111
	public static function cents_to_amount( $cents ) {
112
		return $cents / 100;
113
	}
114
115
	/**
116
	 * Convert boolean to an numceric boolean.
117
	 *
118
	 * @see https://github.com/eet-nu/buckaroo-ideal/blob/master/lib/buckaroo-ideal/request.rb#L136
119
	 *
120
	 * @param boolean $boolean The boolean to convert to 1 or 0.
121
	 *
122
	 * @return int
123
	 */
124
	public static function boolean_to_numeric( $boolean ) {
125
		return $boolean ? 1 : 0;
126
	}
127
128
	/**
129
	 * Convert boolean to an string boolean.
130
	 *
131
	 * @see https://github.com/eet-nu/buckaroo-ideal/blob/master/lib/buckaroo-ideal/request.rb#L136
132
	 *
133
	 * @param boolean $boolean The boolean to convert to the string 'true' or 'false'.
134
	 *
135
	 * @return int
136
	 */
137
	public static function boolean_to_string( $boolean ) {
138
		return $boolean ? 'true' : 'false';
139
	}
140
141
	/**
142
	 * Format price.
143
	 *
144
	 * @see https://github.com/woothemes/woocommerce/blob/v2.2.3/includes/wc-formatting-functions.php#L306-L347
145
	 * @see https://github.com/woothemes/woocommerce/blob/v2.2.3/includes/wc-core-functions.php#L299-L376
146
	 *
147
	 * @param float  $amount   The amount to format.
148
	 * @param string $currency The currency code for the currency symbol.
149
	 *
150
	 * @return string
151
	 */
152
	public static function format_price( $amount, $currency = null ) {
153
		$float = filter_var( $amount, FILTER_VALIDATE_FLOAT );
154
155
		if ( false === $float ) {
156
			return;
157
		}
158
159
		$currency = ( null === $currency ) ? 'EUR' : $currency;
160
161
		$currency_symbol = $currency;
162
163
		// Currencies.
164
		switch ( $currency ) {
165
			case 'EUR':
166
				$currency_symbol  = '€';
167
				$formatted_amount = number_format( $float, 2, ',', '.' );
168
169
				break;
170
			case 'USD':
171
				$currency_symbol  = '$';
172
				$formatted_amount = number_format( $float, 2, '.', ',' );
173
174
				break;
175
			case 'NLG':
176
				$currency_symbol  = 'G';
177
				$formatted_amount = number_format( $float, 4, '.', '' );
178
179
				break;
180
		}
181
182
		if ( ! isset( $formatted_amount ) ) {
183
			$formatted_amount = number_format_i18n( $float, 2 );
184
		}
185
186
		// @see https://en.wikipedia.org/wiki/Non-breaking_space#Keyboard_entry_methods
187
		$non_breaking_space = ' ';
188
189
		return '' . $currency_symbol . $non_breaking_space . $formatted_amount;
190
	}
191
192
	/**
193
	 * Format interval.
194
	 *
195
	 * @param int    $interval The interval number.
196
	 * @param string $period   The period indicator.
197
	 *
198
	 * @return string
199
	 */
200
	public static function format_interval( $interval, $period ) {
201
		switch ( $period ) {
202
			case 'D':
203
			case 'day':
204
			case 'days':
205
				return sprintf( _n( 'Every %s day', 'Every %s days', $interval, 'pronamic_ideal' ), $interval );
206
			case 'W':
207
			case 'week':
208
			case 'weeks':
209
				return sprintf( _n( 'Every %s week', 'Every %s weeks', $interval, 'pronamic_ideal' ), $interval );
210
			case 'M':
211
			case 'month':
212
			case 'months':
213
				return sprintf( _n( 'Every %s month', 'Every %s months', $interval, 'pronamic_ideal' ), $interval );
214
			case 'Y':
215
			case 'year':
216
			case 'years':
217
				return sprintf( _n( 'Every %s year', 'Every %s years', $interval, 'pronamic_ideal' ), $interval );
218
		}
219
	}
220
221
	/**
222
	 * Convert single interval period character to full name.
223
	 *
224
	 * @param string $interval_period string Short interval period (D, W, M or Y).
225
	 *
226
	 * @return string
227
	 */
228
	public static function to_interval_name( $interval_period ) {
229
		switch ( $interval_period ) {
230
			case 'D':
231
				return 'days';
232
			case 'W':
233
				return 'weeks';
234
			case 'M':
235
				return 'months';
236
			case 'Y':
237
				return 'years';
238
		}
239
240
		return $interval_period;
241
	}
242
243
	/**
244
	 * Format frequency.
245
	 *
246
	 * @param int $frequency The number of times.
247
	 *
248
	 * @return string
249
	 */
250
	public static function format_frequency( $frequency ) {
251
		if ( empty( $frequency ) ) {
252
			return _x( 'Unlimited', 'Recurring payment', 'pronamic_ideal' );
253
		}
254
255
		return sprintf( _n( '%s time', '%s times', $frequency, 'pronamic_ideal' ), $frequency );
256
	}
257
258
	/**
259
	 * Build URL with the specified parameters
260
	 *
261
	 * @param string $url        The URL to extend with specified parameters.
262
	 * @param array  $parameters The parameters to add to the specified URL.
263
	 *
264
	 * @return string
265
	 */
266
	public static function build_url( $url, array $parameters ) {
267
		return $url . '?' . _http_build_query( $parameters, null, '&' );
268
	}
269
270
	/**
271
	 * Get hidden inputs HTML for data.
272
	 *
273
	 * @param array $data Array with name and value pairs to convert to hidden HTML input eleemnts.
274
	 *
275
	 * @return string
276
	 */
277
	public static function html_hidden_fields( $data ) {
278
		$html = '';
279
280
		foreach ( $data as $name => $value ) {
281
			$html .= sprintf( '<input type="hidden" name="%s" value="%s" />', esc_attr( $name ), esc_attr( $value ) );
282
		}
283
284
		return $html;
285
	}
286
287
	/**
288
	 * Array to HTML attributes.
289
	 *
290
	 * @param array $attributes The key and value pairs to convert to HTML attributes.
291
	 *
292
	 * @return string
293
	 */
294
	public static function array_to_html_attributes( array $attributes ) {
295
		$html = '';
296
297
		foreach ( $attributes as $key => $value ) {
298
			$html .= sprintf( '%s="%s"', $key, esc_attr( $value ) );
299
		}
300
301
		$html = trim( $html );
302
303
		return $html;
304
	}
305
306
	/**
307
	 * Select options grouped.
308
	 *
309
	 * @param array  $groups         The grouped select options.
310
	 * @param string $selected_value The selected value.
311
	 *
312
	 * @return string
313
	 */
314
	public static function select_options_grouped( $groups, $selected_value = null ) {
315
		$html = '';
316
317
		if ( is_array( $groups ) ) {
0 ignored issues
show
introduced by
The condition is_array($groups) is always true.
Loading history...
318
			foreach ( $groups as $group ) {
319
				$optgroup = isset( $group['name'] ) && ! empty( $group['name'] );
320
321
				if ( $optgroup ) {
322
					$html .= '<optgroup label="' . $group['name'] . '">';
323
				}
324
325
				foreach ( $group['options'] as $value => $label ) {
326
					$html .= '<option value="' . $value . '" ' . selected( $selected_value, $value, false ) . '>' . $label . '</option>';
327
				}
328
329
				if ( $optgroup ) {
330
					$html .= '</optgroup>';
331
				}
332
			}
333
		}
334
335
		return $html;
336
	}
337
}
338