1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace DummyGenerator\Core; |
6
|
|
|
|
7
|
|
|
use DummyGenerator\Definitions\Extension\Awareness\RandomizerAwareExtensionInterface; |
8
|
|
|
use DummyGenerator\Definitions\Extension\Awareness\RandomizerAwareExtensionTrait; |
9
|
|
|
use DummyGenerator\Definitions\Extension\VersionExtensionInterface; |
10
|
|
|
|
11
|
|
|
class Version implements VersionExtensionInterface, RandomizerAwareExtensionInterface |
12
|
|
|
{ |
13
|
|
|
use RandomizerAwareExtensionTrait; |
14
|
|
|
|
15
|
|
|
/** @var string[] */ |
16
|
|
|
protected array $semverCommonPreReleaseIdentifiers = ['alpha', 'beta', 'rc']; |
17
|
|
|
|
18
|
3 |
|
public function semver(bool $preRelease = false, bool $build = false): string |
19
|
|
|
{ |
20
|
3 |
|
return sprintf( |
21
|
3 |
|
'%d.%d.%d%s%s', |
22
|
3 |
|
$this->randomizer->getInt(0, 9), |
23
|
3 |
|
$this->randomizer->getInt(0, 99), |
24
|
3 |
|
$this->randomizer->getInt(0, 99), |
25
|
3 |
|
$preRelease ? '-' . $this->semverPreReleaseIdentifier($this->randomizer->getBool()) : '', |
26
|
3 |
|
$build ? '+' . $this->semverBuildIdentifier($this->randomizer->getBool()) : '', |
27
|
3 |
|
); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Common pre-release identifier |
32
|
|
|
*/ |
33
|
2 |
|
private function semverPreReleaseIdentifier(bool $short = true): string |
34
|
|
|
{ |
35
|
2 |
|
$ident = $this->randomizer->randomElement($this->semverCommonPreReleaseIdentifiers); |
36
|
|
|
|
37
|
2 |
|
if ($short) { |
38
|
1 |
|
return $ident; |
39
|
|
|
} |
40
|
|
|
|
41
|
1 |
|
return $ident . '.' . $this->randomizer->getInt(1, 99); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Common random build identifier |
46
|
|
|
*/ |
47
|
2 |
|
private function semverBuildIdentifier(bool $shortSyntax = true): string |
48
|
|
|
{ |
49
|
2 |
|
if ($shortSyntax) { |
50
|
|
|
// short git revision syntax: https://git-scm.com/book/en/v2/Git-Tools-Revision-Selection |
51
|
1 |
|
return substr(sha1(uniqid('', true)), 0, 7); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
// date syntax |
55
|
1 |
|
return (new \DateTimeImmutable())->format('YmdHis'); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|