Completed
Push — develop ( 1bde17...13bd87 )
by Paul
01:57
created

Utility::cleanString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace GeminiLabs\Castor\Helpers;
4
5
class Utility
6
{
7
	/**
8
	 * @return string
9
	 */
10
	public function buildAttributes( array $atts = [] )
11
	{
12
		$attributes = [];
13
		foreach( $atts as $key => $value ) {
14
			$attributes[] = sprintf( '%s="%s"', $key, $value );
15
		}
16
		return implode( ' ', $attributes );
17
	}
18
19
	/**
20
	 * @return string
21
	 */
22
	public function buildAttributesFor( $tag, array $atts = [] )
23
	{
24
		return $this->buildAttributes(
25
			wp_parse_args( $atts, apply_filters( "castor/render/$tag/attributes", [] ))
26
		);
27
	}
28
29
	/**
30
	 * @param string $name
31
	 * @param string $path
32
	 *
33
	 * @return string
34
	 */
35
	public function buildClassName( $name, $path = '' )
36
	{
37
		$className = array_map( 'ucfirst', array_map( 'strtolower', preg_split( '/[-_]/', $name )));
38
		$className = implode( '', $className );
39
40
		return !empty( $path )
41
			? str_replace( '\\\\', '\\', sprintf( '%s\%s', $path, $className ))
42
			: $className;
43
	}
44
45
	/**
46
	 * @param string $name
47
	 * @param string $prefix
48
	 *
49
	 * @return string
50
	 */
51
	public function buildMethodName( $name, $prefix = 'get' )
52
	{
53
		return lcfirst( $this->buildClassName( $prefix . '-' . $name ));
54
	}
55
56
	/**
57
	 * @param string $suffix
58
	 * @param string $string
59
	 * @param bool   $unique
60
	 *
61
	 * @return string
62
	 */
63
	public function endWith( $suffix, $string, $unique = true )
64
	{
65
		return $unique && $this->endsWith( $suffix, $string )
66
			? $string
67
			: $string . $suffix;
68
	}
69
70
	/**
71
	 * @param string $needle
72
	 * @param string $haystack
73
	 *
74
	 * @return string
75
	 */
76
	public function endsWith( $needle, $haystack )
77
	{
78
		$length = strlen( $needle );
79
		return $length != 0
80
			? substr( $haystack, -$length ) === $needle
81
			: true;
82
	}
83
84
	/**
85
	 * @param string $tag
86
	 * @param string $value
87
	 *
88
	 * @return void
89
	 */
90
	public function printTag( $tag, $value, array $attributes = [] )
91
	{
92
		$attributes = $this->buildAttributesFor( $tag, $attributes );
93
94
		printf( '<%s>%s</%s>',
95
			rtrim( sprintf( '%s %s', $tag, $attributes )),
96
			$value,
97
			$tag
98
		);
99
	}
100
101
	/**
102
	 * @param string $prefix
103
	 * @param string $string
104
	 * @param bool   $unique
105
	 *
106
	 * @return string
107
	 */
108
	public function startWith( $prefix, $string, $unique = true )
109
	{
110
		return $unique && $this->startsWith( $prefix, $string )
111
			? $string
112
			: $prefix . $string;
113
	}
114
115
	/**
116
	 * @param string $needle
117
	 * @param string $haystack
118
	 *
119
	 * @return string
120
	 */
121
	public function startsWith( $needle, $haystack )
122
	{
123
		return substr( $haystack, 0, strlen( $needle )) === $needle;
124
	}
125
126
	/**
127
	 * @param string $string
128
	 * @param string $needle
129
	 * @param bool   $caseSensitive
130
	 *
131
	 * @return string
132
	 */
133
	public function trimLeft( $string, $needle, $caseSensitive = true )
134
	{
135
		$strPos = $caseSensitive ? "strpos" : "stripos";
136
		if( $strPos( $string, $needle ) === 0 ) {
137
			$string = substr( $string, strlen( $needle ));
138
		}
139
		return $string;
140
	}
141
142
	/**
143
	 * @param string $string
144
	 * @param string $needle
145
	 * @param bool   $caseSensitive
146
	 *
147
	 * @return string
148
	 */
149
	public function trimRight( $string, $needle, $caseSensitive = true )
150
	{
151
		$strPos = $caseSensitive ? "strpos" : "stripos";
152
		if( $strPos( $string, $needle, strlen( $string ) - strlen( $needle )) !== false ) {
153
			$string = substr( $string, 0, -strlen( $needle ));
154
		}
155
		return $string;
156
	}
157
}
158