BaseStringHelper.php 4.83 KB
Newer Older
Qiang Xue committed
1 2 3 4 5 6 7
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

8
namespace yii\helpers;
Qiang Xue committed
9

Qiang Xue committed
10 11
use yii\base\InvalidParamException;

Qiang Xue committed
12
/**
13
 * BaseStringHelper provides concrete implementation for [[StringHelper]].
14
 *
15
 * Do not use BaseStringHelper. Use [[StringHelper]] instead.
Qiang Xue committed
16 17 18 19 20
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @author Alex Makarov <sam@rmcreative.ru>
 * @since 2.0
 */
21
class BaseStringHelper
Qiang Xue committed
22 23 24
{
	/**
	 * Returns the number of bytes in the given string.
Qiang Xue committed
25
	 * This method ensures the string is treated as a byte array by using `mb_strlen()`.
Qiang Xue committed
26 27 28
	 * @param string $string the string being measured for length
	 * @return integer the number of bytes in the given string.
	 */
29
	public static function byteLength($string)
Qiang Xue committed
30
	{
Qiang Xue committed
31
		return mb_strlen($string, '8bit');
Qiang Xue committed
32 33 34 35
	}

	/**
	 * Returns the portion of string specified by the start and length parameters.
Qiang Xue committed
36
	 * This method ensures the string is treated as a byte array by using `mb_substr()`.
Qiang Xue committed
37 38 39 40 41 42
	 * @param string $string the input string. Must be one character or longer.
	 * @param integer $start the starting position
	 * @param integer $length the desired portion length
	 * @return string the extracted part of string, or FALSE on failure or an empty string.
	 * @see http://www.php.net/manual/en/function.substr.php
	 */
43
	public static function byteSubstr($string, $start, $length)
Qiang Xue committed
44
	{
Qiang Xue committed
45
		return mb_substr($string, $start, $length, '8bit');
Qiang Xue committed
46 47
	}

48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
	/**
	 * Returns the trailing name component of a path.
	 * This method is similar to the php function `basename()` except that it will
	 * treat both \ and / as directory separators, independent of the operating system.
	 * This method was mainly created to work on php namespaces. When working with real
	 * file paths, php's `basename()` should work fine for you.
	 * Note: this method is not aware of the actual filesystem, or path components such as "..".
	 *
	 * @param string $path A path string.
	 * @param string $suffix If the name component ends in suffix this will also be cut off.
	 * @return string the trailing name component of the given path.
	 * @see http://www.php.net/manual/en/function.basename.php
	 */
	public static function basename($path, $suffix = '')
	{
		if (($len = mb_strlen($suffix)) > 0 && mb_substr($path, -$len) == $suffix) {
			$path = mb_substr($path, 0, -$len);
		}
		$path = rtrim(str_replace('\\', '/', $path), '/\\');
		if (($pos = mb_strrpos($path, '/')) !== false) {
			return mb_substr($path, $pos + 1);
		}
		return $path;
	}

	/**
	 * Returns parent directory's path.
	 * This method is similar to `dirname()` except that it will treat
	 * both \ and / as directory separators, independent of the operating system.
	 *
	 * @param string $path A path string.
	 * @return string the parent directory's path.
	 * @see http://www.php.net/manual/en/function.basename.php
	 */
	public static function dirname($path)
	{
		$pos = mb_strrpos(str_replace('\\', '/', $path), '/');
		if ($pos !== false) {
			return mb_substr($path, 0, $pos);
		} else {
			return $path;
		}
	}
91

Qiang Xue committed
92 93
	/**
	 * Compares two strings or string arrays, and return their differences.
Qiang Xue committed
94
	 * This is a wrapper of the [phpspec/php-diff](https://packagist.org/packages/phpspec/php-diff) package.
Qiang Xue committed
95 96 97 98
	 * @param string|array $lines1 the first string or string array to be compared. If it is a string,
	 * it will be converted into a string array by breaking at newlines.
	 * @param string|array $lines2 the second string or string array to be compared. If it is a string,
	 * it will be converted into a string array by breaking at newlines.
Qiang Xue committed
99 100 101 102
	 * @param string $format the output format. It must be 'inline', 'unified', 'context', 'side-by-side', or 'array'.
	 * @return string|array the comparison result. An array is returned if `$format` is 'array'. For all other
	 * formats, a string is returned.
	 * @throws InvalidParamException if the format is invalid.
Qiang Xue committed
103
	 */
Qiang Xue committed
104
	public static function diff($lines1, $lines2, $format = 'inline')
Qiang Xue committed
105 106 107 108 109 110 111
	{
		if (!is_array($lines1)) {
			$lines1 = explode("\n", $lines1);
		}
		if (!is_array($lines2)) {
			$lines2 = explode("\n", $lines2);
		}
Qiang Xue committed
112 113 114 115 116 117
		foreach ($lines1 as $i => $line) {
			$lines1[$i] = rtrim($line, "\r\n");
		}
		foreach ($lines2 as $i => $line) {
			$lines2[$i] = rtrim($line, "\r\n");
		}
Qiang Xue committed
118 119
		switch ($format) {
			case 'inline':
Qiang Xue committed
120 121 122 123 124 125 126 127 128 129
				$renderer = new \Diff_Renderer_Html_Inline();
				break;
			case 'array':
				$renderer = new \Diff_Renderer_Html_Array();
				break;
			case 'side-by-side':
				$renderer = new \Diff_Renderer_Html_SideBySide();
				break;
			case 'context':
				$renderer = new \Diff_Renderer_Text_Context();
Qiang Xue committed
130 131
				break;
			case 'unified':
Qiang Xue committed
132
				$renderer = new \Diff_Renderer_Text_Unified();
Qiang Xue committed
133 134
				break;
			default:
Qiang Xue committed
135
				throw new InvalidParamException("Output format must be 'inline', 'side-by-side', 'array', 'context' or 'unified'.");
Qiang Xue committed
136
		}
Qiang Xue committed
137 138
		$diff = new \Diff($lines1, $lines2);
		return $diff->render($renderer);
Qiang Xue committed
139
	}
Qiang Xue committed
140
}