Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
SemanticVersion
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
4 / 4
4
100.00% covered (success)
100.00%
1 / 1
 createRandom
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 convert
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 toApplicationVersion
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getRegularExpression
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2namespace Apie\CommonValueObjects;
3
4use Apie\Core\Attributes\Description;
5use Apie\Core\Attributes\FakeMethod;
6use Apie\Core\ValueObjects\Interfaces\HasRegexValueObjectInterface;
7use Apie\Core\ValueObjects\IsStringWithRegexValueObject;
8use Faker\Generator;
9
10#[FakeMethod('createRandom')]
11#[Description('Represents a semantic version, possibly also with a suffix, for example "1.0.0-dev"')]
12class 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    /**
34     * @see https://semver.org/spec/v2.0.0.html
35     */
36    public static function getRegularExpression(): string
37    {
38        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-]+)*))?$/';
39    }
40}