Passed
Push — master ( 4562dd...74d0bd )
by Paul
04:12
created

Helper::dataGet()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 3
dl 0
loc 10
ccs 7
cts 7
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace GeminiLabs\SiteReviews;
4
5
use GeminiLabs\SiteReviews\Database\Cache;
6
use GeminiLabs\Vectorface\Whip\Whip;
7
8
class Helper
9
{
10
	/**
11
	 * @param string $name
12
	 * @param string $path
13
	 * @return string
14
	 */
15 4
	public function buildClassName( $name, $path = '' )
16
	{
17 4
		$className = $this->camelCase( $name );
18 4
		$path = ltrim( str_replace( __NAMESPACE__, '', $path ), '\\' );
19 4
		return !empty( $path )
20 2
			? __NAMESPACE__.'\\'.$path.'\\'.$className
21 4
			: $className;
22
	}
23
24
	/**
25
	 * @param string $name
26
	 * @param string $prefix
27
	 * @return string
28
	 */
29 2
	public function buildMethodName( $name, $prefix = '' )
30
	{
31 2
		return lcfirst( $prefix.$this->buildClassName( $name ));
32
	}
33
34
	/**
35
	 * @param string $name
36
	 * @return string
37
	 */
38 1
	public function buildPropertyName( $name )
39
	{
40 1
		return lcfirst( $this->buildClassName( $name ));
41
	}
42
43
	/**
44
	 * @param string $string
45
	 * @return string
46
	 */
47 4
	public function camelCase( $string )
48
	{
49 4
		$string = ucwords( str_replace( ['-', '_'], ' ', trim( $string )));
50 4
		return str_replace( ' ', '', $string );
51
	}
52
53
	/**
54
	 * @return bool
55
	 */
56 1
	public function compareArrays( array $arr1, array $arr2 )
57
	{
58 1
		sort( $arr1 );
59 1
		sort( $arr2 );
60 1
		return $arr1 == $arr2;
61
	}
62
63
	/**
64
	 * @param mixed $array
65
	 * @return array
66
	 */
67 9
	public function consolidateArray( $array )
68
	{
69 9
		return is_array( $array )
70 9
			? $array
71 9
			: [];
72
	}
73
74
	/**
75
	 * @return array
76
	 */
77 8
	public function convertDotNotationArray( array $array )
78
	{
79 8
		$results = [];
80 8
		foreach( $array as $path => $value ) {
81 8
			$results = $this->dataSet( $results, $path, $value );
82
		}
83 8
		return $results;
84
	}
85
86
	/**
87
	 * @param string $name
88
	 * @return string
89
	 */
90 1
	public function convertPathToId( $path, $prefix = '' )
91
	{
92 1
		return str_replace( ['[', ']'], ['-', ''], $this->convertPathToName( $path, $prefix ));
93
	}
94
95
	/**
96
	 * @param string $path
97
	 * @return string
98
	 */
99 2
	public function convertPathToName( $path, $prefix = '' )
100
	{
101 2
		$levels = explode( '.', $path );
102 2
		return array_reduce( $levels, function( $result, $value ) {
103 2
			return $result.= '['.$value.']';
104 2
		}, $prefix );
105
	}
106
107
	/**
108
	 * @param string $string
109
	 * @param mixed $callback
110
	 * @return array
111
	 */
112 1
	public function convertStringToArray( $string, $callback = null )
113
	{
114 1
		$array = array_map( 'trim', explode( ',', $string ));
115 1
		return $callback
116
			? array_filter( $array, $callback )
117 1
			: array_filter( $array );
118
	}
119
120
	/**
121
	 * @param string $string
122
	 * @return string
123
	 */
124 8
	public function dashCase( $string )
125
	{
126 8
		return str_replace( '_', '-', $this->snakeCase( $string ));
127
	}
128
129
	/**
130
	 * Get a value from an array of values using a dot-notation path as reference
131
	 * @param array $data
132
	 * @param string $path
133
	 * @param mixed $fallback
134
	 * @return mixed
135
	 */
136 8
	public function dataGet( $data, $path = '', $fallback = '' )
137
	{
138 8
		$keys = explode( '.', $path );
139 8
		foreach( $keys as $key ) {
140 8
			if( !isset( $data[$key] )) {
141 8
				return $fallback;
142
			}
143 8
			$data = $data[$key];
144
		}
145 8
		return $data;
146
	}
147
148
	/**
149
	 * Set a value to an array of values using a dot-notation path as reference
150
	 * @param string $path
151
	 * @param mixed $value
152
	 * @return array
153
	 */
154 9
	public function dataSet( array $data, $path = '', $value )
155
	{
156 9
		$token = strtok( $path, '.' );
157 9
		$ref = &$data;
158 9
		while( $token !== false ) {
159 9
			$ref = $this->consolidateArray( $ref );
160 9
			$ref = &$ref[$token];
161 9
			$token = strtok( '.' );
162
		}
163 9
		$ref = $value;
164 9
		return $data;
165
	}
166
167
	/**
168
	 * @param string $needle
169
	 * @param string $haystack
170
	 * @return bool
171
	 */
172 1
	public function endsWith( $needle, $haystack )
173
	{
174 1
		$length = strlen( $needle );
175 1
		return $length != 0
176 1
			? substr( $haystack, -$length ) === $needle
177 1
			: true;
178
	}
179
180
	/**
181
	 * @param string $key
182
	 * @return mixed
183
	 */
184 2
	public function filterInput( $key, array $request = [] )
185
	{
186 2
		if( isset( $request[$key] )) {
187 1
			return $request[$key];
188
		}
189 2
		$variable = filter_input( INPUT_POST, $key );
190 2
		if( is_null( $variable ) && isset( $_POST[$key] )) {
191 2
			$variable = $_POST[$key];
192
		}
193 2
		return $variable;
194
	}
195
196
	/**
197
	 * @param string $key
198
	 * @return array
199
	 */
200 2
	public function filterInputArray( $key )
201
	{
202 2
		$variable = filter_input( INPUT_POST, $key, FILTER_DEFAULT, FILTER_REQUIRE_ARRAY );
203 2
		if( empty( $variable ) && !empty( $_POST[$key] ) && is_array( $_POST[$key] )) {
204 2
			$variable = $_POST[$key];
205
		}
206 2
		return (array)$variable;
207
	}
208
209
	/**
210
	 * @param bool $flattenValue
211
	 * @param string $prefix
212
	 * @return array
213
	 */
214 7
	public function flattenArray( array $array, $flattenValue = false, $prefix = '' )
215
	{
216 7
		$result = [];
217 7
		foreach( $array as $key => $value ) {
218 7
			$newKey = ltrim( $prefix.'.'.$key, '.' );
219 7
			if( $this->isIndexedFlatArray( $value )) {
220 7
				if( $flattenValue ) {
221 7
					$value = '['.implode( ', ', $value ).']';
222
				}
223
			}
224 7
			else if( is_array( $value )) {
225 7
				$result = array_merge( $result, $this->flattenArray( $value, $flattenValue, $newKey ));
226 7
				continue;
227
			}
228 7
			$result[$newKey] = $value;
229
		}
230 7
		return $result;
231
	}
232
233
	/**
234
	 * @return string
235
	 */
236 1
	public function getIpAddress()
237
	{
238 1
		$cloudflareIps = glsr( Cache::class )->getCloudflareIps();
239 1
		$ipv6 = defined( 'AF_INET6' )
240 1
			? $cloudflareIps['v6']
241 1
			: [];
242 1
		return (string)(new Whip( Whip::CLOUDFLARE_HEADERS | Whip::REMOTE_ADDR, [
243 1
			Whip::CLOUDFLARE_HEADERS => [
244 1
				Whip::IPV4 => $cloudflareIps['v4'],
245 1
				Whip::IPV6 => $ipv6,
246
			],
247 1
		]))->getValidIpAddress();
248
	}
249
250
	/**
251
	 * @param string $key
252
	 * @param string $position
253
	 * @return array
254
	 */
255
	public function insertInArray( array $array, array $insert, $key, $position = 'before' )
256
	{
257
		$keyPosition = intval( array_search( $key, array_keys( $array )));
258
		if( 'after' == $position ) {
259
			$keyPosition++;
260
		}
261
		if( false !== $keyPosition ) {
262
			$result = array_slice( $array, 0, $keyPosition );
263
			$result = array_merge( $result, $insert );
264
			return array_merge( $result, array_slice( $array, $keyPosition ));
265
		}
266
		return array_merge( $array, $insert );
267
	}
268
269
	/**
270
	 * @param mixed $array
271
	 * @return bool
272
	 */
273 8
	public function isIndexedFlatArray( $array )
274
	{
275 8
		if( !is_array( $array ) || array_filter( $array, 'is_array' )) {
276 8
			return false;
277
		}
278 8
		return wp_is_numeric_array( $array );
279
	}
280
281
	/**
282
	 * @param string $string
283
	 * @param string $prefix
284
	 * @return string
285
	 */
286 1
	public function prefixString( $string, $prefix = '' )
287
	{
288 1
		return $prefix.str_replace( $prefix, '', trim( $string ));
289
	}
290
291
	/**
292
	 * @return array
293
	 */
294 8
	public function removeEmptyArrayValues( array $array )
295
	{
296 8
		$result = [];
297 8
		foreach( $array as $key => $value ) {
298 8
			if( !$value )continue;
299 8
			$result[$key] = is_array( $value )
300 7
				? $this->removeEmptyArrayValues( $value )
301 8
				: $value;
302
		}
303 8
		return $result;
304
	}
305
306
	/**
307
	 * @param string $prefix
308
	 * @param string $text
309
	 * @return string
310
	 */
311 7
	public function removePrefix( $prefix, $text )
312
	{
313 7
		return 0 === strpos( $text, $prefix )
314 7
			? substr( $text, strlen( $prefix ))
315 7
			: $text;
316
	}
317
318
	/**
319
	 * @param string $string
320
	 * @return string
321
	 */
322 9
	public function snakeCase( $string )
323
	{
324 9
		if( !ctype_lower( $string )) {
325 9
			$string = preg_replace( '/\s+/u', '', $string );
326 9
			$string = preg_replace( '/(.)(?=[A-Z])/u', '$1_', $string );
327 9
			$string = function_exists( 'mb_strtolower' )
328 9
				? mb_strtolower( $string, 'UTF-8' )
329 9
				: strtolower( $string );
330
		}
331 9
		return str_replace( '-', '_', $string );
332
	}
333
334
	/**
335
	 * @param string $needle
336
	 * @param string $haystack
337
	 * @return bool
338
	 */
339 8
	public function startsWith( $needle, $haystack )
340
	{
341 8
		return substr( $haystack, 0, strlen( $needle )) === $needle;
342
	}
343
}
344