Passed
Push — master ( ee0da0...af44f0 )
by Paul
04:33
created

Helper::isStringOrNumeric()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
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
	public function buildClassName( $name, $path = '' )
16
	{
17
		$className = array_map( 'strtolower', (array)preg_split( '/[-_]/', $name ));
18
		$className = array_map( 'ucfirst', $className );
19
		$className = implode( '', $className );
20
		$path = ltrim( str_replace( __NAMESPACE__, '', $path ), '\\' );
21
		return !empty( $path )
22
			? __NAMESPACE__.'\\'.$path.'\\'.$className
23
			: $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 $string
69
	 * @return string
70
	 */
71
	public function dashCase( $string )
72
	{
73
		return str_replace( '_', '-', $this->snakeCase( $string ));
74
	}
75
76
	/**
77
	 * @param string $needle
78
	 * @param string $haystack
79
	 * @return bool
80
	 */
81
	public function endsWith( $needle, $haystack )
82
	{
83
		$length = strlen( $needle );
84
		return $length != 0
85
			? substr( $haystack, -$length ) === $needle
86
			: true;
87
	}
88
89
	/**
90
	 * @param string $prefix
91
	 * @return array
92
	 */
93
	public function flattenArray( array $array, $prefix = '' )
94
	{
95
		$result = [];
96
		foreach( $array as $key => $value ) {
97
			$newKey = $prefix.( empty( $prefix ) ? '' : '.' ).$key;
98
			if( $this->isIndexedArray( $value )) {
99
				$value = '['.implode( ', ', $value ).']';
100
			}
101
			if( is_array( $value )) {
102
				$result = array_merge( $result, $this->flattenArray( $value, $newKey ));
103
				continue;
104
			}
105
			$result[$newKey] = $value;
106
		}
107
		return $result;
108
	}
109
110
	/**
111
	 * @return string
112
	 */
113
	public function getIpAddress()
114
	{
115
		$cloudflareIps = glsr( Cache::class )->getCloudflareIps();
116
		return (string)(new Whip( Whip::CLOUDFLARE_HEADERS | Whip::REMOTE_ADDR, [
117
			Whip::CLOUDFLARE_HEADERS => [
118
				Whip::IPV4 => $cloudflareIps['v4'],
119
				Whip::IPV6 => $cloudflareIps['v6'],
120
			],
121
		]))->getValidIpAddress();
122
	}
123
124
	/**
125
	 * Get a value from an array of values using a dot-notation path as reference
126
	 * @param string $path
127
	 * @param mixed $fallback
128
	 * @return void|mixed
129
	 */
130 7
	public function getPathValue( $path = '', $fallback, array $values )
131
	{
132 7
		$keys = explode( '.', $path );
133 7
		foreach( $keys as $key ) {
134 7
			if( !isset( $values[$key] )) {
135 1
				return $fallback;
136
			}
137 7
			$values = $values[$key];
138
		}
139 7
		return $values;
140
	}
141
142
	/**
143
	 * @param mixed $array
144
	 * @return bool
145
	 */
146
	public function isIndexedArray( $array )
147
	{
148
		if( !is_array( $array ) || array_filter( $array, 'is_array' )) {
149
			return false;
150
		}
151
		$current = 0;
152
		foreach( array_keys( $array ) as $key ) {
153
			if( $key !== $current ) {
154
				return false;
155
			}
156
			$current++;
157
		}
158
		return true;
159
	}
160
161
	/**
162
	 * @param mixed $value
163
	 * @return bool
164
	 */
165 7
	public function isStringOrNumeric( $value )
166
	{
167 7
		return is_string( $value ) || is_numeric( $value );
168
	}
169
170
	/**
171
	 * Remove empty values from an array
172
	 * @return array
173
	 */
174 7
	public function removeEmptyArrayValues( array $array )
175
	{
176 7
		$result = [];
177 7
		foreach( $array as $key => $value ) {
178 7
			if( !$value )continue;
179 7
			$result[$key] = is_array( $value )
180 1
				? $this->removeEmptyArrayValues( $value )
181 7
				: $value;
182
		}
183 7
		return $result;
184
	}
185
186
	/**
187
	 * Set a value to an array of values using a dot-notation path as reference
188
	 * @param string $path
189
	 * @param mixed $value
190
	 * @return array
191
	 */
192 7
	public function setPathValue( $path, $value, array $values )
193
	{
194 7
		$token = strtok( $path, '.' );
195 7
		$ref = &$values;
196 7
		while( $token !== false ) {
197 7
			$ref = is_array( $ref )
198 7
				? $ref
199 7
				: [];
200 7
			$ref = &$ref[$token];
201 7
			$token = strtok( '.' );
202
		}
203 7
		$ref = $value;
204 7
		return $values;
205
	}
206
207
	/**
208
	 * @param string $string
209
	 * @return string
210
	 */
211 7
	public function snakeCase( $string )
212
	{
213 7
		if( !ctype_lower( $string )) {
214 7
			$string = preg_replace( '/\s+/u', '', $string );
215 7
			$string = preg_replace( '/(.)(?=[A-Z])/u', '$1_', $string );
216 7
			$string = mb_strtolower( $string, 'UTF-8' );
217
		}
218 7
		return str_replace( '-', '_', $string );
219
	}
220
221
	/**
222
	 * @param string $needle
223
	 * @param string $haystack
224
	 * @return bool
225
	 */
226 7
	public function startsWith( $needle, $haystack )
227
	{
228 7
		return substr( $haystack, 0, strlen( $needle )) === $needle;
229
	}
230
}
231