someshwer / version-comparison
该Laravel包用于比较两个版本字符串并返回布尔结果。该包还可以解析类似 (($v > 1.24.0) && ($v < 1.25.1.0)) || ($v == 1.26 || $v == 1.27) 的版本表达式,其中$v必须用要比较的版本号替换。因此,该包可用于
2.1.1
2018-10-18 12:59 UTC
Requires
- php: ^5.4 || ^7.0
- composer/semver: ^1.4
- symfony/expression-language: ^4.1
Requires (Dev)
- composer/semver: ^1.4
- phpunit/phpunit: ^4.7|>=5.0 <5.4
- symfony/expression-language: ^4.1
README
此Laravel包用于比较两个版本字符串并返回布尔结果。该包还可以解析类似 (($v > 1.24.0) && ($v < 1.25.1.0)) || ($v == 1.26 || $v == 1.27) 的版本表达式,其中$v必须用要比较的版本号替换。因此,该包可用于版本表达式评估。
安装
要下载最新版本,请在终端中输入以下行并按回车键。(当前此包的稳定版本是2.1 LTS)
composer require someshwer/version-comparison
然后,将以下服务提供者添加到config/app.php配置文件中的providers数组中。
Ex: 'providers' => [
... ,
... ,
Someshwer\VersionComparison\VersionComparisonServiceProvider::class
];
然后,将以下别名添加到config/app.php配置文件中的aliases数组中。
Ex: 'aliases' => [
... ,
.... ,
'VersionComparator' => Someshwer\VersionComparison\Facades\VersionComparator::class
];
现在,只需在终端中运行以下命令即可简单地发布服务提供者。(可选步骤)
$ php artisan vendor:publish --provider="Someshwer\VersionComparison\VersionComparisonServiceProvider"
That's all! You are done with package installation...
用法
- VersionComparator::compare():此方法使用指定的运算符比较两个版本字符串并返回布尔结果。它接受三个参数
VersionComparator::compare(Version1 String, Operator String, Version2 String); // compare(Version1, Operator, Version2);
a. Version number (First Parameter)
b. Operator (Second Parameter)
c. Version number (Third Parameter)
Ex: VersionComparator::compare('1.2.5', '>', '1.2.2'); // Gives the Boolean result either TRUE or FALSE.
For this above example comparison, the result is TRUE.
- VersionComparator::evaluate():此方法通常以以下形式评估版本表达式: (("1.2" > "1.24.0") && ("1.2" < "1.25.1.0")) || ("1.2" == "1.26" || "1.2" == "1.27") 它仅接受一个参数,即上述类型的表达式。
VersionComparator::evaluate('Version Expression');
Ex: VersionComparator::evaluate('(("1.2" > "1.24.0") && ("1.2" < "1.25.1.0")) || ("1.2" == "1.26" || "1.2" == "1.27")');
// Gives Boolean result either TRUE or FALSE
- VersionComparator::substituteThenEvaluate():此方法通常评估以下类型的版本表达式: (($v > "1.24.0") && ($v < "1.25.1.0")) || ($v == "1.26" || $v == "1.27") 此方法接受两个参数。它们是:a. 第一个是将替换表达式中的$v的版本号。b. 第二个是在$v替换后要评估的表达式。
注意:$v是要替换的版本号的变量(第一个参数)。
VersionComparator::substituteThenEvaluate('Version Number', 'Version Expression');
Ex: VersionComparator::substituteThenEvaluate('2.1.4', '((1.2 > 1.24.0) && (1.2 < 1.25.1.0)) || (1.2 == 1.26 || 1.2 == 1.27)');
Hence, in this example: $v in the expression is replaced by '2.1.4'.
After successful evaluation it gives the Boolean result either TRUE or FLASE.
Note: This package requires and depends on two other external third-party packages. They are:
1. composer/semver version 1.4.2 (https://github.com/composer/semver) and
2. symfony/expression-language version 4.1 (https://symfony.com.cn/doc/current/components/expression_language.html).
You no need to manually install them in your project. They will be automatically downloaded to your vendor folder when ever
you install "someshwer/version-comparison" package by executing the following command in terminal.
-> composer require someshwer/version-comparison
响应
响应内容必须是json。此包的方法几乎总是以JSON格式返回响应。
Example:
1. Sample success response:
{
status: "SUCCESS",
message: "Expression successfully evaluated!",
error_message: false,
}
2. Sample validation error response:
{
status: "ERROR",
error_type: "validation",
message: "Unable to evaluate the expression!",
error_message: "Invalid expression! Unexpected ")" around position 44 for expression `(('1.2' > '1.24.0')) && ('1.2' < '1.25.1.0')) || ('1.2' == '1.26' || '1.2' == '1.27')`. Check log for more info.",
}
注意:如果您想,您也可以自定义响应格式。
测试代码片段
Route::get('/test', function () {
$result_one = \VersionComparator::compare('1.2.8', '>', '1.2');
$result_two = \VersionComparator::evaluate('((1.2.8 > 1.24.0) && (1.2.8 < 1.25.1.0)) || (1.2.8 == 1.26 || 1.2.8 == 1.27)');
$result_three = \VersionComparator::substituteThenEvaluate('1.2', '(($v > 1.24.0) && ($v < 1.25.1.0)) || ($v == 1.26 || $v == 1.27)');
// return compact('result_one', 'result_two', 'result_three');
// return $result_one;
// return $result_two;
return $result_three;
});
如果您想测试此包,请将上面的代码复制并粘贴到您的web.php文件中,然后测试此版本-comparison/version-expression评估包。