|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace GeminiLabs\Pollux; |
|
4
|
|
|
|
|
5
|
|
|
class Helper |
|
6
|
|
|
{ |
|
7
|
|
|
/** |
|
8
|
|
|
* @param string $name |
|
9
|
|
|
* @param string $path |
|
10
|
|
|
* @return string |
|
11
|
|
|
*/ |
|
12
|
|
|
public static function buildClassName( $name, $path = '' ) |
|
13
|
|
|
{ |
|
14
|
|
|
$className = array_map( 'ucfirst', array_map( 'strtolower', preg_split( '/[-_]/', $name ))); |
|
|
|
|
|
|
15
|
|
|
$className = implode( '', $className ); |
|
16
|
|
|
return !empty( $path ) |
|
17
|
|
|
? str_replace( '\\\\', '\\', sprintf( '%s\%s', $path, $className )) |
|
18
|
|
|
: $className; |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @param string $name |
|
23
|
|
|
* @param string $prefix |
|
24
|
|
|
* @return string |
|
25
|
|
|
*/ |
|
26
|
|
|
public static function buildMethodName( $name, $prefix = 'get' ) |
|
27
|
|
|
{ |
|
28
|
|
|
return lcfirst( static::buildClassName( $prefix . '-' . $name )); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param string $needle |
|
33
|
|
|
* @param string $haystack |
|
34
|
|
|
* @return bool |
|
35
|
|
|
*/ |
|
36
|
|
|
public static function endsWith( $needle, $haystack ) |
|
37
|
|
|
{ |
|
38
|
|
|
$length = strlen( $needle ); |
|
39
|
|
|
return $length != 0 |
|
40
|
|
|
? substr( $haystack, -$length ) === $needle |
|
41
|
|
|
: true; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param mixed $fromClass |
|
46
|
|
|
* @return string |
|
47
|
|
|
*/ |
|
48
|
|
|
public static function getClassname( $fromClass ) |
|
49
|
|
|
{ |
|
50
|
|
|
$className = is_string( $fromClass ) |
|
51
|
|
|
? $fromClass |
|
52
|
|
|
: get_class( $fromClass ); |
|
53
|
|
|
$paths = explode( '\\', $className ); |
|
54
|
|
|
return end( $paths ); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* get_current_screen() is unreliable because it is not defined on all admin pages. |
|
59
|
|
|
* @return \WP_Screen|\stdClass |
|
60
|
|
|
*/ |
|
61
|
|
|
public static function getCurrentScreen() |
|
62
|
|
|
{ |
|
63
|
|
|
global $hook_suffix, $pagenow; |
|
64
|
|
|
if( function_exists( 'get_current_screen' )) { |
|
65
|
|
|
$screen = get_current_screen(); |
|
|
|
|
|
|
66
|
|
|
} |
|
67
|
|
|
if( empty( $screen )) { |
|
68
|
|
|
$screen = new \stdClass(); |
|
69
|
|
|
$screen->base = $screen->id = $hook_suffix; |
|
70
|
|
|
} |
|
71
|
|
|
$screen->pagenow = $pagenow; |
|
72
|
|
|
return $screen; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param string $needle |
|
77
|
|
|
* @param string $haystack |
|
78
|
|
|
* @return bool |
|
79
|
|
|
*/ |
|
80
|
|
|
public static function startsWith( $needle, $haystack ) |
|
81
|
|
|
{ |
|
82
|
|
|
return substr( $haystack, 0, strlen( $needle )) === $needle; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @param mixed $value |
|
87
|
|
|
* @return array |
|
88
|
|
|
*/ |
|
89
|
|
|
public static function toArray( $value ) |
|
90
|
|
|
{ |
|
91
|
|
|
return array_filter( (array) $value ); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|