rayne / semantic-versioning
一个用于解析和比较语义版本的微型独立库。
1.1.0
2017-02-13 19:40 UTC
Requires
- php: >=5.6
Requires (Dev)
- phpunit/phpunit: ~5.0
This package is auto-updated.
Last update: 2024-08-29 03:29:49 UTC
README
这是一个兼容 Semantic Versioning 2.0 的用于解析和比较语义版本的微型独立库。
依赖关系
生产环境
- PHP 5.6 或更高版本
开发环境
- Composer
- Git
- PHPUnit
设置
下载 Composer 并安装 rayne/semantic-versioning
。
composer require rayne/semantic-versioning
测试
-
克隆仓库
git clone https://github.com/rayne/semantic-versioning.php.git
-
安装开发依赖
composer install --dev
-
运行测试
./vendor/bin/phpunit
示例
该库包含以下类
-
InvalidVersionException
:当SemanticVersion
在无效输入上抛出时 -
SemanticComparator
:用于比较SemanticVersion
对象的语义版本比较器 -
SemanticVersion
:一个语义版本解析器,当遇到无效版本时抛出RuntimeException
示例是测试套件的一部分。有关更多信息,请查看 tests
目录。
解析语义版本
use Rayne\SemanticVersioning\SemanticVersion; $version = new SemanticVersion('1.0.0-beta+exp.sha.5114f85'); assert('1.0.0-beta+exp.sha.5114f85' === (string) $version); assert( 1 === $version->getMajor()); assert( 0 === $version->getMinor()); assert( 0 === $version->getPatch()); assert( 'beta' === $version->getPre()); assert( 'exp.sha.5114f85' === $version->getMeta()); assert('1.0.0-beta+exp.sha.5114f85' === $version->getVersion()); assert(true === $version->isMajorRelease()); assert(false === $version->isMinorRelease()); assert(false === $version->isPatchRelease()); assert(true === $version->isPreRelease());
比较语义版本
use Rayne\SemanticVersioning\SemanticComparator; use Rayne\SemanticVersioning\SemanticVersion; $comparator = new SemanticComparator; $alpha = new SemanticVersion('1.0.0-alpha'); $candidate = new SemanticVersion('1.0.0-rc.1'); $candidate_meta = new SemanticVersion('1.0.0-rc.1+ci'); $release = new SemanticVersion('1.0.0'); // $alpha < $candidate assert($comparator($alpha, $candidate) < 0); assert($comparator->compare($alpha, $candidate) < 0); // $candidate == $candidate_meta assert($comparator($candidate, $candidate_meta) == 0); assert($comparator->compare($candidate, $candidate_meta) == 0); // $release > $candidate assert($comparator($release, $candidate) > 0); assert($comparator->compare($release, $candidate) > 0);
排序语义版本
use Rayne\SemanticVersioning\SemanticComparator; use Rayne\SemanticVersioning\SemanticVersion; $versions = [ $candidate = new SemanticVersion('1.0.0-rc.1'), $release = new SemanticVersion('1.0.0'), $alpha = new SemanticVersion('1.0.0-alpha'), ]; // Sort by semantic precedence. usort($versions, new SemanticComparator); assert($versions[0] === $alpha); assert($versions[1] === $candidate); assert($versions[2] === $release);