Passed
Push — master ( e448bf...51995c )
by Paul
07:24 queued 04:56
created

Helper::endsWith()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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