Skip to content

Commit 04e366d

Browse files
committed
Add next version generation
1 parent 63570a5 commit 04e366d

File tree

6 files changed

+284
-69
lines changed

6 files changed

+284
-69
lines changed

README.md

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ use z4kn4fein\SemVer\Version;
2424

2525
$version = Version::parse('2.5.6-alpha.12+build.34');
2626

27-
echo $version->getMajor(); // 2
28-
echo $version->getMinor(); // 5
29-
echo $version->getPatch(); // 6
30-
echo $version->getPreRelease(); // alpha.12
31-
echo $version->getBuildMeta(); // build.34
32-
echo $version->getVersionString(); // 2.5.6-alpha.12+build.34
27+
echo $version->getMajor(); // 2
28+
echo $version->getMinor(); // 5
29+
echo $version->getPatch(); // 6
30+
echo (string)$version->getPreRelease(); // alpha.12
31+
echo $version->getBuildMeta(); // build.34
32+
echo (string)$version; // 2.5.6-alpha.12+build.34
3333
```
3434
#### Comparing two versions
3535
```php
@@ -53,5 +53,18 @@ echo $version->isGreaterThan('2.5.6'); // false
5353
echo $version->isLessThanOrEqual('2.5.6-alpha.12'); // true
5454
echo $version->isEqual('2.5.6-alpha.12+build.56'); // true
5555
```
56+
#### Producing incremented versions
57+
```php
58+
<?php
59+
60+
use z4kn4fein\SemVer\Version;
61+
62+
$version = Version::create(2, 3, 5, "alpha.4", "build.2");
63+
64+
echo (string)$version->getNextMajorVersion(); // 3.0.0
65+
echo (string)$version->getNextMinorVersion(); // 2.4.0
66+
echo (string)$version->getNextPatchVersion(); // 2.3.5
67+
echo (string)$version->getNextPreReleaseVersion(); // 2.3.5-alpha.5
68+
```
5669
## Invalid version handling
5770
When the version parsing fails due to an invalid format, the library throws a specific `VersionFormatException`.

src/PreRelease.php

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ class PreRelease
77
/** @var array */
88
private $preReleaseParts;
99

10+
/**
11+
* PreRelease constructor.
12+
* @param array $preReleaseParts The prerelease parts.
13+
*/
14+
private function __construct($preReleaseParts)
15+
{
16+
$this->preReleaseParts = $preReleaseParts;
17+
}
18+
1019
/**
1120
* @return PreRelease The incremented prerelease
1221
*/
@@ -39,9 +48,7 @@ public function __toString()
3948

4049
private function copy()
4150
{
42-
$result = new PreRelease();
43-
$result->preReleaseParts = $this->preReleaseParts;
44-
return $result;
51+
return new PreRelease($this->preReleaseParts);
4552
}
4653

4754
/**
@@ -52,20 +59,22 @@ private function validate()
5259
foreach ($this->preReleaseParts as $part) {
5360
if ($this->hasOnlyNumbers($part)) {
5461
if (strlen($part) > 1 && $part[0] == "0") {
55-
throw new VersionFormatException(sprintf("The prerelease part '%s' is numeric but contains a leading zero.", $part));
62+
throw new VersionFormatException(sprintf(
63+
"The prerelease part '%s' is numeric but contains a leading zero.", $part));
5664
} else {
5765
continue;
5866
}
5967
}
6068

6169
if (!$this->hasOnlyAlphanumericsAndHyphen($part)) {
62-
throw new VersionFormatException(sprintf("The prerelease part '%s' contains invalid character.", $part));
70+
throw new VersionFormatException(sprintf(
71+
"The prerelease part '%s' contains invalid character.", $part));
6372
}
6473
}
6574
}
6675

6776
/**
68-
* @param $part string part to check.
77+
* @param string $part part to check.
6978
* @return bool True when the part is containing only numbers.
7079
*/
7180
private function hasOnlyNumbers($part)
@@ -74,7 +83,7 @@ private function hasOnlyNumbers($part)
7483
}
7584

7685
/**
77-
* @param $part string The part to check.
86+
* @param string $part The part to check.
7887
* @return bool True when the part is only containing alphanumerics.
7988
*/
8089
private function hasOnlyAlphanumericsAndHyphen($part)
@@ -83,7 +92,17 @@ private function hasOnlyAlphanumericsAndHyphen($part)
8392
}
8493

8594
/**
86-
* @param $preReleaseString string The prerelease string.
95+
* Creates a new prerelease tag with initial default value (-0).
96+
*
97+
* @return PreRelease The default prerelease tag.
98+
*/
99+
public static function createDefault()
100+
{
101+
return new PreRelease([0]);
102+
}
103+
104+
/**
105+
* @param string $preReleaseString The prerelease string.
87106
* @return PreRelease The parsed prerelease part.
88107
* @throws VersionFormatException When the given prerelease string is invalid.
89108
*/
@@ -94,17 +113,15 @@ public static function parse($preReleaseString)
94113
throw new VersionFormatException("preReleaseString cannot be empty.");
95114
}
96115

97-
$preRelease = new PreRelease();
98-
99-
$preRelease->preReleaseParts = explode('.', $preReleaseString);
116+
$preRelease = new PreRelease(explode('.', $preReleaseString));
100117
$preRelease->validate();
101118

102119
return $preRelease;
103120
}
104121

105122
/**
106-
* @param $p1 string|PreRelease The left side of the comparison.
107-
* @param $p2 string|PreRelease The right side of the comparison.
123+
* @param string|PreRelease $p1 The left side of the comparison.
124+
* @param string|PreRelease $p2 The right side of the comparison.
108125
* @return int -1 when $p1 < $p2, 0 when $p1 == $p2, 1 when $p1 > $p2.
109126
* @throws VersionFormatException When the given prerelease values are invalid.
110127
*/
@@ -134,8 +151,8 @@ public static function compare($p1, $p2)
134151
}
135152

136153
/**
137-
* @param $a mixed The left side of the comparison.
138-
* @param $b mixed The right side of the comparison.
154+
* @param mixed $a The left side of the comparison.
155+
* @param mixed $b The right side of the comparison.
139156
* @return int -1 when $a < $b, 0 when $a == $b, 1 when $v1 > $b.
140157
*/
141158
private static function comparePart($a, $b)

0 commit comments

Comments
 (0)