README.md
100 lines
| 1 | composer/semver |
| 2 | =============== |
| 3 | |
| 4 | Semver (Semantic Versioning) library that offers utilities, version constraint parsing and validation. |
| 5 | |
| 6 | Originally written as part of [](https://github.com/composer/composercomposer/composer](https://github.com/composer/composer](https://github.com/composer/composer), |
| 7 | now extracted and made available as a stand-alone library. |
| 8 | |
| 9 | [](https://github.com/composer/semver/actions/workflows/continuous-integration.yml](https://github.com/composer/semver/actions/workflows/continuous-integration.yml](https://github.com/composer/semver/actions/workflows/continuous-integration.yml) |
| 10 | [](https://github.com/composer/semver/actions/workflows/lint.yml](https://github.com/composer/semver/actions/workflows/lint.yml](https://github.com/composer/semver/actions/workflows/lint.yml) |
| 11 | [](https://github.com/composer/semver/actions/workflows/phpstan.yml](https://github.com/composer/semver/actions/workflows/phpstan.yml](https://github.com/composer/semver/actions/workflows/phpstan.yml) |
| 12 | |
| 13 | Installation |
| 14 | ------------ |
| 15 | |
| 16 | Install the latest version with: |
| 17 | |
| 18 | ```bash |
| 19 | composer require composer/semver |
| 20 | ``` |
| 21 | |
| 22 | |
| 23 | Requirements |
| 24 | ------------ |
| 25 | |
| 26 | * PHP 5.3.2 is required but using the latest version of PHP is highly recommended. |
| 27 | |
| 28 | |
| 29 | Version Comparison |
| 30 | ------------------ |
| 31 | |
| 32 | For details on how versions are compared, refer to the [](https://getcomposer.org/doc/articles/versions.mdVersions](https://getcomposer.org/doc/articles/versions.md](https://getcomposer.org/doc/articles/versions.md) |
| 33 | article in the documentation section of the [](https://getcomposer.orggetcomposer.org](https://getcomposer.org](https://getcomposer.org) website. |
| 34 | |
| 35 | |
| 36 | Basic usage |
| 37 | ----------- |
| 38 | |
| 39 | ### Comparator |
| 40 | |
| 41 | The [](https://github.com/composer/semver/blob/main/src/Comparator.php`Composer\Semver\Comparator`](https://github.com/composer/semver/blob/main/src/Comparator.php](https://github.com/composer/semver/blob/main/src/Comparator.php) class provides the following methods for comparing versions: |
| 42 | |
| 43 | * greaterThan($v1, $v2) |
| 44 | * greaterThanOrEqualTo($v1, $v2) |
| 45 | * lessThan($v1, $v2) |
| 46 | * lessThanOrEqualTo($v1, $v2) |
| 47 | * equalTo($v1, $v2) |
| 48 | * notEqualTo($v1, $v2) |
| 49 | |
| 50 | Each function takes two version strings as arguments and returns a boolean. For example: |
| 51 | |
| 52 | ```php |
| 53 | use Composer\Semver\Comparator; |
| 54 | |
| 55 | Comparator::greaterThan('1.25.0', '1.24.0'); // 1.25.0 > 1.24.0 |
| 56 | ``` |
| 57 | |
| 58 | ### Semver |
| 59 | |
| 60 | The [`Composer\Semver\Semver`](https://github.com/composer/semver/blob/main/src/Semver.php) class provides the following methods: |
| 61 | |
| 62 | * satisfies($version, $constraints) |
| 63 | * satisfiedBy(array $versions, $constraint) |
| 64 | * sort($versions) |
| 65 | * rsort($versions) |
| 66 | |
| 67 | ### Intervals |
| 68 | |
| 69 | The [`Composer\Semver\Intervals`](https://github.com/composer/semver/blob/main/src/Intervals.php) static class provides |
| 70 | a few utilities to work with complex constraints or read version intervals from a constraint: |
| 71 | |
| 72 | ```php |
| 73 | use Composer\Semver\Intervals; |
| 74 | |
| 75 | // Checks whether $candidate is a subset of $constraint |
| 76 | Intervals::isSubsetOf(ConstraintInterface $candidate, ConstraintInterface $constraint); |
| 77 | |
| 78 | // Checks whether $a and $b have any intersection, equivalent to $a->matches($b) |
| 79 | Intervals::haveIntersections(ConstraintInterface $a, ConstraintInterface $b); |
| 80 | |
| 81 | // Optimizes a complex multi constraint by merging all intervals down to the smallest |
| 82 | // possible multi constraint. The drawbacks are this is not very fast, and the resulting |
| 83 | // multi constraint will have no human readable prettyConstraint configured on it |
| 84 | Intervals::compactConstraint(ConstraintInterface $constraint); |
| 85 | |
| 86 | // Creates an array of numeric intervals and branch constraints representing a given constraint |
| 87 | Intervals::get(ConstraintInterface $constraint); |
| 88 | |
| 89 | // Clears the memoization cache when you are done processing constraints |
| 90 | Intervals::clear() |
| 91 | ``` |
| 92 | |
| 93 | See the class docblocks for more details. |
| 94 | |
| 95 | |
| 96 | License |
| 97 | ------- |
| 98 | |
| 99 | composer/semver is licensed under the MIT License, see the LICENSE file for details. |
| 100 |