|
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', (array) 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 $needle |
|
58
|
|
|
* @param string $haystack |
|
59
|
|
|
* |
|
60
|
|
|
* @return bool |
|
61
|
|
|
*/ |
|
62
|
|
|
public function contains($needle, $haystack) |
|
63
|
|
|
{ |
|
64
|
|
|
false !== strpos($haystack, $needle); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param string $suffix |
|
69
|
|
|
* @param string $string |
|
70
|
|
|
* @param bool $unique |
|
71
|
|
|
* |
|
72
|
|
|
* @return string |
|
73
|
|
|
*/ |
|
74
|
|
|
public function endWith($suffix, $string, $unique = true) |
|
75
|
|
|
{ |
|
76
|
|
|
return $unique && $this->endsWith($suffix, $string) |
|
77
|
|
|
? $string |
|
78
|
|
|
: $string.$suffix; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param string $needle |
|
83
|
|
|
* @param string $haystack |
|
84
|
|
|
* |
|
85
|
|
|
* @return bool |
|
86
|
|
|
*/ |
|
87
|
|
|
public function endsWith($needle, $haystack) |
|
88
|
|
|
{ |
|
89
|
|
|
$length = strlen($needle); |
|
90
|
|
|
return 0 != $length |
|
91
|
|
|
? substr($haystack, -$length) === $needle |
|
92
|
|
|
: true; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @param string $tag |
|
97
|
|
|
* @param string $value |
|
98
|
|
|
* |
|
99
|
|
|
* @return void |
|
100
|
|
|
*/ |
|
101
|
|
|
public function printTag($tag, $value, array $attributes = []) |
|
102
|
|
|
{ |
|
103
|
|
|
$attributes = $this->buildAttributesFor($tag, $attributes); |
|
104
|
|
|
|
|
105
|
|
|
printf('<%s>%s</%s>', |
|
106
|
|
|
rtrim(sprintf('%s %s', $tag, $attributes)), |
|
107
|
|
|
$value, |
|
108
|
|
|
$tag |
|
109
|
|
|
); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @param string $prefix |
|
114
|
|
|
* @param string $string |
|
115
|
|
|
* @param bool $unique |
|
116
|
|
|
* |
|
117
|
|
|
* @return string |
|
118
|
|
|
*/ |
|
119
|
|
|
public function startWith($prefix, $string, $unique = true) |
|
120
|
|
|
{ |
|
121
|
|
|
return $unique && $this->startsWith($prefix, $string) |
|
122
|
|
|
? $string |
|
123
|
|
|
: $prefix.$string; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @param string $needle |
|
128
|
|
|
* @param string $haystack |
|
129
|
|
|
* |
|
130
|
|
|
* @return bool |
|
131
|
|
|
*/ |
|
132
|
|
|
public function startsWith($needle, $haystack) |
|
133
|
|
|
{ |
|
134
|
|
|
return substr($haystack, 0, strlen($needle)) === $needle; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* @param mixed $value |
|
139
|
|
|
* |
|
140
|
|
|
* @return array |
|
141
|
|
|
*/ |
|
142
|
|
|
public function toArray($value) |
|
143
|
|
|
{ |
|
144
|
|
|
if (is_string($value)) { |
|
145
|
|
|
$value = trim($value); |
|
146
|
|
|
} |
|
147
|
|
|
return array_filter((array) $value); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* @param string $string |
|
152
|
|
|
* @param string $needle |
|
153
|
|
|
* @param bool $caseSensitive |
|
154
|
|
|
* |
|
155
|
|
|
* @return string |
|
156
|
|
|
*/ |
|
157
|
|
|
public function trimLeft($string, $needle, $caseSensitive = true) |
|
158
|
|
|
{ |
|
159
|
|
|
$strPos = $caseSensitive ? 'strpos' : 'stripos'; |
|
160
|
|
|
if (0 === $strPos($string, $needle)) { |
|
161
|
|
|
$string = substr($string, strlen($needle)); |
|
162
|
|
|
} |
|
163
|
|
|
return $string; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* @param string $string |
|
168
|
|
|
* @param string $needle |
|
169
|
|
|
* @param bool $caseSensitive |
|
170
|
|
|
* |
|
171
|
|
|
* @return string |
|
172
|
|
|
*/ |
|
173
|
|
|
public function trimRight($string, $needle, $caseSensitive = true) |
|
174
|
|
|
{ |
|
175
|
|
|
$strPos = $caseSensitive ? 'strpos' : 'stripos'; |
|
176
|
|
|
if (false !== $strPos($string, $needle, strlen($string) - strlen($needle))) { |
|
177
|
|
|
$string = substr($string, 0, -strlen($needle)); |
|
178
|
|
|
} |
|
179
|
|
|
return $string; |
|
180
|
|
|
} |
|
181
|
|
|
} |
|
182
|
|
|
|