BaseStringHelper.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\helpers;
  8. use yii\base\InvalidParamException;
  9. /**
  10. * BaseStringHelper provides concrete implementation for [[StringHelper]].
  11. *
  12. * Do not use BaseStringHelper. Use [[StringHelper]] instead.
  13. *
  14. * @author Qiang Xue <[email protected]>
  15. * @author Alex Makarov <[email protected]>
  16. * @since 2.0
  17. */
  18. class BaseStringHelper
  19. {
  20. /**
  21. * Returns the number of bytes in the given string.
  22. * This method ensures the string is treated as a byte array by using `mb_strlen()`.
  23. * @param string $string the string being measured for length
  24. * @return integer the number of bytes in the given string.
  25. */
  26. public static function byteLength($string)
  27. {
  28. return mb_strlen($string, '8bit');
  29. }
  30. /**
  31. * Returns the portion of string specified by the start and length parameters.
  32. * This method ensures the string is treated as a byte array by using `mb_substr()`.
  33. * @param string $string the input string. Must be one character or longer.
  34. * @param integer $start the starting position
  35. * @param integer $length the desired portion length
  36. * @return string the extracted part of string, or FALSE on failure or an empty string.
  37. * @see http://www.php.net/manual/en/function.substr.php
  38. */
  39. public static function byteSubstr($string, $start, $length)
  40. {
  41. return mb_substr($string, $start, $length, '8bit');
  42. }
  43. /**
  44. * Returns the trailing name component of a path.
  45. * This method is similar to the php function `basename()` except that it will
  46. * treat both \ and / as directory separators, independent of the operating system.
  47. * This method was mainly created to work on php namespaces. When working with real
  48. * file paths, php's `basename()` should work fine for you.
  49. * Note: this method is not aware of the actual filesystem, or path components such as "..".
  50. *
  51. * @param string $path A path string.
  52. * @param string $suffix If the name component ends in suffix this will also be cut off.
  53. * @return string the trailing name component of the given path.
  54. * @see http://www.php.net/manual/en/function.basename.php
  55. */
  56. public static function basename($path, $suffix = '')
  57. {
  58. if (($len = mb_strlen($suffix)) > 0 && mb_substr($path, -$len) == $suffix) {
  59. $path = mb_substr($path, 0, -$len);
  60. }
  61. $path = rtrim(str_replace('\\', '/', $path), '/\\');
  62. if (($pos = mb_strrpos($path, '/')) !== false) {
  63. return mb_substr($path, $pos + 1);
  64. }
  65. return $path;
  66. }
  67. /**
  68. * Returns parent directory's path.
  69. * This method is similar to `dirname()` except that it will treat
  70. * both \ and / as directory separators, independent of the operating system.
  71. *
  72. * @param string $path A path string.
  73. * @return string the parent directory's path.
  74. * @see http://www.php.net/manual/en/function.basename.php
  75. */
  76. public static function dirname($path)
  77. {
  78. $pos = mb_strrpos(str_replace('\\', '/', $path), '/');
  79. if ($pos !== false) {
  80. return mb_substr($path, 0, $pos);
  81. } else {
  82. return $path;
  83. }
  84. }
  85. /**
  86. * Compares two strings or string arrays, and return their differences.
  87. * This is a wrapper of the [phpspec/php-diff](https://packagist.org/packages/phpspec/php-diff) package.
  88. * @param string|array $lines1 the first string or string array to be compared. If it is a string,
  89. * it will be converted into a string array by breaking at newlines.
  90. * @param string|array $lines2 the second string or string array to be compared. If it is a string,
  91. * it will be converted into a string array by breaking at newlines.
  92. * @param string $format the output format. It must be 'inline', 'unified', 'context', 'side-by-side', or 'array'.
  93. * @return string|array the comparison result. An array is returned if `$format` is 'array'. For all other
  94. * formats, a string is returned.
  95. * @throws InvalidParamException if the format is invalid.
  96. */
  97. public static function diff($lines1, $lines2, $format = 'inline')
  98. {
  99. if (!is_array($lines1)) {
  100. $lines1 = explode("\n", $lines1);
  101. }
  102. if (!is_array($lines2)) {
  103. $lines2 = explode("\n", $lines2);
  104. }
  105. foreach ($lines1 as $i => $line) {
  106. $lines1[$i] = rtrim($line, "\r\n");
  107. }
  108. foreach ($lines2 as $i => $line) {
  109. $lines2[$i] = rtrim($line, "\r\n");
  110. }
  111. switch ($format) {
  112. case 'inline':
  113. $renderer = new \Diff_Renderer_Html_Inline();
  114. break;
  115. case 'array':
  116. $renderer = new \Diff_Renderer_Html_Array();
  117. break;
  118. case 'side-by-side':
  119. $renderer = new \Diff_Renderer_Html_SideBySide();
  120. break;
  121. case 'context':
  122. $renderer = new \Diff_Renderer_Text_Context();
  123. break;
  124. case 'unified':
  125. $renderer = new \Diff_Renderer_Text_Unified();
  126. break;
  127. default:
  128. throw new InvalidParamException("Output format must be 'inline', 'side-by-side', 'array', 'context' or 'unified'.");
  129. }
  130. $diff = new \Diff($lines1, $lines2);
  131. return $diff->render($renderer);
  132. }
  133. }