/vendor/danielstjules/stringy/src/StaticStringy.php

https://gitlab.com/dae.nuli/toko · PHP · 148 lines · 34 code · 10 blank · 104 comment · 4 complexity · ded68d4e9e4a96f2ed2e504060a0f03f MD5 · raw file

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