The Version library provides a programmatic way of creating and manipulating semantic version numbers. Some methods will even automatically apply the logic defined by Semantic Versioning specification.
Herrera\Version\Builder::create()
To create a fresh version builder, call Builder::create()
:
$builder = Herrera\Version\Builder::create();
The default version is 0.0.0
.
Herrera\Version\Builder::importComponents(array $components)
If you have extracted the components of a version string representation using
the Herrera\Version\Parser::toComponents()
method, you can call the method,
importComponents()
:
$builder->importComponents($components);
Any version information missing from the given components array will be reset to their default values in the builder:
- Major Version:
0
- Minor Version:
0
- Patch Version:
0
- Pre-release Version: none
- Build Metadata: none
Herrera\Version\Builder::importString(string $version)
To simplify the parsing and building of versions, the importString()
method
exists for your convenience:
$builder->importString('1.0.0-alpha.1+2');
This is the equivalent of doing the following:
$builder->importComponents(
Herrera\Version\Parser::toComponents('1.0.0-alpha.1+2')
);
The caveat about missing version information applies.
Herrera\Version\Builder::importVersion(Herrera\Version\Version $version)
If you already have an instance of Herrera\Version\Version
, you can import
that directly to the builder:
$builder->importVersion($version);
Herrera\Version\Builder::setMajor(int $number)
Herrera\Version\Builder::setMinor(int $number)
Herrera\Version\Builder::setPatch(int $number)
Herrera\Version\Builder::setPreRelease(array $identifiers)
Herrera\Version\Builder::setBuild(array $identifiers)
You may use any of the above methods to directly alter the version information.
Note that you may only use integer values for setMajor()
, setMinor()
, and
setPatch()
. See the documentation for intval() to understand how string
numbers are converted to integers.
To automatically apply the logic defined in the specification, you may use:
Herrera\Version\Builder::incrementMajor(int $amount = 1)
— increments the major version number by$amount
, and resets the minor and patch version numbers to zeroHerrera\Version\Builder incrementMinor(int $amount = 1)
— increments the minor version number by$amount
, and resets the patch version number to zero
For the sake of completeness and convenience, the method
Herrera\Version\Builder::incrementPatch(int $amount = 1)
has been added. It will simply increment the patch version number by the
$amount
specified.
A couple of convenience methods are available for resetting the pre-release version identifiers and build metadata identifiers:
$builder->clearBuild();
// same as
$builder->setBuild(array());
$builder->clearPreRelease();
// same as
$builder->setPreRelease(array());
Once you have built your final version number, you may retrieve an instance
of the Herrera\Version\Version
class. This class maintains a read-only copy
of the version information.
$version = $builder->getVersion();