Passed
Push — master ( 78492a...4f268f )
by Paul
04:26
created

Helper::buildPropertyName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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