nikolaposa / version
Value Object that represents a SemVer-compliant version number.
Installs: 3 734 661
Dependents: 13
Suggesters: 0
Security: 0
Stars: 139
Watchers: 6
Forks: 10
Open Issues: 0
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-11-08 12:55:32 UTC
README
Value Object that represents a SemVer-compliant version number.
Installation
The preferred method of installation is via Composer. Run the following
command to install the latest version of a package and add it to your project's composer.json
:
composer require nikolaposa/version
Usage
Creating a Version object via named constructor and accessing its values
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
Creating a Version object from a string
$v = Version::fromString('1.10.0'); echo $v->toString(); //1.10.0
Comparing Version objects
$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)
Matching Version objects against constraints
$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)
Version operations
$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
Version Collection
$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
Credits
License
Released under MIT License - see the License File for details.