nikolaposa/version

表示符合SemVer规范版本号的值对象。

4.2.0 2023-12-29 22:07 UTC

README

Build Scrutinizer Code Quality Code Coverage Latest Stable Version PDS Skeleton

表示符合 SemVer 规范的版本号的值对象。

安装

首选安装方法是通过 Composer。运行以下命令安装最新版本的包并将其添加到您的项目中的 composer.json

composer require nikolaposa/version

用法

通过命名构造函数创建版本对象并访问其值

use Version\Extension\Build;
use Version\Extension\PreRelease;
use Version\Version;

$v = Version::from(2, 0, 0, PreRelease::from('alpha', '1'), Build::from('20191222232137'));

echo $v->getMajor(); //2
echo $v->getMinor(); //0
echo $v->getPatch(); //0
print_r($v->getPreRelease()->getIdentifiers()); //Array([0] => alpha [1] => 1)
echo $v->getPreRelease()->toString(); //alpha.1
print_r($v->getBuild()->getIdentifiers()); //Array([0] => 20191222232137)
echo $v->getBuild()->toString(); //20191222232137

从字符串创建版本对象

$v = Version::fromString('1.10.0');

echo $v->toString(); //1.10.0

比较版本对象

$v1 = Version::fromString('1.10.0');
$v2 = Version::fromString('2.3.3');

var_dump($v1->isLessThan($v2)); //bool(true)
var_dump($v1->isGreaterThan($v2)); //bool(false)
var_dump($v1->isEqualTo($v2)); //bool(false)

var_dump($v2->isLessThan($v1)); //bool(false)
var_dump($v2->isGreaterThan($v1)); //bool(true)

将版本对象与约束匹配

$v = Version::fromString('2.2.0');

var_dump($v->matches(OperationConstraint::equalTo(Version::fromString('2.2.0')))); //bool(true)
var_dump($v->matches(OperationConstraint::notEqualTo(Version::fromString('2.2.0')))); //bool(true)
var_dump($v->matches(OperationConstraint::fromString('>=2.0.0 <2.3.0'))); //bool(true)
var_dump($v->matches(OperationConstraint::fromString('>=2.0.0 <2.1.0 || 2.2.0'))); //bool(true)

版本操作

$v = Version::fromString('1.10.0');

$v1101 = $v->incrementPatch();
echo $v1101->toString(); //1.10.1

$v1110 = $v1101->incrementMinor();
echo $v1110->toString(); //1.11.0

$v2 = $v1101->incrementMajor();
echo $v2->toString(); //2.0.0

$v2Alpha = $v2->withPreRelease('alpha');
echo $v2Alpha->toString(); //2.0.0-alpha

$v2Alpha111 = $v2Alpha->withBuild('111');
echo $v2Alpha111->toString(); //2.0.0-alpha+111

版本集合

$versions = new VersionCollection(
    Version::from(1, 0, 1),
    Version::fromString('1.1.0'),
    Version::fromString('2.3.3')
);

echo count($versions); //3

$versions = $versions->sortedDescending();

//Outputs: 2.3.3, 1.1.0, 1.0.1
foreach ($versions as $version) {
    echo $version->toString();
}

$minorReleases = $versions->minorReleases();
echo $minorReleases->first(); //1.1.0

致谢

许可证

在MIT许可证下发布 - 有关详细信息,请参阅许可证文件