Passed
Push — develop ( 5e8f92...83d763 )
by Paul
03:00
created

Helper::getCurrentScreen()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
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|null
62
	 */
63
	public function getCurrentScreen()
64
	{
65
		global $current_screen;
66
		return isset( $current_screen ) ? $current_screen : (object) [
67
			'base' => '',
68
			'id' => '',
69
		];
70
	}
71
72
	/**
73
	 * @param string $needle
74
	 * @param string $haystack
75
	 * @return bool
76
	 */
77
	public function startsWith( $needle, $haystack )
78
	{
79
		return substr( $haystack, 0, strlen( $needle )) === $needle;
80
	}
81
82
	/**
83
	 * @param mixed $value
84
	 * @return array
85
	 */
86
	public function toArray( $value )
87
	{
88
		return array_filter( (array) $value );
89
	}
90
}
91