StaticStringy.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. namespace Stringy;
  3. use BadMethodCallException;
  4. use ReflectionClass;
  5. use ReflectionMethod;
  6. /**
  7. * Class StaticStringy
  8. *
  9. * @method static string append(string $str, string $stringAppend, string $encoding = null)
  10. * @method static string at(string $str, int $index, string $encoding = null)
  11. * @method static string between(string $str, string $start, string $end, int $offset = 0, string $encoding = null)
  12. * @method static string camelize(string $str, string $encoding = null)
  13. * @method static string chars(string $str, string $encoding = null)
  14. * @method static string collapseWhitespace(string $str, string $encoding = null)
  15. * @method static bool contains(string $str, string $needle, bool $caseSensitive = true, string $encoding = null)
  16. * @method static bool containsAll(string $str, string[] $needle, bool $caseSensitive = true, string $encoding = null)
  17. * @method static bool containsAny(string $str, string[] $needle, bool $caseSensitive = true, string $encoding = null)
  18. * @method static int count(string $str, string $encoding = null)
  19. * @method static int countSubstr(string $str, string $substring, bool $caseSensitive = true, string $encoding = null)
  20. * @method static string dasherize(string $str, string $encoding = null)
  21. * @method static string delimit(string $str, string $delimiter, string $encoding = null)
  22. * @method static bool endsWith(string $str, string $substring, bool $caseSensitive = true, string $encoding = null)
  23. * @method static bool endsWithAny(string $str, string[] $substrings, bool $caseSensitive = true, string $encoding = null)
  24. * @method static string ensureLeft(string $str, string $substring, string $encoding = null)
  25. * @method static string ensureRight(string $str, string $substring, string $encoding = null)
  26. * @method static string first(string $str, int $n, string $encoding = null)
  27. * @method static bool hasLowerCase(string $str, string $encoding = null)
  28. * @method static bool hasUpperCase(string $str, string $encoding = null)
  29. * @method static string htmlDecode(string $str, int $flags = ENT_COMPAT, string $encoding = null)
  30. * @method static string htmlEncode(string $str, int $flags = ENT_COMPAT, string $encoding = null)
  31. * @method static string humanize(string $str, string $encoding = null)
  32. * @method static int indexOf(string $str, string $needle, int $offset = 0, string $encoding = null)
  33. * @method static int indexOfLast(string $str, string $needle, int $offset = 0, string $encoding = null)
  34. * @method static string insert(string $str, string $substring, int $index = 0, string $encoding = null)
  35. * @method static bool isAlpha(string $str, string $encoding = null)
  36. * @method static bool isAlphanumeric(string $str, string $encoding = null)
  37. * @method static bool isBase64(string $str, string $encoding = null)
  38. * @method static bool isBlank(string $str, string $encoding = null)
  39. * @method static bool isHexadecimal(string $str, string $encoding = null)
  40. * @method static bool isJson(string $str, string $encoding = null)
  41. * @method static bool isLowerCase(string $str, string $encoding = null)
  42. * @method static bool isSerialized(string $str, string $encoding = null)
  43. * @method static bool isUpperCase(string $str, string $encoding = null)
  44. * @method static string last(string $str, string $encoding = null)
  45. * @method static int length(string $str, string $encoding = null)
  46. * @method static string[] lines(string $str, string $encoding = null)
  47. * @method static string longestCommonPrefix(string $str, string $otherStr, string $encoding = null)
  48. * @method static string longestCommonSuffix(string $str, string $otherStr, string $encoding = null)
  49. * @method static string longestCommonSubstring(string $str, string $otherStr, string $encoding = null)
  50. * @method static string lowerCaseFirst(string $str, string $encoding = null)
  51. * @method static string pad(string $str, int $length, string $padStr = ' ', string $padType = 'right', string $encoding = null)
  52. * @method static string padBoth(string $str, int $length, string $padStr = ' ', string $encoding = null)
  53. * @method static string padLeft(string $str, int $length, string $padStr = ' ', string $encoding = null)
  54. * @method static string padRight(string $str, int $length, string $padStr = ' ', string $encoding = null)
  55. * @method static string prepend(string $str, string $string, string $encoding = null)
  56. * @method static string regexReplace(string $str, string $pattern, string $replacement, string $options = 'msr', string $encoding = null)
  57. * @method static string removeLeft(string $str, string $substring, string $encoding = null)
  58. * @method static string removeRight(string $str, string $substring, string $encoding = null)
  59. * @method static string repeat(string $str, int $multiplier, string $encoding = null)
  60. * @method static string replace(string $str, string $search, string $replacement, string $encoding = null)
  61. * @method static string reverse(string $str, string $encoding = null)
  62. * @method static string safeTruncate(string $str, int $length, string $substring = '', string $encoding = null)
  63. * @method static string shuffle(string $str, string $encoding = null)
  64. * @method static string slugify(string $str, string $replacement = '-', string $encoding = null)
  65. * @method static string slice(string $str, int $start, int $end = null, string $encoding = null)
  66. * @method static string split(string $str, string $pattern, int $limit = null, string $encoding = null)
  67. * @method static bool startsWith(string $str, string $substring, bool $caseSensitive = true, string $encoding = null)
  68. * @method static bool startsWithAny(string $str, string[] $substrings, bool $caseSensitive = true, string $encoding = null)
  69. * @method static string stripWhitespace(string $str, string $encoding = null)
  70. * @method static string substr(string $str, int $start, int $length = null, string $encoding = null)
  71. * @method static string surround(string $str, string $substring, string $encoding = null)
  72. * @method static string swapCase(string $str, string $encoding = null)
  73. * @method static string tidy(string $str, string $encoding = null)
  74. * @method static string titleize(string $str, string $encoding = null)
  75. * @method static string toAscii(string $str, string $language = 'en', bool $removeUnsupported = true, string $encoding = null)
  76. * @method static bool toBoolean(string $str, string $encoding = null)
  77. * @method static string toLowerCase(string $str, string $encoding = null)
  78. * @method static string toSpaces(string $str, int $tabLength = 4, string $encoding = null)
  79. * @method static string toTabs(string $str, int $tabLength = 4, string $encoding = null)
  80. * @method static string toTitleCase(string $str, string $encoding = null)
  81. * @method static string toUpperCase(string $str, string $encoding = null)
  82. * @method static string trim(string $str, string $chars = null, string $encoding = null)
  83. * @method static string trimLeft(string $str, string $chars = null, string $encoding = null)
  84. * @method static string trimRight(string $str, string $chars = null, string $encoding = null)
  85. * @method static string truncate(string $str, int $length, string $substring = '', string $encoding = null)
  86. * @method static string underscored(string $str, string $encoding = null)
  87. * @method static string upperCamelize(string $str, string $encoding = null)
  88. * @method static string upperCaseFirst(string $str, string $encoding = null)
  89. */
  90. class StaticStringy
  91. {
  92. /**
  93. * A mapping of method names to the numbers of arguments it accepts. Each
  94. * should be two more than the equivalent Stringy method. Necessary as
  95. * static methods place the optional $encoding as the last parameter.
  96. *
  97. * @var string[]
  98. */
  99. protected static $methodArgs = null;
  100. /**
  101. * Creates an instance of Stringy and invokes the given method with the
  102. * rest of the passed arguments. The optional encoding is expected to be
  103. * the last argument. For example, the following:
  104. * StaticStringy::slice('fòôbàř', 0, 3, 'UTF-8'); translates to
  105. * Stringy::create('fòôbàř', 'UTF-8')->slice(0, 3);
  106. * The result is not cast, so the return value may be of type Stringy,
  107. * integer, boolean, etc.
  108. *
  109. * @param string $name
  110. * @param mixed[] $arguments
  111. *
  112. * @return Stringy
  113. *
  114. * @throws \BadMethodCallException
  115. */
  116. public static function __callStatic($name, $arguments)
  117. {
  118. if (!static::$methodArgs) {
  119. $stringyClass = new ReflectionClass('Stringy\Stringy');
  120. $methods = $stringyClass->getMethods(ReflectionMethod::IS_PUBLIC);
  121. foreach ($methods as $method) {
  122. $params = $method->getNumberOfParameters() + 2;
  123. static::$methodArgs[$method->name] = $params;
  124. }
  125. }
  126. if (!isset(static::$methodArgs[$name])) {
  127. throw new BadMethodCallException($name . ' is not a valid method');
  128. }
  129. $numArgs = count($arguments);
  130. $str = ($numArgs) ? $arguments[0] : '';
  131. if ($numArgs === static::$methodArgs[$name]) {
  132. $args = array_slice($arguments, 1, -1);
  133. $encoding = $arguments[$numArgs - 1];
  134. } else {
  135. $args = array_slice($arguments, 1);
  136. $encoding = null;
  137. }
  138. $stringy = Stringy::create($str, $encoding);
  139. $result = call_user_func_array([$stringy, $name], $args);
  140. $cast = function($val) {
  141. if (is_object($val) && $val instanceof Stringy) {
  142. return (string) $val;
  143. } else {
  144. return $val;
  145. }
  146. };
  147. return is_array($result) ? array_map($cast, $result) : $cast($result);
  148. }
  149. }