Download the PHP package gregpriday/php-version without Composer
On this page you can find all versions of the php package gregpriday/php-version. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download gregpriday/php-version
More information about gregpriday/php-version
Files in gregpriday/php-version
Package php-version
Short Description A simple, powerful PHP class for parsing, validating, and comparing semantic version strings.
License MIT
Informations about the package php-version
PHP Version
A simple yet powerful library for parsing, validating, comparing, and manipulating semantic version strings in PHP. It also includes flexible support for version constraints (e.g., ^1.2.3
, >=1.0.0 <2.0.0
) to check whether a particular version satisfies one or more complex conditions.
Installation
Overview
This library offers:
- Strict or Loose Parsing of version strings (e.g.
"1.2.3"
,"v1.2.3"
,"1.2"
,"1"
). - Version Object to access and modify version components (major, minor, patch, pre-release, and build metadata).
- SemVer Checks to see if a version is stable or a pre-release.
- Version Bumping/Lowering (increment/decrement major, minor, patch) including preserving or clearing pre-release/build metadata.
- Constraint Parsing and Evaluation using a fluent, chainable syntax with logical AND and OR conditions.
Basic Usage Example
Below is a quick snapshot of how you might use the library to parse a version, check its properties, bump the version, and validate it against constraints.
Creating and Inspecting Versions
Strict vs. Loose Parsing
- Strict mode (default): Expects full
major.minor.patch
(optionally-preRelease
and/or+buildMetadata
). Examples of valid strict versions:1.0.0
0.9.5-alpha+build.1
- Loose mode: More lenient. Accepts shorter forms and can include a leading
v
. Examples of acceptable loose versions:v1.2.3
1.2
(interpreted as1.2.0
)1
(interpreted as1.0.0
)
Accessing Components
Stability and Pre-Release Checks
Bumping and Lowering Version Numbers
The VersionBumpingTrait
gives you methods to increment or decrement specific parts of a version. Each method returns a new Version
instance. By default, these operations clear pre-release and build metadata, but you can preserve them if you wish.
Bumping (Incrementing)
Lowering (Decrementing)
You can similarly reduce major, minor, or patch. By default, these also clear pre-release/build metadata, unless you preserve them.
Note: Lowering a
0
major/minor/patch throws an exception, since negative version segments are invalid.
Parsing and Evaluating Constraints
The library can parse powerful OR and AND constraints using the VersionConstraintParser
. This enables checks such as >=1.0.0 <2.0.0 || ^3.0.0
.
Operators
- Basic:
>
,>=
,<
,<=
,=
,==
,!
,!=
- Caret
^
: e.g.^1.2.3
means>=1.2.3
and<2.0.0
(for 1.x versions) - Tilde
~
: e.g.~1.2.3
means>=1.2.3
and<1.3.0
Combining Constraints
- AND: Use space or commas. For example:
>=1.0.0 <2.0.0
>=1.0.0, <2.0.0
This means the version must satisfy both constraints. - OR: Split with
||
. For example:
^1.0.0 || ^2.0.0
Means the version must satisfy either^1.0.0
or^2.0.0
.
Example: Parsing Constraints
Testing
To run the test suite, clone this repository (or have it locally) and install dev dependencies:
Then run:
This runs the PHPUnit tests under tests/
.
Code Formatting
A Laravel Pint configuration is included for formatting. To format the code:
License
This project is open source under the MIT license.