PHP code example of naneau / semver

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

    

naneau / semver example snippets




use Naneau\SemVer\Parser;

// Parse a SemVer string
$version = Parser::parse('1.2.3-alpha.1+build.12345.ea4f51');

// Root parts
echo $version->getMajor(); // => 1
echo $version->getMinor(); // => 2
echo $version->getPatch(); // => 3

// Pre-release part ('-alpha.1')
if ($version->hasPreRelease()) {
    echo $version->getPreRelease()->getGreek(); // => alpha
    echo $version->getPreRelease()->getReleaseNumber(); // => 1
    echo $version->getPreRelease() // => alpha.1
}

// Build part ('+build.12345')
if ($version->hasBuild()) {
    echo $version->getBuild()->getNumber(); // => 12345
    var_dump($version->getBuild()->getParts()); // => array(0 => 'ea4f51');
}

// Full version echo
echo $version; // => 1.2.3-alpha.1+build.12345.ea4f51



use Naneau\SemVer\Parser;
use Naneau\SemVer\Compare;

Compare::greaterThan(
    Parser::parse('1.2.1-beta'),
    Parser::parse('1.2.1-alpha.1')
); // ==> true

Compare::equals(
    Parser::parse('1.2.1-beta'),
    Parser::parse('1.2.1-alpha.1')
); // ==> false

Compare::smallerThan(
    Parser::parse('1.2.0'),
    Parser::parse('1.2.1')
); // ==> true



use Naneau\SemVer\Sort;

$sorted = Sort::sort('1.2.1-beta', '1.2.0+build.10', '0.9.29');

echo $sorted[0]; // => 0.9.29
echo $sorted[1]; // => 1.2.0+build.10
echo $sorted[2]; // => 1.2.1-beta