PHP code example of mistralys / version-parser

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

    

mistralys / version-parser example snippets


use Mistralys\VersionParser\VersionParser;

$version = VersionParser::create('1.5.2');

$major = $version->getMajorVersion(); // 1
$minor = $version->getMinorVersion(); // 5
$patch = $version->getPatchVersion(); // 2

use Mistralys\VersionParser\VersionParser;

$version = VersionParser::create('1.5.2-RC3');

$number = $version->getVersion(); // 1.5.2

use Mistralys\VersionParser\VersionParser;

$version = VersionParser::create('1');

$number = $version->getVersion(); // 1.0.0

use Mistralys\VersionParser\VersionParser;

$version = VersionParser::create('1.0.0');

$number = $version->getVersion(); // 1

use Mistralys\VersionParser\VersionParser;

$version = VersionParser::create("1.2.0 'Cool Release' RC5");

$normalized = $version->getTagVersion(); // 1.0.0-CoolRelease-rc5

use Mistralys\VersionParser\VersionParser;

$version = VersionParser::create('1.5.2-beta');

$isBeta = $version->isBeta(); // true

use Mistralys\VersionParser\VersionParser;

$version = VersionParser::create('1.5.2-beta5');

if($version->getTagType() === VersionParser::TAG_TYPE_BETA)
{
	// is a beta version
}

use Mistralys\VersionParser\VersionParser;

$tag = VersionParser::create('1.5.2-B2')->getTagInfo();

if($tag !== null)
{
    echo $tag->getTagName(); // b
    echo $tag->getTagType(); // beta
}

use Mistralys\VersionParser\VersionParser;

$version = VersionParser::create('1.5.2-beta');

$betaVersion = $version->getTagNumber(); // 1 (implicit)

use Mistralys\VersionParser\VersionParser;

$version = VersionParser::create('1.5.2-beta5');

$betaVersion = $version->getTagNumber(); // 5

use Mistralys\VersionParser\VersionParser;

$version = VersionParser::create('1.5.2-Foobar');

$hasBranch = $version->hasBranch(); // true
$branchName = $version->getBranchName(); // Foobar

use Mistralys\VersionParser\VersionParser;

$version = VersionParser::create('1.5.2-Foobar-RC1');

$hasBranch = $version->hasBranch(); // true
$branchName = $version->getBranchName(); // Foobar

use Mistralys\VersionParser\VersionParser;

$version = VersionParser::create('1.5.2 "Foobar/42"');

$hasBranch = $version->hasBranch(); // true
$branchName = $version->getBranchName(); // Foobar/42

use Mistralys\VersionParser\VersionParser;

$version = VersionParser::create('1.5.2-BranchName-alpha5');

echo $version
    ->setSeparatorChar('_')
    ->getTagVersion();

use Mistralys\VersionParser\VersionParser;

$version = VersionParser::create('1.5.2-BranchName-alpha5');

echo $version
    ->setTagUppercase()
    ->getTagVersion();

use Mistralys\VersionParser\VersionParser;

// The third parameter is the short variant of the tag type.
VersionParser::registerTagType('foobar', 5, 'f');

$version = VersionParser::create('1.0.5-foobar2');
$short = VersionParser::create('1.0.5-F2');

echo $version->getTagType(); // foobar
echo $short->getTagType(); // foobar

use Mistralys\VersionParser\VersionParser;

$weight = 900; // Can be any number
VersionParser::registerTagType(VersionParser::TAG_TYPE_ALPHA, $weight--);
VersionParser::registerTagType(VersionParser::TAG_TYPE_BETA, $weight--);
VersionParser::registerTagType('foobar', $weight--);
VersionParser::registerTagType(VersionParser::TAG_TYPE_RELEASE_CANDIDATE, $weight--);
VersionParser::registerTagType('prefinal', $weight--);

use \Mistralys\VersionParser\VersionParser;

$version = VersionParser::create('1.0-alpha2');

$float = $version->getBuildNumber();
$int = $version->getBuildNumberInt();

use \Mistralys\VersionParser\VersionParser;

$versions = array(
    VersionParser::create('1.1'),
    VersionParser::create('2'),
    VersionParser::create('1.5.9'),
    VersionParser::create('1.5.9-beta'),
    VersionParser::create('2.0.0-alpha')
);

usort($versions, static function (VersionParser $a, VersionParser $b) : int {
    return $a->getBuildNumberInt() - $b->getBuildNumberInt();
});