nikolaposa / version
表示符合SemVer规范版本号的值对象。
4.2.0
2023-12-29 22:07 UTC
Requires
- php: ^8.1
- beberlei/assert: ^3.2
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.44
- phpstan/phpstan: ^1.10
- phpstan/phpstan-beberlei-assert: ^1.1
- phpstan/phpstan-phpunit: ^1.3
- phpunit/phpunit: ^10.5
This package is auto-updated.
Last update: 2024-09-08 12:42:20 UTC
README
表示符合 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许可证下发布 - 有关详细信息,请参阅许可证文件。