Skip to content

Commit daa7699

Browse files
committed
Updated all examples; Added docs.
1 parent 4d45ac0 commit daa7699

1 file changed

Lines changed: 134 additions & 29 deletions

File tree

README.md

Lines changed: 134 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ The parser expects versions to be in the following format:
1010

1111
This allows the use of a wide range of version strings. Some examples:
1212

13-
- 1
14-
- 1.1
15-
- 1.1.5
16-
- 1.145.147
17-
- 1.1.5-rc1
18-
- 1.1.5-beta
19-
- 1.1.5-beta2
20-
- 1.1.5-BranchName
21-
- 1.1.5-BranchName-alpha2
13+
- `1`
14+
- `1.1`
15+
- `1.1.5`
16+
- `1.145.147`
17+
- `1.1.5-rc1`
18+
- `1.1.5-beta`
19+
- `1.1.5-beta2`
20+
- `1.1.5-BranchName`
21+
- `1.1.5-BranchName-alpha2`
2222

2323
> NOTE: Hyphens are typically used as separators, but underscores
2424
are acceptable as well. Spaces are not supported.
@@ -29,23 +29,27 @@ Simply require the package with composer.
2929

3030
Via command line:
3131

32-
```
32+
```shell
3333
composer require mistralys/version-parser
3434
```
3535

3636
Via composer.json:
3737

3838
```json
39-
"require": {
40-
"mistralys/version-parser": "dev-master"
39+
{
40+
"require": {
41+
"mistralys/version-parser": "dev-master"
42+
}
4143
}
4244
```
4345

4446
## Usage
4547

4648
### Getting individual version numbers
4749

48-
```
50+
```php
51+
use Mistralys\VersionParser\VersionParser;
52+
4953
$version = VersionParser::create('1.5.2');
5054

5155
$major = $version->getMajorVersion(); // 1
@@ -55,15 +59,19 @@ $patch = $version->getPatchVersion(); // 2
5559

5660
### Getting a version without tag
5761

58-
```
62+
```php
63+
use Mistralys\VersionParser\VersionParser;
64+
5965
$version = VersionParser::create('1.5.2-RC3');
6066

6167
$number = $version->getVersion(); // 1.5.2
6268
```
6369

6470
The version is normalized to show all three levels, even if they were not specified.
6571

66-
```
72+
```php
73+
use Mistralys\VersionParser\VersionParser;
74+
6775
$version = VersionParser::create('1');
6876

6977
$number = $version->getVersion(); // 1.0.0
@@ -73,15 +81,19 @@ $number = $version->getVersion(); // 1.0.0
7381

7482
The method `getShortVersion()` retrieves a version string with the minimum possible levels.
7583

76-
```
84+
```php
85+
use Mistralys\VersionParser\VersionParser;
86+
7787
$version = VersionParser::create('1.0.0');
7888

7989
$number = $version->getVersion(); // 1
8090
```
8191

8292
### Getting the full version, normalized
8393

84-
```
94+
```php
95+
use Mistralys\VersionParser\VersionParser;
96+
8597
$version = VersionParser::create('1-BETA');
8698

8799
$normalized = $version->getTagVersion(); // 1.0.0-beta
@@ -92,15 +104,19 @@ $normalized = $version->getTagVersion(); // 1.0.0-beta
92104
To check the release type, the shorthand methods `isBeta()`, `isAlpha()` and `isReleaseCandidate()` can be used.
93105
See "Supported release tags" for details.
94106

95-
```
107+
```php
108+
use Mistralys\VersionParser\VersionParser;
109+
96110
$version = VersionParser::create('1.5.2-beta');
97111

98112
$isBeta = $version->isBeta(); // true
99113
```
100114

101115
Alternatively, it is possible to check the tag type manually.
102116

103-
```
117+
```php
118+
use Mistralys\VersionParser\VersionParser;
119+
104120
$version = VersionParser::create('1.5.2-beta5');
105121

106122
if($version->getTagType() === VersionParser::TAG_TYPE_BETA)
@@ -113,23 +129,29 @@ if($version->getTagType() === VersionParser::TAG_TYPE_BETA)
113129

114130
When no number is added to the tag, it is assumed that it is the tag #1.
115131

116-
```
132+
```php
133+
use Mistralys\VersionParser\VersionParser;
134+
117135
$version = VersionParser::create('1.5.2-beta');
118136

119137
$betaVersion = $version->getTagNumber(); // 1 (implicit)
120138
```
121139

122140
With a number added:
123141

124-
```
142+
```php
143+
use Mistralys\VersionParser\VersionParser;
144+
125145
$version = VersionParser::create('1.5.2-beta5');
126146

127147
$betaVersion = $version->getTagNumber(); // 5
128148
```
129149

130150
### Getting the branch name
131151

132-
```
152+
```php
153+
use Mistralys\VersionParser\VersionParser;
154+
133155
$version = VersionParser::create('1.5.2-Foobar');
134156

135157
$hasBranch = $version->hasBranch(); // true
@@ -138,7 +160,9 @@ $branchName = $version->getBranchName(); // Foobar
138160

139161
This also works in combination with a release tag:
140162

141-
```
163+
```php
164+
use Mistralys\VersionParser\VersionParser;
165+
142166
$version = VersionParser::create('1.5.2-Foobar-RC1');
143167

144168
$hasBranch = $version->hasBranch(); // true
@@ -147,21 +171,66 @@ $branchName = $version->getBranchName(); // Foobar
147171

148172
Branch names may contain numbers, but no hyphens.
149173

150-
```
174+
```php
175+
use Mistralys\VersionParser\VersionParser;
176+
151177
$version = VersionParser::create('1.5.2-Foobar45');
152178

153179
$hasBranch = $version->hasBranch(); // true
154180
$branchName = $version->getBranchName(); // Foobar45
155181
```
156182

183+
### Setting the separator character
184+
185+
By default, the branch name and tag are separated with hyphens (`-`).
186+
This can be adjusted to any character:
187+
188+
```php
189+
use Mistralys\VersionParser\VersionParser;
190+
191+
$version = VersionParser::create('1.5.2-BranchName-alpha5');
192+
193+
echo $version
194+
->setSeparatorChar('_')
195+
->getTagVersion();
196+
```
197+
198+
Will output:
199+
200+
```
201+
1.5.2_BranchName_alpha5
202+
```
203+
204+
### Converting tag names to uppercase
205+
206+
By default, tag names are lowercased. They can be converted to
207+
uppercase:
208+
209+
```php
210+
use Mistralys\VersionParser\VersionParser;
211+
212+
$version = VersionParser::create('1.5.2-BranchName-alpha5');
213+
214+
echo $version
215+
->setUppercase()
216+
->getTagVersion();
217+
```
218+
219+
Will output:
220+
221+
```
222+
1.5.2-BranchName-ALPHA5
223+
```
224+
157225
## Supported release tags
158226

159227
The parser will handle the following tags automatically, and assign
160228
them a build number value:
161229

162-
- `alpha` - Alpha release
163-
- `beta` - Beta release
164-
- `rc` - Release candidate
230+
- `alpha` - Alpha release, weight: `6`
231+
- `beta` - Beta release, weight: `4`
232+
- `rc` - Release candidate, weight: `2`
233+
- `snapshot` - Code snapshot, weight: `0`
165234

166235
This means that comparing the same version numbers with different
167236
release tags will work. For example, `1.4-beta` is considered a higher
@@ -174,14 +243,50 @@ Also supported is numbering tagged versions:
174243
- `1.0-alpha` - Implied `alpha1`
175244
- `1.0-alpha2` - Alpha `2`
176245

246+
### Adding custom tags
247+
248+
If you use other tag types in your application's version strings,
249+
they can be added so the parser recognizes them:
250+
251+
```php
252+
use Mistralys\VersionParser\VersionParser;
253+
254+
VersionParser::registerTagType('foobar', 5);
255+
256+
$version = VersionParser::create('1.0.5-foobar2');
257+
258+
echo $version->getTagType(); // foobar
259+
```
260+
261+
If you mix custom tag types and the standard ones, be careful with
262+
the sorting weight you set for them, so they will be weighted correctly.
263+
If needed, you can change the weight of the default types to make more
264+
room.
265+
266+
This for example resets them all, and inserts some new types:
267+
268+
```php
269+
use Mistralys\VersionParser\VersionParser;
270+
271+
$weight = 900; // Can be any number
272+
VersionParser::registerTagType(VersionParser::TAG_TYPE_ALPHA, $weight--);
273+
VersionParser::registerTagType(VersionParser::TAG_TYPE_BETA, $weight--);
274+
VersionParser::registerTagType('foobar', $weight--);
275+
VersionParser::registerTagType(VersionParser::TAG_TYPE_RELEASE_CANDIDATE, $weight--);
276+
VersionParser::registerTagType('prefinal', $weight--);
277+
```
278+
279+
> NOTE: The weights are used in the generated build numbers. Changing the
280+
> default tag type weights will change their build numbers as well.
281+
177282
## Build numbers
178283

179284
The version strings are intelligently converted to numbers, to allow
180285
comparisons and sorting. This includes the release tags like `alpha`
181286
or `beta`, which are converted as well. The result is a build number
182287
which can be either a floating point number, or an integer.
183288

184-
> NOTE: These numbers are not meant to be human readable. Their sole
289+
> NOTE: These numbers are not meant to be human-readable. Their sole
185290
purpose is to recognize version numbers programmatically.
186291

187292
There are two methods related to this:
@@ -222,4 +327,4 @@ This will sort the list the following way:
222327
2. `1.5.9-beta`
223328
3. `1.5.9`
224329
4. `2.0.0-alpha`
225-
5. `2.0.0`
330+
5. `2.0.0`

0 commit comments

Comments
 (0)