PHP Luminova: Compare Version Numbers and Constraints
Compare numeric versions and check whether a version satisfies one or more constraints.
Use Version for numeric versions with up to three segments. Missing segments are compared as zero, and the current version accepts a lowercase v prefix. Separate combined constraints with spaces and keep each operator next to its version. Both ^ and ~ use the same boundaries in this utility. For example, ^3.8 allows versions from 3.8.0 up to but excluding 3.9.0, while ^3 extends to the next major version.
Usage
use Luminova\Utility\Version;
var_dump(Version::satisfies('v3.8.2', '>=3.8 <4')); // bool(true)
var_dump(Version::match('3.9.0', '^3.8')); // bool(false)
var_dump(Version::compare('3.8', '=', '3.8.0')); // bool(true)Class Definition
- Full namespace:
Luminova\Utility\Version - This class is final.
Methods
satisfies
Evaluate whitespace-separated constraints as an AND expression. All constraints must pass. The method stops at the first failed comparison.
public static function satisfies(string $current, string $constraint): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$current | string | Current numeric version. |
$constraint | string | One or more constraints, such as >=3.8 <4. |
Return:
bool - true if every comparison passes.
Throws:
- Luminova\Exceptions\InvalidArgumentException if the current version is invalid, the constraint is empty, or an evaluated constraint fails syntax validation.
compare
Compare two versions with one of >=, <=, >, <, !=, <>, =, or ==. Unsupported operators yield false.
public static function compare(string $current, string $operator, string $target): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$current | string | Current numeric version. |
$operator | string | Comparison operator. |
$target | string | Target numeric version without a v prefix. |
Return:
bool - true if the comparison passes.
Throws:
- Luminova\Exceptions\InvalidArgumentException if version validation fails.
match
Evaluate one constraint. Interior whitespace is rejected, so write >=3.8 as one token.
public static function match(string $current, string $constraint): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$current | string | Current numeric version. |
$constraint | string | A single comparison, range, or exact version. |
Return:
bool - true if the version matches.
Throws:
- Luminova\Exceptions\InvalidArgumentException if the current version is invalid, the constraint is empty or contains whitespace, or constraint validation fails.