Libraries / semver
Overview
A zero-dependency Semantic Versioning (SemVer 2.0.0) library for HolyPHP. Provides strict version parsing, precedence comparison, version bumping (major, minor, patch), and range matching (^, ~, >=, <=, >, <, =, and ||).
bundled — works offline
zero dependencies
version
range
Quick start
import "semver";
$v = semver_parse("1.2.3-beta.1+build.42");
println($v->major); // 1
println($v->minor); // 2
println($v->patch); // 3
println($v->prerelease); // beta.1
println($v->build); // build.42
$next = $v->bumpMinor();
println($next->toString()); // 1.3.0
println(semver_compare("1.0.0-alpha", "1.0.0")); // -1
println(semver_satisfies("1.4.2", "^1.2.0")); // true
println(semver_valid("2.0.0")); // true
Installation
In a project
Inside a project (a folder with hphp.yaml, see Projects & packages ):
hphp install semver
This adds the library to hphp.yaml, installs it into .hphp/packages/semver/ and pins the exact version in hphp.lock:
require:
semver: "^1.0"
Use it
import "semver";
semver is bundled with the compiler, so import "semver"; also works in a single file without installing anything, and hphp install works offline.
Outside a project
hphp install semver # for your user, shared by all scripts
hphp create myapp # or start a project, then: hphp install semver
Documentation
API Reference
class SemVer
Represents a parsed SemVer 2.0.0 version.
Properties
public int $major
public int $minor
public int $patch
public string $prerelease (empty string "" if none)
public string $build (empty string "" if none)
Constructor
new SemVer(int $major, int $minor, int $patch, string $prerelease = "", string $build = "") Creates a SemVer instance and validates all components.
Methods
toString(): string — Formats the version as MAJOR.MINOR.PATCH[-PRERELEASE][+BUILD].
compare(SemVer $other): int — Returns -1 if $this < $other, 0 if equal (ignoring build metadata per SemVer 2.0.0), or 1 if $this > $other.
equals(SemVer $other): bool — True if compare($other) == 0.
gt(SemVer $other): bool — True if $this > $other.
gte(SemVer $other): bool — True if $this >= $other.
lt(SemVer $other): bool — True if $this < $other.
lte(SemVer $other): bool — True if $this <= $other.
bumpMajor(): SemVer — Increments major, resets minor and patch to 0, clears prerelease and build.
bumpMinor(): SemVer — Increments minor, resets patch to 0, clears prerelease and build.
bumpPatch(): SemVer — Increments patch, clears prerelease and build.
bump(string $part): SemVer — Bumps "major", "minor", or "patch".
satisfies(string $range): bool — Checks if this version satisfies a range expression.
Functions
semver_parse(string $version): SemVer Parses a SemVer 2.0.0 string into a SemVer object. Throws Exception on invalid input.
semver_valid(string $version): bool Returns true if $version is a valid SemVer 2.0.0 string, false otherwise.
semver_compare(string $a, string $b): int Parses and compares two version strings. Returns -1, 0, or 1.
semver_satisfies(string $version, string $range): bool Returns true if $version satisfies $range.
Supported Range Syntax
Exact: 1.2.3 or =1.2.3
Comparisons: >1.0.0, >=1.0.0, <2.0.0, <=2.0.0
Caret (^): allows changes that do not modify the left-most non-zero digit (^1.2.3 => >=1.2.3 <2.0.0, ^0.2.3 => >=0.2.3 <0.3.0, ^0.0.3 => >=0.0.3 <0.0.4)
Tilde (~): allows patch-level changes (~1.2.3 => >=1.2.3 <1.3.0)
AND (space-separated): >=1.0.0 <2.0.0
OR (||): ^1.0.0 || ^2.0.0
Error Handling
All invalid inputs throw Exception with a clear, actionable message prefixed with semver::
semver: empty version string
semver: invalid version '1.2' (expected MAJOR.MINOR.PATCH)
semver: invalid major version '01' (leading zeros are not allowed)
semver: invalid prerelease '01' (numeric identifier '01' must not have leading zeros)
semver: invalid bump part 'foo' (expected 'major', 'minor', or 'patch')
semver: empty version range
semver: missing version in comparator '>='
Examples
01 Quick start
Bundled with the compiler — import "semver"; works offline in any file, or add it to a project with hphp install semver.
import "semver";
$v = semver_parse("1.2.3-beta.1+build.42");
println($v->major); // 1
println($v->minor); // 2
println($v->patch); // 3
println($v->prerelease); // beta.1
println($v->build); // build.42
$next = $v->bumpMinor();
println($next->toString()); // 1.3.0
println(semver_compare("1.0.0-alpha", "1.0.0")); // -1
println(semver_satisfies("1.4.2", "^1.2.0")); // true
println(semver_valid("2.0.0")); // true
Try it hphp install semver
hphp run main.hphp
Covered by the official test suite in tests/positive/lib_semver.hphp.