1. Go to this page and download the library: Download gregpriday/php-version library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
gregpriday / php-version example snippets
use GregPriday\Version\Version;
use GregPriday\Version\Constraint\VersionConstraintParser;
// 1. Create a version object (strict mode by default).
$version = new Version('1.2.3-beta');
// 2. Inspect the version
echo "Major: " . $version->getMajor() . "\n"; // 1
echo "Minor: " . $version->getMinor() . "\n"; // 2
echo "Patch: " . $version->getPatch() . "\n"; // 3
echo "Pre-release: " . $version->getPreRelease() . "\n"; // beta
// 3. Check stability
if ($version->isStable()) {
echo "Version is stable.\n";
} else {
echo "Version is not stable.\n";
}
// 4. Bump the version (bump minor, reset patch to 0, remove pre-release)
$bumped = $version->bumpMinor();
echo "Bumped Version: " . $bumped->getExtraInfo()['version'] . "\n"; // "1.3.0"
// 5. Parse constraints and check if a version satisfies them
$parser = new VersionConstraintParser();
$rangeSet = $parser->parseConstraints('>=1.0.0 <2.0.0 || ^3.0.0');
if ($rangeSet->isSatisfiedBy($bumped)) {
echo $bumped->getExtraInfo()['version']." satisfies the constraint.\n";
} else {
echo $bumped->getExtraInfo()['version']." does not satisfy the constraint.\n";
}
// Strict mode (throws InvalidArgumentException if invalid)
$strictVersion = new Version('1.2.3');
// Loose mode
$looseVersion = new Version('v1.2', null, false);
// Internally becomes 1.2.0
$version = new Version('1.2.3-alpha+build.123');
// Basic components
echo $version->getMajor(); // 1
echo $version->getMinor(); // 2
echo $version->getPatch(); // 3
// Pre-release and build metadata
echo $version->getPreRelease(); // alpha
echo $version->getBuildMetadata(); // build.123
// Extra info array (
$version = new Version('1.0.0-rc1');
if ($version->isStable()) {
// Major >= 1 and no pre-release
echo "Stable release.\n";
} else {
echo "Not stable.\n"; // This will run in this example
}
if ($version->isPreRelease()) {
echo "It's a pre-release!\n"; // True for "1.0.0-rc1"
}
$version = new Version('1.2.3-beta+build.123');
// Bump Major: becomes 2.0.0 (clears pre-release & build by default)
$bumpedMajor = $version->bumpMajor();
echo $bumpedMajor->getExtraInfo()['version']; // 2.0.0
// Bump Minor but preserve pre-release and build metadata
$bumpedMinor = $version->bumpMinor(true, true);
echo $bumpedMinor->getExtraInfo()['version']; // 1.3.0-beta+build.123
// Bump Patch: 1.2.4 (default clears pre-release and build)
$bumpedPatch = $version->bumpPatch();
echo $bumpedPatch->getExtraInfo()['version']; // 1.2.4
// Bump (or set) Pre-release:
// - If no pre-release, sets the given identifier.
// - If pre-release is something like "beta.1", it increments the last number.
$newPre = $version->bumpPreRelease('alpha', true);
echo $newPre->getExtraInfo()['version']; // "1.2.3-beta.1+build.123"