PharIo Version
repository·master·Indexed 27 days ago
https://github.com/phar-io/versionA 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.
What's inside phar-io/version
- 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.
Parse and check version compliance
masterUse
PharIo\Version\VersionConstraintParserto parse constraint strings andPharIo\Version\Versionto represent discrete versions. You can then use thecomplies()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' ) ); // falseCompare versions with pre-release labels
masterSince 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 likeisGreaterThan().$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); // trueUnderstand Version constraints and operators
masterVersion 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.0is 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.0is 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).- Caret operator (
Use GreaterThanOrEqualToVersionConstraint to validate minimum versions
masterTheGreaterThanOrEqualToVersionConstraintclass is used to check if a givenVersionobject meets or exceeds a specific minimum version requirement. It implements the logic for a 'greater than or equal to' comparison.