You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
139 lines
4.8 KiB
139 lines
4.8 KiB
12 years ago
|
<?php
|
||
|
/**
|
||
|
* @link http://www.yiiframework.com/
|
||
|
* @copyright Copyright (c) 2008 Yii Software LLC
|
||
|
* @license http://www.yiiframework.com/license/
|
||
|
*/
|
||
|
|
||
12 years ago
|
namespace yii\helpers;
|
||
12 years ago
|
|
||
12 years ago
|
use yii\base\InvalidParamException;
|
||
|
|
||
12 years ago
|
/**
|
||
11 years ago
|
* BaseStringHelper provides concrete implementation for [[StringHelper]].
|
||
12 years ago
|
*
|
||
11 years ago
|
* Do not use BaseStringHelper. Use [[StringHelper]] instead.
|
||
12 years ago
|
*
|
||
|
* @author Qiang Xue <qiang.xue@gmail.com>
|
||
|
* @author Alex Makarov <sam@rmcreative.ru>
|
||
|
* @since 2.0
|
||
|
*/
|
||
11 years ago
|
class BaseStringHelper
|
||
12 years ago
|
{
|
||
|
/**
|
||
|
* Returns the number of bytes in the given string.
|
||
12 years ago
|
* This method ensures the string is treated as a byte array by using `mb_strlen()`.
|
||
12 years ago
|
* @param string $string the string being measured for length
|
||
|
* @return integer the number of bytes in the given string.
|
||
|
*/
|
||
|
public static function strlen($string)
|
||
|
{
|
||
12 years ago
|
return mb_strlen($string, '8bit');
|
||
12 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Returns the portion of string specified by the start and length parameters.
|
||
12 years ago
|
* This method ensures the string is treated as a byte array by using `mb_substr()`.
|
||
12 years ago
|
* @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
|
||
|
*/
|
||
|
public static function substr($string, $start, $length)
|
||
|
{
|
||
12 years ago
|
return mb_substr($string, $start, $length, '8bit');
|
||
12 years ago
|
}
|
||
|
|
||
|
/**
|
||
12 years ago
|
* Returns the trailing name component of a path.
|
||
11 years ago
|
* This method is similar to the php function `basename()` except that it will
|
||
|
* treat both \ and / as directory separators, independent of the operating system.
|
||
12 years ago
|
* This method was mainly created to work on php namespaces. When working with real
|
||
|
* file paths, php's `basename()` should work fine for you.
|
||
12 years ago
|
* Note: this method is not aware of the actual filesystem, or path components such as "..".
|
||
12 years ago
|
* @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;
|
||
|
}
|
||
12 years ago
|
|
||
|
/**
|
||
11 years ago
|
* 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;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
12 years ago
|
* Compares two strings or string arrays, and return their differences.
|
||
12 years ago
|
* This is a wrapper of the [phpspec/php-diff](https://packagist.org/packages/phpspec/php-diff) package.
|
||
12 years ago
|
* @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.
|
||
12 years ago
|
* @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.
|
||
12 years ago
|
*/
|
||
12 years ago
|
public static function diff($lines1, $lines2, $format = 'inline')
|
||
12 years ago
|
{
|
||
|
if (!is_array($lines1)) {
|
||
|
$lines1 = explode("\n", $lines1);
|
||
|
}
|
||
|
if (!is_array($lines2)) {
|
||
|
$lines2 = explode("\n", $lines2);
|
||
|
}
|
||
12 years ago
|
foreach ($lines1 as $i => $line) {
|
||
|
$lines1[$i] = rtrim($line, "\r\n");
|
||
|
}
|
||
|
foreach ($lines2 as $i => $line) {
|
||
|
$lines2[$i] = rtrim($line, "\r\n");
|
||
|
}
|
||
12 years ago
|
switch ($format) {
|
||
|
case 'inline':
|
||
12 years ago
|
$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();
|
||
12 years ago
|
break;
|
||
|
case 'unified':
|
||
12 years ago
|
$renderer = new \Diff_Renderer_Text_Unified();
|
||
12 years ago
|
break;
|
||
|
default:
|
||
12 years ago
|
throw new InvalidParamException("Output format must be 'inline', 'side-by-side', 'array', 'context' or 'unified'.");
|
||
12 years ago
|
}
|
||
12 years ago
|
$diff = new \Diff($lines1, $lines2);
|
||
|
return $diff->render($renderer);
|
||
12 years ago
|
}
|
||
12 years ago
|
}
|