Cancelled
Push — master ( 9c2a96...cfa019 )
by Paul
04:39
created

Helper::convertPathToId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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