stevegrunwell/semver-parser

Library for parsing and manipulating Semantic Versioning ("SemVer") relases

dev-develop 2024-08-18 16:14 UTC

This package is auto-updated.

Last update: 2024-10-18 16:39:40 UTC


README

This library defines the SteveGrunwell\SemVer\Version class, which is meant to parse and manipulate version numbers based on the rules of Semantic Versioning (a.k.a. "SemVer").

Installation

Install the library using Composer:

$ composer require stevegrunwell/semver-parser

Please note that while this library should be compatible with PHP 7.4 and newer, the unit tests are only run in CI against actively-supported versions of PHP.

Usage

The constructor of the Version class can accept a valid, semantic version based on the Semantic Versioning 2.0.0 specification:

use SteveGrunwell\SemVer\Parser;

// Import the Composer-generated autoloader.
require_once __DIR__ . '/vendor/autoload.php';

$version = new Version('1.2.3-rc1+local');

// Parse the version.
$version->getMajorVersion();      // 1
$version->getMinorVersion();      // 2
$version->getPatchVersion();      // 3
$version->getPreReleaseVersion(); // rc1
$version->getBuildMetadata():     // local

// Modify the version.
$version->setMajorVersion(4);
$version->setMinorVersion(5);
$version->setPatchVersion(6);
$version->setPreReleaseVersion('rc2');
$version->setBuildMetadata('github-actions.ubuntu2404');

// Retrieve the updated version as a string.
$version->getVersion(); // "4.5.6-rc2+github-actions.ubuntu2404"
(string) $version;      // "4.5.6-rc2+github-actions.ubuntu2404"

Additional methods

In addition to the setters and getters described above, each of the major, minor, and patch values have corresponding increment and decrement methods:

// Increment values.
$version->incrementMajorVersion();
$version->incrementMinorVersion();
$version->incrementPatchVersion();

// Decrement values.
$version->decrementMajorVersion();
$version->decrementMinorVersion();
$version->decrementPatchVersion();

It's worth noting that incrementMajorVersion() and incrementMinorVersion() will reset the the minor/patch and patch numbers (respectively) according to the Semantic Versioning 2.0.0 specification.

License

This library is released under the MIT License. Please see LICENSE.md for more details.