1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12: function xu_camel_case( $str ) {
13: if ( ! is_string( $str ) ) {
14: throw new InvalidArgumentException( 'Invalid argument. Must be string.' );
15: }
16:
17: return lcfirst( xu_studly_case( $str ) );
18: }
19:
20: 21: 22: 23: 24: 25: 26: 27: 28: 29:
30: function xu_contains( $haystack, $needles ) {
31: if ( ! is_string( $haystack ) ) {
32: throw new InvalidArgumentException( 'Invalid argument. `$haystack` must be string.' );
33: }
34:
35: if ( ! is_array( $needles ) && ! is_string( $needles ) ) {
36: throw new InvalidArgumentException( 'Invalid argument. `$needles` must be array or string.' );
37: }
38:
39: foreach ( (array) $needles as $needle ) {
40: if ( (string) $needle !== '' && strpos( $haystack, $needle ) !== false ) {
41: return true;
42: }
43: }
44:
45: return false;
46: }
47:
48: 49: 50: 51: 52: 53: 54:
55: function xu_convert_to_string( $obj ) {
56: if ( $obj === true ) {
57: return 'true';
58: }
59:
60: if ( $obj === false ) {
61: return 'false';
62: }
63:
64: if ( ! is_array( $obj ) && ( ( ! is_object( $obj ) && settype( $obj, 'string' ) !== false ) || ( is_object( $obj ) && method_exists( $obj, '__toString' ) ) ) ) {
65: return (string) $obj;
66: }
67:
68: return '';
69: }
70:
71: 72: 73: 74: 75: 76: 77: 78: 79:
80: function xu_dashify( $str ) {
81: if ( ! is_string( $str ) ) {
82: throw new InvalidArgumentException( 'Invalid argument. Must be string.' );
83: }
84:
85: return xu_strip_spaces( strtolower( preg_replace( '/(.)(?=[A-Z])|\_|\s/', '$1-', $str ) ) );
86: }
87:
88: 89: 90: 91: 92: 93: 94: 95: 96: 97:
98: function xu_ends_with( $haystack, $needles ) {
99: if ( ! is_string( $haystack ) ) {
100: throw new InvalidArgumentException( 'Invalid argument. `$haystack` must be string.' );
101: }
102:
103: if ( ! is_array( $needles ) && ! is_string( $needles ) ) {
104: throw new InvalidArgumentException( 'Invalid argument. `$needles` must be array or string.' );
105: }
106:
107: foreach ( (array) $needles as $needle ) {
108: if ( (string) $needle === substr( $haystack, -strlen( $needle ) ) ) {
109: return true;
110: }
111: }
112:
113: return false;
114: }
115:
116: 117: 118: 119: 120: 121: 122: 123: 124: 125:
126: function xu_starts_with( $haystack, $needles ) {
127: if ( ! is_string( $haystack ) ) {
128: throw new InvalidArgumentException( 'Invalid argument. `$haystack` must be string.' );
129: }
130:
131: if ( ! is_array( $needles ) && ! is_string( $needles ) ) {
132: throw new InvalidArgumentException( 'Invalid argument. `$needles` must be string.' );
133: }
134:
135: foreach ( (array) $needles as $needle ) {
136: if ( (string) $needle !== '' && strpos( $haystack, $needle ) === 0 ) {
137: return true;
138: }
139: }
140:
141: return false;
142: }
143:
144: 145: 146: 147: 148: 149: 150: 151: 152: 153:
154: function xu_snake_case( $str, $delimiter = '_' ) {
155: if ( ! is_string( $str ) ) {
156: throw new InvalidArgumentException( 'Invalid argument. `$str` must be string.' );
157: }
158:
159: if ( ! is_string( $delimiter ) ) {
160: throw new InvalidArgumentException( 'Invalid argument. `$delimiter` must be string.' );
161: }
162:
163: return xu_strip_spaces( strtolower( preg_replace( '/(.)(?=[A-Z])|\-|\s/', '$1' . $delimiter, $str ) ) );
164: }
165:
166:
167: 168: 169: 170: 171: 172: 173: 174: 175:
176: function xu_studly_case( $str ) {
177: if ( ! is_string( $str ) ) {
178: throw new InvalidArgumentException( 'Invalid argument. Must be string.' );
179: }
180:
181: return str_replace( ' ', '', ucwords( str_replace( ['-', '_'], ' ', $str ) ) );
182: }
183:
184: 185: 186: 187: 188: 189: 190: 191: 192:
193: function xu_strip_spaces( $str ) {
194: if ( ! is_string( $str ) ) {
195: throw new InvalidArgumentException( 'Invalid argument. Must be string.' );
196: }
197:
198: return trim( preg_replace( '/\s+/', ' ', $str ) );
199: }
200: