PHP Semver

Version
in package
uses NextProducer, Comparable, Copyable, Singles, Validator, Sortable

This class describes a semantic version and related operations following the semver 2.0.0 specification.

Instances of this class are immutable.

Table of Contents

Methods

__toString()  : string
compare()  : int
Compares two versions.
compareString()  : int
Compares two version strings.
copy()  : Version
Constructs a copy of the version. The copied object's properties can be altered with the optional parameters.
create()  : Version
Constructs a semantic version from the given arguments following the pattern: <[major]>.<[minor]>.<[patch]>-<[preRelease]>+<[buildMetadata]>.
equal()  : bool
Compares two version strings and returns true when the first and second are equal.
getBuildMeta()  : null|string
Returns the build metadata.
getMajor()  : int
Returns the major version number.
getMinor()  : int
Returns the minor version number.
getNextMajorVersion()  : Version
Produces the next major version.
getNextMinorVersion()  : Version
Produces the next minor version.
getNextPatchVersion()  : Version
Produces the next patch version.
getNextPreReleaseVersion()  : Version
Produces the next pre-release version.
getPatch()  : int
Returns the patch version number.
getPreRelease()  : null|string
Returns the pre-release tag.
greaterThan()  : bool
Compares two version strings and returns true when the first is greater than the second.
greaterThanOrEqual()  : bool
Compares two version strings and returns true when the first is greater than the second or equal.
inc()  : Version
Increases the version by its Inc::MAJOR, Inc::MINOR, Inc::PATCH, or Inc::PRE_RELEASE segment.
isEqual()  : bool
Compares the version with the given one, returns true when they are equal.
isGreaterThan()  : bool
Compares the version with the given one, returns true when the current is greater than the other.
isGreaterThanOrEqual()  : bool
Compares the version with the given one, returns true when the current is greater than the other or equal.
isLessThan()  : bool
Compares the version with the given one, returns true when the current is less than the other.
isLessThanOrEqual()  : bool
Compares the version with the given one, returns true when the current is less than the other or equal.
isNotEqual()  : bool
Compares the version with the given one, returns true when they are not equal.
isPreRelease()  : bool
Returns true when the version has a pre-release tag.
isSatisfying()  : bool
Determines whether the version satisfies the given Constraint or not.
isStable()  : bool
Determines whether the version is considered stable or not.
lessThan()  : bool
Compares two version strings and returns true when the first is less than the second.
lessThanOrEqual()  : bool
Compares two version strings and returns true when the first is less than the second or equal.
minVersion()  : Version
notEqual()  : bool
Compares two version strings and returns true when the first and second are not equal.
parse()  : Version
Parses the given string as a Version and returns the result or throws a SemverException if the string is not a valid representation of a semantic version.
parseOrNull()  : null|Version
Parses the given string as a Version and returns the result or null if the string is not a valid representation of a semantic version.
rsort()  : array<string|int, Version>
Sorts an array of versions in reverse order.
rsortString()  : array<string|int, string>
Sorts an array of version strings in reverse order.
satisfies()  : bool
Determines whether a Version satisfies a Constraint or not.
sort()  : array<string|int, Version>
Sorts an array of versions.
sortString()  : array<string|int, string>
Sorts an array of version strings.
withoutSuffixes()  : Version
Produces a copy of the Version without the PRE-RELEASE and BUILD METADATA identities.

Methods

__toString()

public __toString() : string
Return values
string

the string representation of the version

compare()

Compares two versions.

public static compare(Version $v1, Version $v2) : int
Parameters
$v1 : Version

the left side of the comparison

$v2 : Version

the right side of the comparison

Return values
int

-1 when $v1 < $v2, 0 when $v1 == $v2, 1 when $v1 > $v2

compareString()

Compares two version strings.

public static compareString(string $v1, string $v2) : int
Parameters
$v1 : string

the left side of the comparison

$v2 : string

the right side of the comparison

Tags
throws
SemverException

when the version strings are invalid

Return values
int

-1 when $v1 < $v2, 0 when $v1 == $v2, 1 when $v1 > $v2

copy()

Constructs a copy of the version. The copied object's properties can be altered with the optional parameters.

public copy([null|int $major = null ][, null|int $minor = null ][, null|int $patch = null ][, null|string $preRelease = null ][, null|string $buildMeta = null ]) : Version
Parameters
$major : null|int = null

the major version number

$minor : null|int = null

the minor version number

$patch : null|int = null

the patch version number

$preRelease : null|string = null

the pre-release part

$buildMeta : null|string = null

the build metadata

Tags
throws
SemverException

when the version parts are invalid

Return values
Version

the new version

create()

Constructs a semantic version from the given arguments following the pattern: <[major]>.<[minor]>.<[patch]>-<[preRelease]>+<[buildMetadata]>.

public static create(int $major, int $minor, int $patch[, null|string $preRelease = null ][, null|string $buildMeta = null ]) : Version
Parameters
$major : int

the major version number

$minor : int

the minor version number

$patch : int

the patch version number

$preRelease : null|string = null

the pre-release part

$buildMeta : null|string = null

the build metadata

Tags
throws
SemverException

when the version parts are invalid

Return values
Version

the new version

equal()

Compares two version strings and returns true when the first and second are equal.

public static equal(string $v1, string $v2) : bool
Parameters
$v1 : string

the left side of the comparison

$v2 : string

the right side of the comparison

Tags
throws
SemverException

when the version strings are invalid

Return values
bool

true when $v1 == $v2, otherwise false

getBuildMeta()

Returns the build metadata.

public getBuildMeta() : null|string
Return values
null|string

the build metadata part

getMajor()

Returns the major version number.

public getMajor() : int
Return values
int

the major version number

getMinor()

Returns the minor version number.

public getMinor() : int
Return values
int

the minor version number

getNextMajorVersion()

Produces the next major version.

public getNextMajorVersion([null|string $preRelease = null ]) : Version
Parameters
$preRelease : null|string = null

the pre-release part

Tags
throws
SemverException

when the pre-release tag is non-null and invalid

Return values
Version

the next major version

getNextMinorVersion()

Produces the next minor version.

public getNextMinorVersion([null|string $preRelease = null ]) : Version
Parameters
$preRelease : null|string = null

the pre-release part

Tags
throws
SemverException

when the pre-release tag is non-null and invalid

Return values
Version

the next minor version

getNextPatchVersion()

Produces the next patch version.

public getNextPatchVersion([null|string $preRelease = null ]) : Version
Parameters
$preRelease : null|string = null

the pre-release part

Tags
throws
SemverException

when the pre-release tag is non-null and invalid

Return values
Version

the next patch version

getNextPreReleaseVersion()

Produces the next pre-release version.

public getNextPreReleaseVersion([null|string $preRelease = null ]) : Version
Parameters
$preRelease : null|string = null

the pre-release part

Tags
throws
SemverException

when the pre-release tag is non-null and invalid

Return values
Version

the next pre-release version

getPatch()

Returns the patch version number.

public getPatch() : int
Return values
int

the patch version number

getPreRelease()

Returns the pre-release tag.

public getPreRelease() : null|string
Return values
null|string

the pre-release part

greaterThan()

Compares two version strings and returns true when the first is greater than the second.

public static greaterThan(string $v1, string $v2) : bool
Parameters
$v1 : string

the left side of the comparison

$v2 : string

the right side of the comparison

Tags
throws
SemverException

when the version strings are invalid

Return values
bool

true when $v1 > $v2, otherwise false

greaterThanOrEqual()

Compares two version strings and returns true when the first is greater than the second or equal.

public static greaterThanOrEqual(string $v1, string $v2) : bool
Parameters
$v1 : string

the left side of the comparison

$v2 : string

the right side of the comparison

Tags
throws
SemverException

when the version strings are invalid

Return values
bool

true when $v1 >= $v2, otherwise false

inc()

Increases the version by its Inc::MAJOR, Inc::MINOR, Inc::PATCH, or Inc::PRE_RELEASE segment.

public inc(int $by[, null|string $preRelease = null ]) : Version

Returns a new version while the original remains unchanged.

Parameters
$by : int

determines by which part the Version should be incremented

$preRelease : null|string = null

the optional pre-release part

Tags
throws
SemverException

when the pre-release tag is non-null and invalid

Return values
Version

the incremented version

isEqual()

Compares the version with the given one, returns true when they are equal.

public isEqual(Version $v) : bool
Parameters
$v : Version

the version to compare

Return values
bool

true when instance == $v, otherwise false

isGreaterThan()

Compares the version with the given one, returns true when the current is greater than the other.

public isGreaterThan(Version $v) : bool
Parameters
$v : Version

the version to compare

Return values
bool

true when instance > $v, otherwise false

isGreaterThanOrEqual()

Compares the version with the given one, returns true when the current is greater than the other or equal.

public isGreaterThanOrEqual(Version $v) : bool
Parameters
$v : Version

the version to compare

Return values
bool

true when instance >= $v, otherwise false

isLessThan()

Compares the version with the given one, returns true when the current is less than the other.

public isLessThan(Version $v) : bool
Parameters
$v : Version

the version to compare

Return values
bool

true when instance < $v, otherwise false

isLessThanOrEqual()

Compares the version with the given one, returns true when the current is less than the other or equal.

public isLessThanOrEqual(Version $v) : bool
Parameters
$v : Version

the version to compare

Return values
bool

true when instance <= $v, otherwise false

isNotEqual()

Compares the version with the given one, returns true when they are not equal.

public isNotEqual(Version $v) : bool
Parameters
$v : Version

the version to compare

Return values
bool

true when instance != $v, otherwise false

isPreRelease()

Returns true when the version has a pre-release tag.

public isPreRelease() : bool
Return values
bool

true when the version is a pre-release version

isSatisfying()

Determines whether the version satisfies the given Constraint or not.

public isSatisfying(Constraint $constraint) : bool
Parameters
$constraint : Constraint

the constraint to satisfy

Return values
bool

true when the constraint is satisfied, otherwise false

isStable()

Determines whether the version is considered stable or not.

public isStable() : bool

Stable versions have a positive major number and no pre-release identifier.

Return values
bool

true when the version is a stable version

lessThan()

Compares two version strings and returns true when the first is less than the second.

public static lessThan(string $v1, string $v2) : bool
Parameters
$v1 : string

the left side of the comparison

$v2 : string

the right side of the comparison

Tags
throws
SemverException

when the version strings are invalid

Return values
bool

true when $v1 < $v2, otherwise false

lessThanOrEqual()

Compares two version strings and returns true when the first is less than the second or equal.

public static lessThanOrEqual(string $v1, string $v2) : bool
Parameters
$v1 : string

the left side of the comparison

$v2 : string

the right side of the comparison

Tags
throws
SemverException

when the version strings are invalid

Return values
bool

true when $v1 <= $v2, otherwise false

notEqual()

Compares two version strings and returns true when the first and second are not equal.

public static notEqual(string $v1, string $v2) : bool
Parameters
$v1 : string

the left side of the comparison

$v2 : string

the right side of the comparison

Tags
throws
SemverException

when the version strings are invalid

Return values
bool

true when $v1 != $v2, otherwise false

parse()

Parses the given string as a Version and returns the result or throws a SemverException if the string is not a valid representation of a semantic version.

public static parse(string $versionString[, bool $strict = true ]) : Version

Strict mode is on by default, which means partial versions (e.g. '1.0' or '1') and versions with 'v' prefix are considered invalid. This behaviour can be turned off by setting the strict parameter to false.

Parameters
$versionString : string

the version string

$strict : bool = true

enables or disables strict parsing

Tags
throws
SemverException

when the given version string is invalid

Return values
Version

the parsed version

parseOrNull()

Parses the given string as a Version and returns the result or null if the string is not a valid representation of a semantic version.

public static parseOrNull(string $versionString[, bool $strict = true ]) : null|Version

Strict mode is on by default, which means partial versions (e.g. '1.0' or '1') and versions with 'v' prefix are considered invalid. This behaviour can be turned off by setting the strict parameter to false.

Parameters
$versionString : string

the version string

$strict : bool = true

enables or disables strict parsing

Return values
null|Version

the parsed version, or null if the parse fails

rsort()

Sorts an array of versions in reverse order.

public static rsort(array<string|int, Version$versions) : array<string|int, Version>
Parameters
$versions : array<string|int, Version>

the versions to sort

Return values
array<string|int, Version>

the sorted array of versions

rsortString()

Sorts an array of version strings in reverse order.

public static rsortString(array<string|int, string> $versions) : array<string|int, string>
Parameters
$versions : array<string|int, string>

the version strings to sort

Tags
throws
SemverException

when the given array contains an invalid version string

Return values
array<string|int, string>

the sorted array of version strings

satisfies()

Determines whether a Version satisfies a Constraint or not.

public static satisfies(string $versionString, string $constraintString) : bool
Parameters
$versionString : string

the version to check

$constraintString : string

the constraint to satisfy

Tags
throws
SemverException

when the version string or the constraint string is invalid

Return values
bool

true when the given version satisfies the given constraint, otherwise false

sort()

Sorts an array of versions.

public static sort(array<string|int, Version$versions) : array<string|int, Version>
Parameters
$versions : array<string|int, Version>

the versions to sort

Return values
array<string|int, Version>

the sorted array of versions

sortString()

Sorts an array of version strings.

public static sortString(array<string|int, string> $versions) : array<string|int, string>
Parameters
$versions : array<string|int, string>

the version strings to sort

Tags
throws
SemverException

when the given array contains an invalid version string

Return values
array<string|int, string>

the sorted array of version strings

withoutSuffixes()

Produces a copy of the Version without the PRE-RELEASE and BUILD METADATA identities.

public withoutSuffixes() : Version
Return values
Version

the new version


        
On this page

Search results