PHP Classes

PHP Semver Compare: Parse and compare software semantic version string

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 67 All time: 10,346 This week: 78Up
Version License PHP version Categories
semver 1.0.0Custom (specified...5PHP 5, Tools, Parsers
Description 

Author

This package can parse and compare software semantic version string.

It can take a string with a version number in semver format and parse it to extract the version and subversion numbers like the major version, minor version, patch level, release number, as well tell if it is a beta or a stable version.

The package can also compare two software version numbers and tell if they are the same version, older or more recent.

Innovation Award
PHP Programming Innovation award nominee
May 2018
Number 10
SemVer is a convention that is used to define the number of versions and revisions of software products.

This package can parse strings with software versions in the semver format, as well can compare two software versions and tell which is more recent.


Manuel Lemos
Picture of Mohamed Elbahja
  Performance   Level  
Name: Mohamed Elbahja is available for providing paid consulting. Contact Mohamed Elbahja .
Classes: 12 packages by
Country: Morocco Morocco
Age: 29
All time rank: 8991 in Morocco Morocco
Week rank: 44 Up1 in Morocco Morocco Equal
Innovation award
Innovation award
Nominee: 4x

Documentation

Semver

Simple PHP Semantic Versioning Parser and Comparator

Installation :

Using Composer:

composer require melbahja/semver ^1.0.0

Usage :

Simple Example:


require 'vendor/autoload.php';

use Melbahja\Semver\Semver;

$version = Semver::parse('1.3.0-beta+exp.sha.5114f85');

var_dump(
	$version->getMajor(), 	// int 1
	$version->getMinor(), 	// int 3
	$version->getPatch(), 	// int 0
	$version->getRelease(), // string beta
	$version->getMeta(),	// string exp.sha.5114f85
	$version->is('beta'), 	// true
	$version->is('stable') 	// false
);

// compare versions
var_dump(
	$version->compare('1.3.0-beta'), // true
	$version->compare('1.3.0', '<'), // true 1.3.0-beta is smaller than 1.3.0
	$version->compare('1.3.0-alpha') // false
);


Compare Examples:


var_dump(Semver::compare('1.2.3-alpha', '1.2.3-alpha.1', '<')); // true 1.2.3-alpha is smaller than 1.2.3-alpha.1

$version1 = Semver::parse('1.0.0-alpha.beta');

// $version1->getRelease(); // is alpha.beta

$version2 = Semver::parse('1.0.0-beta.2');

// $version2->getRelease(); // is beta.2

var_dump(Semver::compare($version1, $version2)); // false

var_dump(Semver::compare($version1, $version2, '<')); // true $version1 is smller than $version2

var_dump(Semver::compare($version1, $version2, '<=')); // true


$version3 = Semver::parse('2.2.0-alpha+exp.sha.5114f85');

$version4 = Semver::parse('2.2.0-alpha');

var_dump(Semver::compare($version3, $version4, '=')); // true

License :

MIT Copyright (c) 2018 Mohamed Elbahja


  Files folder image Files (10)  
File Role Description
Files folder imagesrc (3 files, 2 directories)
Files folder imagetests (1 file)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (10)  /  src  
File Role Description
Files folder imageExceptions (1 file)
Files folder imageInterfaces (1 file)
  Plain text file Semver.php Class Class source
  Plain text file Version.php Class Class source
  Plain text file VersionParser.php Class Class source

  Files folder image Files (10)  /  src  /  Exceptions  
File Role Description
  Plain text file SemverException.php Class Class source

  Files folder image Files (10)  /  src  /  Interfaces  
File Role Description
  Plain text file VersionInterface.php Class Class source

  Files folder image Files (10)  /  tests  
File Role Description
  Plain text file SemverTest.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:67
This week:0
All time:10,346
This week:78Up