Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
15 / 15 |
|
100.00% |
7 / 7 |
CRAP | |
100.00% |
1 / 1 |
| SemanticVersion | |
100.00% |
15 / 15 |
|
100.00% |
7 / 7 |
7 | |
100.00% |
1 / 1 |
| createRandom | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| convert | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| toApplicationVersion | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| bumpPatch | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| bumpMinor | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| bumpMajor | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| getRegularExpression | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | namespace Apie\CommonValueObjects; |
| 3 | |
| 4 | use Apie\Core\Attributes\Description; |
| 5 | use Apie\Core\Attributes\FakeMethod; |
| 6 | use Apie\Core\ValueObjects\Interfaces\HasRegexValueObjectInterface; |
| 7 | use Apie\Core\ValueObjects\IsStringWithRegexValueObject; |
| 8 | use Faker\Generator; |
| 9 | |
| 10 | #[FakeMethod('createRandom')] |
| 11 | #[Description('Represents a semantic version, possibly also with a suffix, for example "1.0.0-dev"')] |
| 12 | class SemanticVersion implements HasRegexValueObjectInterface |
| 13 | { |
| 14 | use IsStringWithRegexValueObject; |
| 15 | |
| 16 | public static function createRandom(Generator $generator): self |
| 17 | { |
| 18 | return static::fromNative($generator->semver(true, true)); |
| 19 | } |
| 20 | |
| 21 | protected function convert(string $input): string |
| 22 | { |
| 23 | return trim($input); |
| 24 | } |
| 25 | |
| 26 | public function toApplicationVersion(): ApplicationVersion |
| 27 | { |
| 28 | preg_match('/^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)/', $this->internal, $matches); |
| 29 | assert(!empty($matches)); |
| 30 | return new ApplicationVersion($matches[1] . '.' . $matches[2] . '.' . $matches[3]); |
| 31 | } |
| 32 | |
| 33 | public function bumpPatch(): self |
| 34 | { |
| 35 | preg_match('/^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(.*)$/', $this->internal, $matches); |
| 36 | assert(!empty($matches)); |
| 37 | |
| 38 | return new self(sprintf('%d.%d.%d%s', (int) $matches[1], (int) $matches[2], (int) $matches[3] + 1, $matches[4])); |
| 39 | } |
| 40 | |
| 41 | public function bumpMinor(): self |
| 42 | { |
| 43 | preg_match('/^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(.*)$/', $this->internal, $matches); |
| 44 | assert(!empty($matches)); |
| 45 | |
| 46 | return new self(sprintf('%d.%d.%d%s', (int) $matches[1], (int) $matches[2] + 1, 0, $matches[4])); |
| 47 | } |
| 48 | |
| 49 | public function bumpMajor(): self |
| 50 | { |
| 51 | preg_match('/^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(.*)$/', $this->internal, $matches); |
| 52 | assert(!empty($matches)); |
| 53 | |
| 54 | return new self(sprintf('%d.%d.%d%s', (int) $matches[1] + 1, 0, 0, $matches[4])); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * @see https://semver.org/spec/v2.0.0.html |
| 59 | */ |
| 60 | public static function getRegularExpression(): string |
| 61 | { |
| 62 | return '/^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/'; |
| 63 | } |
| 64 | } |