requtize/semver-converter

Converts SemVer version (like Composer packages) into integer version with operators. Helps managing versions: store, compare, sort and retrive by conditions.

0.3.1 2018-01-30 20:26 UTC

README

Converts SemVer version (like Composer packages) into integer version with operators. Helps managing versions: store, compare, sort and retrive by conditions.

Converter accepts the same versions as Composer, so You can use it with any package manager that accepts Semantic Versioning.

How it works?

For each version it uses Composer SemVer parser to parse version and normalize it. And then, for each version, converter creates array exploded by dot values. Each value pads with zeros and create from it long string which is converted to integer. And for each version we have big integer.

Sample:

  1. Version 1.0.5
  2. Normalize with SemVer: 1.0.5
  3. Explode: [ 1, 0, 5 ]
  4. Converts to strings and pad zeros: [ '100', '000', '500' ]
  5. Concatenate all strings: '100' + '000' + '500'
  6. Converts to integer: (int) '100000500'

Result: '1.0.5' == 100000500

Examples

Simple version

$result = (new SemVerConverter)->convert('0.1.0');

// Result
array (size=1)
  0 => 
    array (size=2)
      'from' => 
        array (size=2)
          0 => int 100000000
          1 => string '==' (length=2)
      'to' => 
        array (size=2)
          0 => int 100000000
          1 => string '==' (length=2)

Version between

$result = (new SemVerConverter)->convert('>= 1.3.0 < 1.7.0');

// Result
array (size=1)
  0 => 
    array (size=2)
      'from' => 
        array (size=2)
          0 => int 100300000
          1 => string '>=' (length=2)
      'to' => 
        array (size=2)
          0 => int 100700000
          1 => string '<' (length=1)

Tilde operator

$result = (new SemVerConverter)->convert('~1.3');

// Result
array (size=1)
  0 => 
    array (size=2)
      'from' => 
        array (size=2)
          0 => int 100300000
          1 => string '>=' (length=2)
      'to' => 
        array (size=2)
          0 => int 200000000
          1 => string '<' (length=1)

Version or

$result = (new SemVerConverter)->convert('^1.9 || 3.0.*');

// Result
array (size=2)
  0 => 
    array (size=2)
      'from' => 
        array (size=2)
          0 => int 100900000
          1 => string '>=' (length=2)
      'to' => 
        array (size=2)
          0 => int 200000000
          1 => string '<' (length=1)
  1 => 
    array (size=2)
      'from' => 
        array (size=2)
          0 => int 300000000
          1 => string '>=' (length=2)
      'to' => 
        array (size=2)
          0 => int 300100000
          1 => string '<' (length=1)

Settings

new SemVerConverter($zeros, $sections);

$zeros

Defines how meny zeros need to pad for each section of versions. It allows to define how long should be result.

$sections

Defines how many sections need to be generated from input. Default is 3, Composer SemVer generates 4. This also have an impact for result.

Licence

This code is licensed under MIT License.