PharIo Version

repository·master·Indexed 27 days ago

https://github.com/phar-io/version

A PHP library for handling and comparing semantic versions and version constraints. It supports standard mathematical operators, caret (^) and tilde (~) operators, and pre-release labels. Key components include VersionConstraintParser for parsing constraints and the Version class for representing discrete versions and checking compliance.

Tokens
795
Snippets
2
Records
5
Agent score
43%

What's inside phar-io/version

  1. Install phar-io/version via Composer

    master
    Add this library to your project using Composer. You can install it as a standard project dependency or as a development-only dependency if it is only needed for tasks like running test suites.
  2. Parse and check version compliance

    master

    Use PharIo\Version\VersionConstraintParser to parse constraint strings and PharIo\Version\Version to represent discrete versions. You can then use the complies() method on the parsed constraint to check if a specific version satisfies the requirement.

    use PharIo\Version\Version;
    use PharIo\Version\VersionConstraintParser;
    
    $parser = new VersionConstraintParser();
    
    // Caret constraint example
    $caret_constraint = $parser->parse( '^7.0' );
    $caret_constraint->complies( new Version( '7.0.17' ) ); // true
    $caret_constraint->complies( new Version( '7.1.0' ) ); // true
    $caret_constraint->complies( new Version( '6.4.34' ) ); // false
    
    // Tilde constraint example
    $tilde_constraint = $parser->parse( '~1.1.0' );
    $tilde_constraint->complies( new Version( '1.1.4' ) ); // true
    $tilde_constraint->complies( new Version( '1.2.0' ) ); // false
  3. Compare versions with pre-release labels

    master

    Since version 2.0.0, the library supports pre-release labels (e.g., 3.0.0-alpha.1). You can compare these versions using comparison methods like isGreaterThan().

    $leftVersion = new PharIo\Version\Version('3.0.0-alpha.1');
    $rightVersion = new PharIo\Version\Version('3.0.0-alpha.2');
    
    $leftVersion->isGreaterThan($rightVersion); // false
    $rightVersion->isGreaterThan($leftVersion); // true
  4. Understand Version constraints and operators

    master

    Version constraints describe a range of versions or a discrete version number following semantic versioning (<major>.<minor>.<patch>). The library supports standard mathematical operators (e.g., <=, >=) as well as two special operators:

    • Caret operator (^): Specifies a range within a major version. For example, ^1.0 is equivalent to >=1.0.0 <2.0.0 (every version within major version 1).
    • Tilde operator (~): Specifies a range within a minor version. For example, ~1.1.0 is equivalent to >=1.1.0 <1.2.0.

    Note: If no patch level is provided to the tilde operator (e.g., ~1.0), it behaves identically to the caret operator (^1.0).