PHP code example of kherge / semver

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

    

kherge / semver example snippets


use function KHerGe\Version\parse;

// Create a new value object.
$version = parse('1.2.3-alpha.1+20161004');

// Bump the patch number: 1.0.1
$patched = $version->incrementPatch();

// The original value object is unchanged.
echo $version; // 1.2.3-alpha.1+20161004

// But the patched version number has the change.
echo $patched; // 1.2.4

if ($patched->isGreaterThan($version)) {
    // $patched is greater than $version
}

use function KHerGe\Version\is_valid;

$version = '1.2.3-alpha.1+20161004';

if (is_valid($version)) {
    // $version is valid
}

$version = new Version(

    // major
    1,

    // minor
    2,

    // patch
    3,

    // pre-release
    ['a', 'b', 'c'],

    // build
    ['x', 'y', 'z']

);

use function KHerGe\Version\parse_components;

$components = parse_components('1.2.3-alpha.1+20161004');

$components = [
    'major' => 1,
    'minor' => 2,
    'patch' => 3,
    'pre-release' => ['alpha', '1'],
    'build' => ['20161004']
];

use KHerGe\Version\Compare\Constraint\AndX;
use KHerGe\Version\Compare\Constraint\EqualTo;
use KHerGe\Version\Compare\Constraint\GreaterThan;
use KHerGe\Version\Compare\Constraint\LessThan;
use KHerGe\Version\Compare\Constraint\OrX;

use function KHerGe\Version\parse;

// Match any of the following constraints.
$constraint = new OrX(
    [
        // Match all of the following constraints.
        new AndX(
            [
                // Must be greater than "0.2.3".
                new GreaterThan(parse('0.2.3')),

                // Must be less than "0.4.4".
                new LessThan(parse('0.4.4')),
            ]
        ),

        // Match exactly "0.4.5".
        new EqualTo(parse('0.4.5'))
    ]
);

// Verify that the version meets the constraints.
$version = parse('0.4.0');

if ($constraint->allows($version)) {
    // $version is allowed
}