Completed
Push — develop ( f6ac94...a82602 )
by Paul
01:59
created

Utility::buildAttributes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
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 $string
58
	 *
59
	 * @return string
60
	 */
61
	public function cleanString( $string )
62
	{
63
		return trim( strip_tags( $string ));
64
	}
65
66
	/**
67
	 * @param string $suffix
68
	 * @param string $string
69
	 * @param bool   $unique
70
	 *
71
	 * @return string
72
	 */
73
	public function endWith( $suffix, $string, $unique = true )
74
	{
75
		return $unique && $this->endsWith( $suffix, $string )
76
			? $string
77
			: $string . $suffix;
78
	}
79
80
	/**
81
	 * @param string $needle
82
	 * @param string $haystack
83
	 *
84
	 * @return string
85
	 */
86
	public function endsWith( $needle, $haystack )
87
	{
88
		$length = strlen( $needle );
89
		return $length != 0
90
			? substr( $haystack, -$length ) === $needle
91
			: true;
92
	}
93
94
	/**
95
	 * @param string $tag
96
	 * @param string $content
0 ignored issues
show
Bug introduced by
There is no parameter named $content. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
97
	 *
98
	 * @return void
99
	 */
100
	public function printTag( $tag, $value, array $attributes = [] )
101
	{
102
		$attributes = $this->buildAttributesFor( $tag, $attributes );
103
104
		printf( '<%s>%s</%s>',
105
			rtrim( sprintf( '%s %s', $tag, $attributes )),
106
			$value,
107
			$tag
108
		);
109
	}
110
111
	/**
112
	 * @param string $prefix
113
	 * @param string $string
114
	 * @param bool   $unique
115
	 *
116
	 * @return string
117
	 */
118
	public function startWith( $prefix, $string, $unique = true )
119
	{
120
		return $unique && $this->startsWith( $prefix, $string )
121
			? $string
122
			: $prefix . $string;
123
	}
124
125
	/**
126
	 * @param string $needle
127
	 * @param string $haystack
128
	 *
129
	 * @return string
130
	 */
131
	public function startsWith( $needle, $haystack )
132
	{
133
		return substr( $haystack, 0, strlen( $needle )) === $needle;
134
	}
135
136
	/**
137
	 * @param string $string
138
	 * @param string $needle
139
	 * @param bool   $caseSensitive
140
	 *
141
	 * @return string
142
	 */
143
	public function trimLeft( $string, $needle, $caseSensitive = true )
144
	{
145
		$strPos = $caseSensitive ? "strpos" : "stripos";
146
		if( $strPos( $string, $needle ) === 0 ) {
147
			$string = substr( $string, strlen( $needle ));
148
		}
149
		return $string;
150
	}
151
152
	/**
153
	 * @param string $string
154
	 * @param string $needle
155
	 * @param bool   $caseSensitive
156
	 *
157
	 * @return string
158
	 */
159
	public function trimRight( $string, $needle, $caseSensitive = true )
160
	{
161
		$strPos = $caseSensitive ? "strpos" : "stripos";
162
		if( $strPos( $string, $needle, strlen( $string ) - strlen( $needle )) !== false ) {
163
			$string = substr( $string, 0, -strlen( $needle ));
164
		}
165
		return $string;
166
	}
167
}
168