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
/*
3
 * This file is part of PharIo\Version.
4
 *
5
 * (c) Arne Blankerts <arne@blankerts.de>, Sebastian Heuer <sebastian@phpeople.de>, Sebastian Bergmann <sebastian@phpunit.de>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace PharIo\Version;
11
 
12
class Version {
13
    /** @var string */
14
    private $originalVersionString;
15
 
16
    /** @var VersionNumber */
17
    private $major;
18
 
19
    /** @var VersionNumber */
20
    private $minor;
21
 
22
    /** @var VersionNumber */
23
    private $patch;
24
 
25
    /** @var null|PreReleaseSuffix */
26
    private $preReleaseSuffix;
27
 
28
    /** @var null|BuildMetaData */
29
    private $buildMetadata;
30
 
31
    public function __construct(string $versionString) {
32
        $this->ensureVersionStringIsValid($versionString);
33
        $this->originalVersionString = $versionString;
34
    }
35
 
36
    /**
37
     * @throws NoPreReleaseSuffixException
38
     */
39
    public function getPreReleaseSuffix(): PreReleaseSuffix {
40
        if ($this->preReleaseSuffix === null) {
41
            throw new NoPreReleaseSuffixException('No pre-release suffix set');
42
        }
43
 
44
        return $this->preReleaseSuffix;
45
    }
46
 
47
    public function getOriginalString(): string {
48
        return $this->originalVersionString;
49
    }
50
 
51
    public function getVersionString(): string {
52
        $str = \sprintf(
53
            '%d.%d.%d',
54
            $this->getMajor()->getValue() ?? 0,
55
            $this->getMinor()->getValue() ?? 0,
56
            $this->getPatch()->getValue() ?? 0
57
        );
58
 
59
        if (!$this->hasPreReleaseSuffix()) {
60
            return $str;
61
        }
62
 
63
        return $str . '-' . $this->getPreReleaseSuffix()->asString();
64
    }
65
 
66
    public function hasPreReleaseSuffix(): bool {
67
        return $this->preReleaseSuffix !== null;
68
    }
69
 
70
    public function equals(Version $other): bool {
71
        if ($this->getVersionString() !== $other->getVersionString()) {
72
            return false;
73
        }
74
 
75
        if ($this->hasBuildMetaData() !== $other->hasBuildMetaData()) {
76
            return false;
77
        }
78
 
79
        if ($this->hasBuildMetaData() && $other->hasBuildMetaData() &&
80
            !$this->getBuildMetaData()->equals($other->getBuildMetaData())) {
81
            return false;
82
        }
83
 
84
        return true;
85
    }
86
 
87
    public function isGreaterThan(Version $version): bool {
88
        if ($version->getMajor()->getValue() > $this->getMajor()->getValue()) {
89
            return false;
90
        }
91
 
92
        if ($version->getMajor()->getValue() < $this->getMajor()->getValue()) {
93
            return true;
94
        }
95
 
96
        if ($version->getMinor()->getValue() > $this->getMinor()->getValue()) {
97
            return false;
98
        }
99
 
100
        if ($version->getMinor()->getValue() < $this->getMinor()->getValue()) {
101
            return true;
102
        }
103
 
104
        if ($version->getPatch()->getValue() > $this->getPatch()->getValue()) {
105
            return false;
106
        }
107
 
108
        if ($version->getPatch()->getValue() < $this->getPatch()->getValue()) {
109
            return true;
110
        }
111
 
112
        if (!$version->hasPreReleaseSuffix() && !$this->hasPreReleaseSuffix()) {
113
            return false;
114
        }
115
 
116
        if ($version->hasPreReleaseSuffix() && !$this->hasPreReleaseSuffix()) {
117
            return true;
118
        }
119
 
120
        if (!$version->hasPreReleaseSuffix() && $this->hasPreReleaseSuffix()) {
121
            return false;
122
        }
123
 
124
        return $this->getPreReleaseSuffix()->isGreaterThan($version->getPreReleaseSuffix());
125
    }
126
 
127
    public function getMajor(): VersionNumber {
128
        return $this->major;
129
    }
130
 
131
    public function getMinor(): VersionNumber {
132
        return $this->minor;
133
    }
134
 
135
    public function getPatch(): VersionNumber {
136
        return $this->patch;
137
    }
138
 
139
    /**
140
     * @psalm-assert-if-true BuildMetaData $this->buildMetadata
141
     * @psalm-assert-if-true BuildMetaData $this->getBuildMetaData()
142
     */
143
    public function hasBuildMetaData(): bool {
144
        return $this->buildMetadata !== null;
145
    }
146
 
147
    /**
148
     * @throws NoBuildMetaDataException
149
     */
150
    public function getBuildMetaData(): BuildMetaData {
151
        if (!$this->hasBuildMetaData()) {
152
            throw new NoBuildMetaDataException('No build metadata set');
153
        }
154
 
155
        return $this->buildMetadata;
156
    }
157
 
158
    /**
159
     * @param string[] $matches
160
     *
161
     * @throws InvalidPreReleaseSuffixException
162
     */
163
    private function parseVersion(array $matches): void {
164
        $this->major = new VersionNumber((int)$matches['Major']);
165
        $this->minor = new VersionNumber((int)$matches['Minor']);
166
        $this->patch = isset($matches['Patch']) ? new VersionNumber((int)$matches['Patch']) : new VersionNumber(0);
167
 
168
        if (isset($matches['PreReleaseSuffix']) && $matches['PreReleaseSuffix'] !== '') {
169
            $this->preReleaseSuffix = new PreReleaseSuffix($matches['PreReleaseSuffix']);
170
        }
171
 
172
        if (isset($matches['BuildMetadata'])) {
173
            $this->buildMetadata = new BuildMetaData($matches['BuildMetadata']);
174
        }
175
    }
176
 
177
    /**
178
     * @param string $version
179
     *
180
     * @throws InvalidVersionException
181
     */
182
    private function ensureVersionStringIsValid($version): void {
183
        $regex = '/^v?
184
            (?P<Major>0|[1-9]\d*)
185
            \\.
186
            (?P<Minor>0|[1-9]\d*)
187
            (\\.
188
                (?P<Patch>0|[1-9]\d*)
189
            )?
190
            (?:
191
                -
192
                (?<PreReleaseSuffix>(?:(dev|beta|b|rc|alpha|a|patch|p|pl)\.?\d*))
193
            )?
194
            (?:
195
                \\+
196
                (?P<BuildMetadata>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-@]+)*)
197
            )?
198
        $/xi';
199
 
200
        if (\preg_match($regex, $version, $matches) !== 1) {
201
            throw new InvalidVersionException(
202
                \sprintf("Version string '%s' does not follow SemVer semantics", $version)
203
            );
204
        }
205
 
206
        $this->parseVersion($matches);
207
    }
208
}