/vendor/anahkiasen/underscore-php/src/Underscore/Methods/StringMethods.php

https://bitbucket.org/larryg/powerhut · PHP · 358 lines · 144 code · 46 blank · 168 comment · 17 complexity · 4bbeaa5613c26c12cd52c187aec0d45a MD5 · raw file

  1. <?php
  2. namespace Underscore\Methods;
  3. use Illuminate\Support\Str;
  4. use Underscore\Types\String;
  5. /**
  6. * Methods to manage strings
  7. */
  8. class StringMethods extends Str
  9. {
  10. ////////////////////////////////////////////////////////////////////
  11. ////////////////////////////// CREATE /////////////////////////////
  12. ////////////////////////////////////////////////////////////////////
  13. /**
  14. * Create a string from a number
  15. *
  16. * @param integer $count A number
  17. * @param string $many If many
  18. * @param string $one If one
  19. * @param string $zero If one
  20. * @return string A string
  21. */
  22. public static function accord($count, $many, $one, $zero = null)
  23. {
  24. if($count == 1) $output = $one;
  25. else if($count == 0 and !empty($zero)) $output = $zero;
  26. else $output = $many;
  27. return sprintf($output, $count);
  28. }
  29. /**
  30. * Generates a random suite of words
  31. *
  32. * @param integer $words The number of words
  33. * @param integer $length The length of each word
  34. *
  35. * @return string
  36. */
  37. public static function randomStrings($words, $length = 10)
  38. {
  39. return String::from('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
  40. ->shuffle()
  41. ->split($length)
  42. ->slice(0, $words)
  43. ->implode(' ')
  44. ->obtain();
  45. }
  46. ////////////////////////////////////////////////////////////////////
  47. ////////////////////////////// ANALYZE /////////////////////////////
  48. ////////////////////////////////////////////////////////////////////
  49. /**
  50. * Get a String's length
  51. *
  52. * @param string $string
  53. *
  54. * @return integer
  55. */
  56. public static function length($string)
  57. {
  58. return mb_strlen($string);
  59. }
  60. /**
  61. * Check if a string is an IP
  62. *
  63. * @return boolean
  64. */
  65. public static function isIp($string)
  66. {
  67. return filter_var($string, FILTER_VALIDATE_IP) !== false;
  68. }
  69. /**
  70. * Check if a string is an email
  71. *
  72. * @return boolean
  73. */
  74. public static function isEmail($string)
  75. {
  76. return filter_var($string, FILTER_VALIDATE_EMAIL) !== false;
  77. }
  78. /**
  79. * Check if a string is an url
  80. *
  81. * @return boolean
  82. */
  83. public static function isUrl($string)
  84. {
  85. return filter_var($string, FILTER_VALIDATE_URL) !== false;
  86. }
  87. ////////////////////////////////////////////////////////////////////
  88. ///////////////////////////// FETCH FROM ///////////////////////////
  89. ////////////////////////////////////////////////////////////////////
  90. /**
  91. * Find one or more needles in one or more haystacks
  92. *
  93. * @param array|string $string The haystack(s) to search in
  94. * @param array|string $needle The needle(s) to search for
  95. * @param boolean $caseSensitive Whether the function is case sensitive or not
  96. * @param boolean $absolute Whether all needle need to be found or whether one is enough
  97. * @return boolean Found or not
  98. */
  99. public static function find($string, $needle, $caseSensitive = false, $absolute = false)
  100. {
  101. // If several needles
  102. if (is_array($needle) or is_array($string)) {
  103. if (is_array($needle)) {
  104. $sliceFrom = $needle;
  105. $sliceTo = $string;
  106. } else {
  107. $sliceFrom = $string;
  108. $sliceTo = $needle;
  109. }
  110. $found = 0;
  111. foreach ($sliceFrom as $need) {
  112. if(static::find($sliceTo, $need, $absolute, $caseSensitive)) $found++;
  113. }
  114. return ($absolute) ? count($sliceFrom) == $found : $found > 0;
  115. }
  116. // If not case sensitive
  117. if (!$caseSensitive) {
  118. $string = strtolower($string);
  119. $needle = strtolower($needle);
  120. }
  121. // If string found
  122. $pos = strpos($string, $needle);
  123. return !($pos === false);
  124. }
  125. /**
  126. * Slice a string with another string
  127. */
  128. public static function slice($string, $slice)
  129. {
  130. $sliceTo = static::sliceTo($string, $slice);
  131. $sliceFrom = static::sliceFrom($string, $slice);
  132. return array($sliceTo, $sliceFrom);
  133. }
  134. /**
  135. * Slice a string from a certain point
  136. */
  137. public static function sliceFrom($string, $slice)
  138. {
  139. $slice = strpos($string, $slice);
  140. return substr($string, $slice);
  141. }
  142. /**
  143. * Slice a string up to a certain point
  144. */
  145. public static function sliceTo($string, $slice)
  146. {
  147. $slice = strpos($string, $slice);
  148. return substr($string, 0, $slice);
  149. }
  150. /**
  151. * Get the base class in a namespace
  152. *
  153. * @param string $string
  154. *
  155. * @return string
  156. */
  157. public static function baseClass($string)
  158. {
  159. $string = static::replace($string, '\\', '/');
  160. return basename($string);
  161. }
  162. ////////////////////////////////////////////////////////////////////
  163. /////////////////////////////// ALTER //////////////////////////////
  164. ////////////////////////////////////////////////////////////////////
  165. /**
  166. * Prepend a string with another
  167. *
  168. * @param string $string The string
  169. * @param string $with What to prepend with
  170. *
  171. * @return string
  172. */
  173. public static function prepend($string, $with)
  174. {
  175. return $with.$string;
  176. }
  177. /**
  178. * Append a string to another
  179. *
  180. * @param string $string The string
  181. * @param string $with What to append with
  182. *
  183. * @return string
  184. */
  185. public static function append($string, $with)
  186. {
  187. return $string.$with;
  188. }
  189. /**
  190. * Remove part of a string
  191. */
  192. public static function remove($string, $remove)
  193. {
  194. // If we only have one string to remove
  195. if(!is_array($remove)) $string = str_replace($remove, null, $string);
  196. // Else, use Regex
  197. else $string = preg_replace('#(' .implode('|', $remove). ')#', null, $string);
  198. // Trim and return
  199. return trim($string);
  200. }
  201. /**
  202. * Correct arguments order for str_replace
  203. */
  204. public static function replace($string, $replace, $with)
  205. {
  206. return str_replace($replace, $with, $string);
  207. }
  208. /**
  209. * Toggles a string between two states
  210. *
  211. * @param string $string The string to toggle
  212. * @param string $first First value
  213. * @param string $second Second value
  214. * @param boolean $loose Whether a string neither matching 1 or 2 should be changed
  215. * @return string The toggled string
  216. */
  217. public static function toggle($string, $first, $second, $loose = false)
  218. {
  219. // If the string given match none of the other two, and we're in strict mode, return it
  220. if (!$loose and !in_array($string, array($first, $second))) {
  221. return $string;
  222. }
  223. return $string == $first ? $second : $first;
  224. }
  225. /**
  226. * Slugifies a string
  227. */
  228. public static function slugify($string, $separator = '-')
  229. {
  230. $string = str_replace('_', ' ', $string);
  231. return static::slug($string, $separator);
  232. }
  233. /**
  234. * Explode a string into an array
  235. */
  236. public static function explode($string, $with, $limit = null)
  237. {
  238. if (!$limit) return explode($with, $string);
  239. return explode($with, $string, $limit);
  240. }
  241. /**
  242. * Lowercase a string
  243. *
  244. * @param string $string
  245. *
  246. * @return string
  247. */
  248. public static function lower($string)
  249. {
  250. return mb_strtolower($string, 'UTF-8');
  251. }
  252. /**
  253. * Lowercase a string
  254. *
  255. * @param string $string
  256. *
  257. * @return string
  258. */
  259. public static function upper($string)
  260. {
  261. return mb_strtoupper($string, 'UTF-8');
  262. }
  263. /**
  264. * Convert a string to title case
  265. *
  266. * @param string $string
  267. *
  268. * @return string
  269. */
  270. public static function title($string)
  271. {
  272. return mb_convert_case($string, MB_CASE_TITLE, "UTF-8");
  273. }
  274. ////////////////////////////////////////////////////////////////////
  275. /////////////////////////// CASE SWITCHERS /////////////////////////
  276. ////////////////////////////////////////////////////////////////////
  277. /**
  278. * Convert a string to PascalCase
  279. *
  280. * @param string $string
  281. *
  282. * @return string
  283. */
  284. public static function toPascalCase($string)
  285. {
  286. return static::studly($string);
  287. }
  288. /**
  289. * Convert a string to snake_case
  290. *
  291. * @param string $string
  292. *
  293. * @return string
  294. */
  295. public static function toSnakeCase($string)
  296. {
  297. return preg_replace_callback('/([A-Z])/', function($match) {
  298. return '_'.strtolower($match[1]);
  299. }, $string);
  300. }
  301. /**
  302. * Convert a string to camelCase
  303. *
  304. * @param string $string
  305. *
  306. * @return string
  307. */
  308. public static function toCamelCase($string)
  309. {
  310. return static::camel($string);
  311. }
  312. }