PHP code example of phlak / semver

1. Go to this page and download the library: Download phlak/semver 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/ */

    

phlak / semver example snippets


use PHLAK\SemVer;

$version = new SemVer\Version(); // Initilializes to '0.1.0'

$version = new SemVer\Version('v1.2.3-alpha.5+sha.8d31ff4');

$version = SemVer\Version::parse('v1')   // Initializes to '1.0.0'
$version = SemVer\Version::parse('v1.2') // Initializes to '1.2.0'

$version = new SemVer\Version('v1.2.3-beta.4+007');

echo $version;             // '1.2.3-beta.4+007'
echo $version->major;      // 1
echo $version->minor;      // 2
echo $version->patch;      // 3
echo $version->preRelease; // 'beta.4'
echo $version->build;      // '007'

$version = new SemVer\Version('v1.2.3');

$version->incrementMajor();      // v1.2.3 -> v2.0.0
$version->incrementMinor();      // v1.2.3 -> v1.3.0
$version->incrementPatch();      // v1.2.3 -> v1.2.4
$version->incrementPreRelease(); // v1.2.3-alpha.5 -> v1.2.3-alpha.6

$version = new SemVer\Version();

$version->setVersion('v1.2.3');  // v1.2.3
$version->setMajor(3);           // v1.2.3 -> v3.0.0
$version->setMinor(5);           // v1.2.3 -> v1.5.0
$version->setPatch(7);           // v1.2.3 -> 1.2.7
$version->setPreRelease('rc.2'); // v1.2.3 -> v1.2.3-rc.2
$version->setBuild('007');       // v1.2.3 -> v1.2.3+007

$version->setPreRelease(null); // v1.2.3-rc.2 -> v1.2.3
$version->setBuild(null);      // v1.2.3+007 -> v1.2.3

$version->isPreRelease();
$version->hasBuild();

$version1 = new SemVer\Version('v1.2.3');
$version2 = new SemVer\Version('v3.2.1');

$version1->gt($version2);  // false
$version1->lt($version2);  // true
$version1->eq($version2);  // false
$version1->neq($version2); // true
$version1->gte($version2); // false
$version1->lte($version2); // true

$version1 = new SemVer\Version('v1.2.3-alpha.4');
$version2 = new SemVer\Version('v1.3.4-alpha.5');

$version1->gt($version2, Compare::MAJOR);  // false
$version1->lt($version2, Compare::MAJOR);  // false
$version1->eq($version2, Compare::MAJOR);  // true
$version1->neq($version2, Compare::MAJOR); // false
$version1->gte($version2, Compare::MAJOR); // true
$version1->lte($version2, Compare::MAJOR); // true

$version1 = new SemVer\Version('v1.2.3-alpha.4');
$version2 = new SemVer\Version('v1.2.4-alpha.5');

$version1->gt($version2, Compare::MINOR);  // false
$version1->lt($version2, Compare::MINOR);  // false
$version1->eq($version2, Compare::MINOR);  // true
$version1->neq($version2, Compare::MINOR); // false
$version1->gte($version2, Compare::MINOR); // true
$version1->lte($version2, Compare::MINOR); // true

$version1 = new SemVer\Version('v1.2.3-alpha.4');
$version2 = new SemVer\Version('v1.2.3-alpha.5');

$version1->gt($version2, Compare::PATCH);  // false
$version1->lt($version2, Compare::PATCH);  // false
$version1->eq($version2, Compare::PATCH);  // true
$version1->neq($version2, Compare::PATCH); // false
$version1->gte($version2, Compare::PATCH); // true
$version1->lte($version2, Compare::PATCH); // true