Giter Site home page Giter Site logo

phlak / semver Goto Github PK

View Code? Open in Web Editor NEW
171.0 8.0 15.0 160 KB

Semantic versioning helper library for PHP

Home Page: https://packagist.org/packages/phlak/semver

License: MIT License

PHP 98.11% Makefile 1.89%
semver php semantic-versioning versioning

semver's Introduction

SemVer

SemVer

Semantic versioning helper library • Created by Chris Kankiewicz (@PHLAK)

Join our Community Become a Sponsor One-time Donation
Latest Stable Version Total Downloads License GitHub branch checks state


Requirements

Installation

composer require phlak/semver

Initializing

use PHLAK\SemVer;

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

Or initialize with a custom version by passing a version string on creation. Accepts any valid semantic version string with or without a preceding v.

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

Or parse an incomple version string with the static Version::parse() constructor.

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

Usage

Retrieve the version or individual values

$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'

Increment the version

$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

Set (override) the version or individual values

$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

Clear pre-release / build values

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

Compare two SemVer objects

$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

Troubleshooting

For general help and support join our GitHub Discussions or reach out on Twitter.

Please report bugs to the GitHub Issue Tracker.

Copyright

This project is liscensed under the MIT License.

semver's People

Contributors

beleneglorion avatar chapeupreto avatar cloud-arrow avatar dependabot-preview[bot] avatar dependabot[bot] avatar jpickwell avatar oscar-ol avatar peter279k avatar phlak avatar samnela avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

semver's Issues

Express SemVer as number

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

echo $version;               // '1.2.3'
echo $version->toNumber();   // 100200300 (default 3 digits left padding with zero)
echo $version->toNumber(2);  // 102030 (2 digits left padding with zero)

Undefined offset

$version = new SemVer\Version()
PHP Notice:  Undefined offset: 4 in /home/.../vendor/phlak/semver/src/Version.php on line 77
PHP Notice:  Undefined offset: 5 in /home/.../vendor/phlak/semver/src/Version.php on line 78

Doesn't work when I try to parse a chrome browser version

Take the following code:

use PHLAK\SemVer;

$version = new SemVer\Version('89.0.4373.1');

echo $version.'<br>';             // '89.0.4373.1'
echo $version->major.'<br>';      // 89
echo $version->minor.'<br>';      // 0
echo $version->patch.'<br>';      // 4373
echo $version->preRelease;        // 1

Note: 89.0.4373.1 is current Chrome version I'm using.

Comes back saying this:

PHLAK\SemVer\Exceptions\InvalidVersionException: Invalid Semantic Version string provided in /vendor/phlak/semver/src/Version.php:113

Add Version::isPre(), Version::hasBuild()

Thanks for developing this lib!

It would be useful, if we implement functions like Version::isPre() and Version::hasBuild() to check if a version is a pre version or/and a version has a build part.

Comparing Versions with Pre-Release fail

The test

public function test_failed(): void
{
    $version = new SemVer\Version('v1.3.37+build.1');
    $this->assertTrue($version->eq(new SemVer\Version('v1.3.37')));
}

fails. According to https://semver.org/#spec-item-10 v1.3.37+build.1 and v1.3.37 have to be equal.

The reason is comparing the pre-release part null with '':
grafik

git integration

It would be nice if we could integrate somehow with git tags.

Add Version::min(), Version::max()

Maybe add Version::min(), Version::max() statics to get minimum possible version (v0.1.0? or v0.0.0?), maximum version (vMAX_INT.MAX_INT.MAX_INT?) respectively?

Versions compare bug

Hi,
Nice lib , but i found a little bug in version comparaison, if you compare 1.0.0 and 1.0.0-alpha the result say that 1.0.0-alpha is greater than 1.0.0 because of the <=> (alpha is greater than null when comparing pre-release parts)

It can check the pre-release tag

According to the TODO comment in gt and lt method inside Version class, I think we need to add the feature of pre-release tag check.

For example, we have to check the 1.0.0-alpha, 1.0.0-beta and 1.0.0-rc versions comparison.

@PHLAK, do you have any approaches/suggestions about implementing this feature?

Thanks.

SemVer parsing

$version = new \PHLAK\SemVer\Version('1.0');

This code throws an error:

Invalid Semantic Version string provided

I know that it's not valid for SemVer, but there are a lot of repositories which using SemVer, but their versions are not always has patch number.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.