|
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 mixed $value |
|
128
|
|
|
* |
|
129
|
|
|
* @return array |
|
130
|
|
|
*/ |
|
131
|
|
|
public function toArray( $value ) |
|
132
|
|
|
{ |
|
133
|
|
|
if( is_string( $value )) { |
|
134
|
|
|
$value = trim( $value ); |
|
135
|
|
|
} |
|
136
|
|
|
return array_filter((array) $value ); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @param string $string |
|
141
|
|
|
* @param string $needle |
|
142
|
|
|
* @param bool $caseSensitive |
|
143
|
|
|
* |
|
144
|
|
|
* @return string |
|
145
|
|
|
*/ |
|
146
|
|
|
public function trimLeft( $string, $needle, $caseSensitive = true ) |
|
147
|
|
|
{ |
|
148
|
|
|
$strPos = $caseSensitive ? "strpos" : "stripos"; |
|
149
|
|
|
if( $strPos( $string, $needle ) === 0 ) { |
|
150
|
|
|
$string = substr( $string, strlen( $needle )); |
|
151
|
|
|
} |
|
152
|
|
|
return $string; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @param string $string |
|
157
|
|
|
* @param string $needle |
|
158
|
|
|
* @param bool $caseSensitive |
|
159
|
|
|
* |
|
160
|
|
|
* @return string |
|
161
|
|
|
*/ |
|
162
|
|
|
public function trimRight( $string, $needle, $caseSensitive = true ) |
|
163
|
|
|
{ |
|
164
|
|
|
$strPos = $caseSensitive ? "strpos" : "stripos"; |
|
165
|
|
|
if( $strPos( $string, $needle, strlen( $string ) - strlen( $needle )) !== false ) { |
|
166
|
|
|
$string = substr( $string, 0, -strlen( $needle )); |
|
167
|
|
|
} |
|
168
|
|
|
return $string; |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
|