Skip to content

Commit 25d9721

Browse files
committed
Add copy function, fix pre-release regexes
1 parent 9b0dba8 commit 25d9721

File tree

5 files changed

+147
-57
lines changed

5 files changed

+147
-57
lines changed

README.md

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,26 @@ Semantic version utility library with parser and comparator written in PHP. It p
1010
## Requirements
1111
[PHP](https://www.php.net/) >= 5.5
1212

13-
## Installation with [Composer](https://getcomposer.org/)
13+
## Install with [Composer](https://getcomposer.org/)
1414
```shell
1515
composer require z4kn4fein/php-semver
1616
```
1717

1818
## Usage
19-
#### Parsing and available properties
19+
The following options are supported to construct a `Version`:
20+
1. Building part by part with `Version.create()`.
21+
22+
```php
23+
Version::create(3, 5, 2, "alpha", "build")
24+
```
25+
26+
2. Parsing from a string with `Version.parse()`.
27+
28+
```php
29+
Version::parse("3.5.2-alpha+build")
30+
```
31+
32+
The following information is accessible on a constructed `Version` object:
2033
```php
2134
<?php
2235

@@ -27,11 +40,12 @@ $version = Version::parse('2.5.6-alpha.12+build.34');
2740
echo $version->getMajor(); // 2
2841
echo $version->getMinor(); // 5
2942
echo $version->getPatch(); // 6
30-
echo (string)$version->getPreRelease(); // alpha.12
43+
echo $version->getPreRelease(); // alpha.12
3144
echo $version->getBuildMeta(); // build.34
3245
echo (string)$version; // 2.5.6-alpha.12+build.34
3346
```
34-
#### Comparing two versions
47+
### Comparing two versions
48+
It is possible to compare two `Version` objects with the following comparison methods.
3549
```php
3650
<?php
3751

@@ -53,7 +67,9 @@ echo $version->isGreaterThan('2.5.6'); // false
5367
echo $version->isLessThanOrEqual('2.5.6-alpha.12'); // true
5468
echo $version->isEqual('2.5.6-alpha.12+build.56'); // true
5569
```
56-
#### Producing incremented versions
70+
### Increment
71+
`Version` objects can produce incremented versions of themselves with the `getNext{Major|Minor|Patch|PreRelease}Version` methods.
72+
These methods can be used to determine the next version in order incremented by the according part.
5773
```php
5874
<?php
5975

@@ -65,6 +81,29 @@ echo (string)$version->getNextMajorVersion(); // 3.0.0
6581
echo (string)$version->getNextMinorVersion(); // 2.4.0
6682
echo (string)$version->getNextPatchVersion(); // 2.3.5
6783
echo (string)$version->getNextPreReleaseVersion(); // 2.3.5-alpha.5
84+
85+
$version = Version::create(1, 0, 0);
86+
87+
echo (string)$version->getNextMajorVersion(); // 2.0.0
88+
echo (string)$version->getNextMinorVersion(); // 1.1.0
89+
echo (string)$version->getNextPatchVersion(); // 1.0.1
90+
echo (string)$version->getNextPreReleaseVersion(); // 1.0.1-0
6891
```
92+
93+
### Copy
94+
It's possible to make a copy of a particular version with the `copy()` method.
95+
It allows altering the copied version's properties with optional parameters.
96+
```php
97+
$version = Version::parse("1.0.0-alpha.2+build.1");
98+
99+
echo (string)$version->copy(3) // 3.0.0
100+
echo (string)$version->copy(null, 4) // 1.4.0
101+
echo (string)$version->copy(null, null, 5) // 1.0.5
102+
echo (string)$version->copy(null, null, null, "alpha.4") // 1.0.0-alpha.4
103+
echo (string)$version->copy(null, null, null, null, "build.3") // 1.0.0-alpha.2+build.3
104+
echo (string)$version->copy(3, 4, 5) // 3.4.5-alpha.2+build.1
105+
```
106+
> Without setting any optional parameter, the `copy()` method will produce an exact copy of the original version.
107+
69108
## Invalid version handling
70109
When the version parsing fails due to an invalid format, the library throws a specific `VersionFormatException`.

src/PreRelease.php

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ class PreRelease
99

1010
/**
1111
* PreRelease constructor.
12-
* @param array $preReleaseParts The prerelease parts.
12+
* @param array $preReleaseParts The pre-release parts.
1313
*/
1414
private function __construct($preReleaseParts)
1515
{
1616
$this->preReleaseParts = $preReleaseParts;
1717
}
1818

1919
/**
20-
* @return PreRelease The incremented prerelease
20+
* @return PreRelease The incremented pre-release.
2121
*/
2222
public function increment()
2323
{
@@ -39,13 +39,16 @@ public function increment()
3939
}
4040

4141
/**
42-
* @return string The string representation of the prerelease.
42+
* @return string The string representation of the pre-release.
4343
*/
4444
public function __toString()
4545
{
4646
return implode('.', $this->preReleaseParts);
4747
}
4848

49+
/**
50+
* @return PreRelease The copied pre-release.
51+
*/
4952
private function copy()
5053
{
5154
return new PreRelease($this->preReleaseParts);
@@ -57,20 +60,16 @@ private function copy()
5760
private function validate()
5861
{
5962
foreach ($this->preReleaseParts as $part) {
60-
if ($this->hasOnlyNumbers($part)) {
61-
if (strlen($part) > 1 && $part[0] == "0") {
62-
throw new VersionFormatException(sprintf(
63-
"The prerelease part '%s' is numeric but contains a leading zero.",
64-
$part
65-
));
66-
} else {
67-
continue;
68-
}
63+
if ($this->hasOnlyNumbers($part) && strlen($part) > 1 && $part[0] == "0") {
64+
throw new VersionFormatException(sprintf(
65+
"The pre-release part '%s' is numeric but contains a leading zero.",
66+
$part
67+
));
6968
}
7069

7170
if (!$this->hasOnlyAlphanumericsAndHyphen($part)) {
7271
throw new VersionFormatException(sprintf(
73-
"The prerelease part '%s' contains invalid character.",
72+
"The pre-release part '%s' contains invalid character.",
7473
$part
7574
));
7675
}
@@ -79,36 +78,36 @@ private function validate()
7978

8079
/**
8180
* @param string $part part to check.
82-
* @return bool True when the part is containing only numbers.
81+
* @return bool True when the part has only numbers.
8382
*/
8483
private function hasOnlyNumbers($part)
8584
{
86-
return !preg_match("/[^0-9]/", $part);
85+
return preg_match("/^[0-9]+$/", $part);
8786
}
8887

8988
/**
9089
* @param string $part The part to check.
91-
* @return bool True when the part is only containing alphanumerics.
90+
* @return bool True when the part has only alphanumerics.
9291
*/
9392
private function hasOnlyAlphanumericsAndHyphen($part)
9493
{
95-
return !preg_match("/[^0-9A-Za-z-]/", $part);
94+
return preg_match("/^[0-9A-Za-z-]+$/", $part);
9695
}
9796

9897
/**
99-
* Creates a new prerelease tag with initial default value (-0).
98+
* Creates a new pre-release tag with initial default value (-0).
10099
*
101-
* @return PreRelease The default prerelease tag.
100+
* @return PreRelease The default pre-release tag.
102101
*/
103102
public static function createDefault()
104103
{
105104
return new PreRelease([0]);
106105
}
107106

108107
/**
109-
* @param string $preReleaseString The prerelease string.
110-
* @return PreRelease The parsed prerelease part.
111-
* @throws VersionFormatException When the given prerelease string is invalid.
108+
* @param string $preReleaseString The pre-release string.
109+
* @return PreRelease The parsed pre-release part.
110+
* @throws VersionFormatException When the given pre-release string is invalid.
112111
*/
113112
public static function parse($preReleaseString)
114113
{
@@ -127,7 +126,7 @@ public static function parse($preReleaseString)
127126
* @param string|PreRelease $p1 The left side of the comparison.
128127
* @param string|PreRelease $p2 The right side of the comparison.
129128
* @return int -1 when $p1 < $p2, 0 when $p1 == $p2, 1 when $p1 > $p2.
130-
* @throws VersionFormatException When the given prerelease values are invalid.
129+
* @throws VersionFormatException When the given pre-release values are invalid.
131130
*/
132131
public static function compare($p1, $p2)
133132
{

0 commit comments

Comments
 (0)