Passed
Push — master ( 624a9a...4c85b9 )
by Arthur
36:47
created

Version::setMajor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 7
ccs 6
cts 6
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: arthur
5
 * Date: 23.03.19
6
 * Time: 19:46
7
 */
8
9
namespace Modules\Script\Support;
10
11
class Version
12
{
13
    /** @var int Major release number */
14
    protected $major;
15
16
    /** @var int Minor release number */
17
    protected $minor;
18
19
    /** @var int Patch release number */
20
    protected $patch;
21
22
    /** @var string|null Pre-release value */
23
    protected $preRelease;
24
25
    /** @var string Build release value */
26
    protected $build;
27
28
    /**
29
     * Class constructor, runs on object creation.
30
     *
31
     * @param string $version Version string
32
     */
33 7
    public function __construct($version = null)
34
    {
35 7
        $this->setVersion($version ?? '0.1.0');
36 7
    }
37
    /**
38
     * Magic get method; privied access to version properties.
39
     *
40
     * @param string $property Version property
41
     *
42
     * @return mixed Version property value
43
     */
44
    public function __get($property)
45
    {
46
        return $this->$property;
47
    }
48
    /**
49
     * Magic toString method; allows object interaction as if it were a string.
50
     *
51
     * @param string $prefix Prefix the version string with a custom string
52
     *                       (default: 'v')
53
     *
54
     * @return string Current version string
55
     */
56 7
    public function __toString()
57
    {
58 7
        return $this->toString();
59
    }
60
    /**
61
     * Set (override) the entire version value.
62
     *
63
     * @param string $version Version string
64
     *
65
     * @return Version This Version object
66
     */
67 7
    public function setVersion($version)
68
    {
69 7
        $semverRegex = '/^v?(\d+)\.(\d+)\.(\d+)(?:-([0-9A-Z-.]+))?(?:\+([0-9A-Z-.]+)?)?$/i';
70 7
        if (! preg_match($semverRegex, $version, $matches)) {
71
            throw new \Exception('Invalid Semantic Version string provided');
72
        }
73 7
        $this->major = (int) $matches[1];
74 7
        $this->minor = (int) $matches[2];
75 7
        $this->patch = (int) $matches[3];
76 7
        $this->preRelease = @$matches[4] ?: null;
77 7
        $this->build = @$matches[5] ?: null;
78 7
        return $this;
79
    }
80
    /**
81
     * Increment the major version value by one.
82
     *
83
     * @return Version This Version object
84
     */
85 1
    public function incrementMajor()
86
    {
87 1
        $this->setMajor($this->major + 1);
88 1
        return $this;
89
    }
90
    /**
91
     * Set the major version to a custom value.
92
     *
93
     * @param int $value Positive integer value
94
     *
95
     * @return Version This Version object
96
     */
97 1
    public function setMajor($value)
98
    {
99 1
        $this->major = $value;
100 1
        $this->minor = 0;
101 1
        $this->patch = 0;
102 1
        $this->preRelease = null;
103 1
        return $this;
104
    }
105
    /**
106
     * Increment the minor version value by one.
107
     *
108
     * @return Version This Version object
109
     */
110 7
    public function incrementMinor()
111
    {
112 7
        $this->setMinor($this->minor + 1);
113 7
        return $this;
114
    }
115
    /**
116
     * Set the minor version to a custom value.
117
     *
118
     * @param int $value Positive integer value
119
     *
120
     * @return Version This Version object
121
     */
122 7
    public function setMinor($value)
123
    {
124 7
        $this->minor = $value;
125 7
        $this->patch = 0;
126 7
        $this->preRelease = null;
127 7
        return $this;
128
    }
129
    /**
130
     * Increment the patch version value by one.
131
     *
132
     * @return Version This Version object
133
     */
134
    public function incrementPatch()
135
    {
136
        $this->setPatch($this->patch + 1);
137
        return $this;
138
    }
139
    /**
140
     * Set the patch version to a custom value.
141
     *
142
     * @param int $value Positive integer value
143
     *
144
     * @return Version This Version object
145
     */
146
    public function setPatch($value)
147
    {
148
        $this->patch = $value;
149
        $this->preRelease = null;
150
        return $this;
151
    }
152
    /**
153
     * Set the pre-release string to a custom value.
154
     *
155
     * @param string $value A new pre-release value
156
     *
157
     * @return Version This Version object
158
     */
159
    public function setPreRelease($value)
160
    {
161
        $this->preRelease = $value;
162
        return $this;
163
    }
164
    /**
165
     * Set the build string to a custom value.
166
     *
167
     * @param string $value A new build value
168
     *
169
     * @return Version This Version object
170
     */
171
    public function setBuild($value)
172
    {
173
        $this->build = $value;
174
        return $this;
175
    }
176
    /**
177
     * Check if this Version object is greater than another.
178
     *
179
     * @param Version $version An instance of SemVer/Version
180
     *
181
     * @return bool True if this Version object is greater than the comparing
182
     *              object, otherwise false
183
     */
184
    public function gt(Version $version)
185
    {
186
        if ($this->major > $version->major) {
187
            return true;
188
        }
189
        if ($this->major == $version->major
190
            && $this->minor > $version->minor
191
        ) {
192
            return true;
193
        }
194
        if ($this->major == $version->major
195
            && $this->minor == $version->minor
196
            && $this->patch > $version->patch
197
        ) {
198
            return true;
199
        }
200
        // TODO: Check pre-release tag
201
        return false;
202
    }
203
    /**
204
     * Check if this Version object is less than another.
205
     *
206
     * @param Version $version An instance of SemVer/Version
207
     *
208
     * @return bool True if this Version object is less than the comparing
209
     *              object, otherwise false
210
     */
211
    public function lt(Version $version)
212
    {
213
        if ($this->major < $version->major) {
214
            return true;
215
        }
216
        if ($this->major == $version->major
217
            && $this->minor < $version->minor
218
        ) {
219
            return true;
220
        }
221
        if ($this->major == $version->major
222
            && $this->minor == $version->minor
223
            && $this->patch < $version->patch
224
        ) {
225
            return true;
226
        }
227
        // TODO: Check pre-release tag
228
        return false;
229
    }
230
    /**
231
     * Check if this Version object is equal to than another.
232
     *
233
     * @param Version $version An instance of SemVer/Version
234
     *
235
     * @return bool True if this Version object is equal to the comparing
236
     *              object, otherwise false
237
     */
238
    public function eq(Version $version)
239
    {
240
        return $this == $version;
241
    }
242
    /**
243
     * Check if this Version object is not equal to another.
244
     *
245
     * @param Version $version An instance of SemVer/Version
246
     *
247
     * @return bool True if this Version object is not equal to the comparing
248
     *              object, otherwise false
249
     */
250
    public function neq(Version $version)
251
    {
252
        return $this != $version;
253
    }
254
    /**
255
     * Check if this Version object is greater than or equal to another.
256
     *
257
     * @param Version $version An instance of SemVer/Version
258
     *
259
     * @return bool True if this Version object is greater than or equal to the
260
     *              comparing object, otherwise false
261
     */
262
    public function gte(Version $version)
263
    {
264
        return $this->gt($version) || $this->eq($version);
265
    }
266
    /**
267
     * Check if this Version object is less than or equal to another.
268
     *
269
     * @param Version $version An instance of SemVer/Version
270
     *
271
     * @return bool True if this Version object is less than or equal to the
272
     *              comparing object, otherwise false
273
     */
274
    public function lte(Version $version)
275
    {
276
        return $this->lt($version) || $this->eq($version);
277
    }
278
    /**
279
     * Get the version string prefixed with a custom string.
280
     *
281
     * @param string $prefix String to prepend to the version string
282
     *                       (default: 'v')
283
     *
284
     * @return string Prefixed version string
285
     */
286
    public function prefix($prefix = 'v')
287
    {
288
        return $prefix . $this->toString();
289
    }
290
    /**
291
     * Get the current version value as a string.
292
     *
293
     * @return string Current version string
294
     */
295 7
    private function toString()
296
    {
297 7
        $version = implode('.', [$this->major, $this->minor, $this->patch]);
298 7
        $version .= isset($this->preRelease) ? '-' . $this->preRelease : null;
299 7
        $version .= isset($this->build) ? '+' . $this->build : null;
300 7
        return $version;
301
    }
302
}
303