Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php declare(strict_types = 1);
2
namespace PharIo\Version;
3
 
4
class PreReleaseSuffix {
5
    private const valueScoreMap = [
6
        'dev'   => 0,
7
        'a'     => 1,
8
        'alpha' => 1,
9
        'b'     => 2,
10
        'beta'  => 2,
11
        'rc'    => 3,
12
        'p'     => 4,
13
        'pl'    => 4,
14
        'patch' => 4,
15
    ];
16
 
17
    /** @var string */
18
    private $value;
19
 
20
    /** @var int */
21
    private $valueScore;
22
 
23
    /** @var int */
24
    private $number = 0;
25
 
26
    /** @var string */
27
    private $full;
28
 
29
    /**
30
     * @throws InvalidPreReleaseSuffixException
31
     */
32
    public function __construct(string $value) {
33
        $this->parseValue($value);
34
    }
35
 
36
    public function asString(): string {
37
        return $this->full;
38
    }
39
 
40
    public function getValue(): string {
41
        return $this->value;
42
    }
43
 
44
    public function getNumber(): ?int {
45
        return $this->number;
46
    }
47
 
48
    public function isGreaterThan(PreReleaseSuffix $suffix): bool {
49
        if ($this->valueScore > $suffix->valueScore) {
50
            return true;
51
        }
52
 
53
        if ($this->valueScore < $suffix->valueScore) {
54
            return false;
55
        }
56
 
57
        return $this->getNumber() > $suffix->getNumber();
58
    }
59
 
60
    private function mapValueToScore(string $value): int {
61
        $value = \strtolower($value);
62
 
63
        return self::valueScoreMap[$value];
64
    }
65
 
66
    private function parseValue(string $value): void {
67
        $regex = '/-?((dev|beta|b|rc|alpha|a|patch|p|pl)\.?(\d*)).*$/i';
68
 
69
        if (\preg_match($regex, $value, $matches) !== 1) {
70
            throw new InvalidPreReleaseSuffixException(\sprintf('Invalid label %s', $value));
71
        }
72
 
73
        $this->full  = $matches[1];
74
        $this->value = $matches[2];
75
 
76
        if ($matches[3] !== '') {
77
            $this->number = (int)$matches[3];
78
        }
79
 
80
        $this->valueScore = $this->mapValueToScore($matches[2]);
81
    }
82
}