|
| 1 | +# Version string parser for PHP |
| 2 | + |
| 3 | +PHP utility used to parse application version strings, and retrieve information on the version, as well as convert it to a numeric build number (float or integer). Supports release tags like alpha, beta and reelease candidate, as well as custom branch names. |
| 4 | + |
| 5 | +## Supported version strings |
| 6 | + |
| 7 | +The parser expects versions to be in the following format: |
| 8 | + |
| 9 | +`MajorVersion.MinorVersion.PatchVersion-BranchName-ReleaseTagNumber` |
| 10 | + |
| 11 | +This allows the use of a wide range of version strings. Some examples: |
| 12 | + |
| 13 | + - 1 |
| 14 | + - 1.1 |
| 15 | + - 1.1.5 |
| 16 | + - 1.145.147 |
| 17 | + - 1.1.5-beta |
| 18 | + - 1.1.5-beta2 |
| 19 | + - 1.1.5-BranchName |
| 20 | + - 1.1.5-BranchName-alpha2 |
| 21 | + |
| 22 | +## Installation |
| 23 | + |
| 24 | +Simply require the package with composer. |
| 25 | + |
| 26 | +Via command line: |
| 27 | + |
| 28 | +``` |
| 29 | +composer require mistralys/version-parser |
| 30 | +``` |
| 31 | + |
| 32 | +Via composer.json: |
| 33 | + |
| 34 | +```json |
| 35 | +"require": { |
| 36 | + "mistralys/version-parser": "dev-master" |
| 37 | +} |
| 38 | +``` |
| 39 | + |
| 40 | +## Usage |
| 41 | + |
| 42 | +### Getting individual version numbers |
| 43 | + |
| 44 | +``` |
| 45 | +$version = VersionParser::create('1.5.2'); |
| 46 | +
|
| 47 | +$major = $version->getMajorVersion(); // 1 |
| 48 | +$minor = $version->getMinorVersion(); // 5 |
| 49 | +$patch = $version->getPatchVersion(); // 2 |
| 50 | +``` |
| 51 | + |
| 52 | +### Getting a version without tag |
| 53 | + |
| 54 | +``` |
| 55 | +$version = VersionParser::create('1.5.2-RC3'); |
| 56 | +
|
| 57 | +$number = $version->getVersion(); // 1.5.2 |
| 58 | +``` |
| 59 | + |
| 60 | +The version is normalized to show all three levels, even if they were not specified. |
| 61 | + |
| 62 | +``` |
| 63 | +$version = VersionParser::create('1'); |
| 64 | +
|
| 65 | +$number = $version->getVersion(); // 1.0.0 |
| 66 | +``` |
| 67 | + |
| 68 | +### Getting a short version |
| 69 | + |
| 70 | +The method `getShortVersion()` retrieves a version string with the minimum possible levels. |
| 71 | + |
| 72 | +``` |
| 73 | +$version = VersionParser::create('1.0.0'); |
| 74 | +
|
| 75 | +$number = $version->getVersion(); // 1 |
| 76 | +``` |
| 77 | + |
| 78 | +### Getting the full version, normalized |
| 79 | + |
| 80 | +``` |
| 81 | +$version = VersionParser::create('1-BETA'); |
| 82 | +
|
| 83 | +$normalized = $version->getTagVersion(); // 1.0.0-beta |
| 84 | +``` |
| 85 | + |
| 86 | +### Checking the tag type |
| 87 | + |
| 88 | +To check the release type, the shorthand methods `isBeta()`, `isAlpha()` and `isReleaseCandidate()` can be used. |
| 89 | + |
| 90 | +``` |
| 91 | +$version = VersionParser::create('1.5.2-beta'); |
| 92 | +
|
| 93 | +$isBeta = $version->isBeta(); // true |
| 94 | +``` |
| 95 | + |
| 96 | +Alternatively, it is possible to check the tag type manually. |
| 97 | + |
| 98 | +``` |
| 99 | +$version = VersionParser::create('1.5.2-beta5'); |
| 100 | +
|
| 101 | +if($version->getTagType() === VersionParser::TAG_TYPE_BETA) |
| 102 | +{ |
| 103 | + // is a beta version |
| 104 | +} |
| 105 | +``` |
| 106 | + |
| 107 | +### Getting the tag number |
| 108 | + |
| 109 | +When no number is added to the tag, it is assumed that it is the tag #1. |
| 110 | + |
| 111 | +``` |
| 112 | +$version = VersionParser::create('1.5.2-beta'); |
| 113 | +
|
| 114 | +$betaVersion = $version->getTagNumber(); // 1 (implicit) |
| 115 | +``` |
| 116 | + |
| 117 | +With a number added: |
| 118 | + |
| 119 | +``` |
| 120 | +$version = VersionParser::create('1.5.2-beta5'); |
| 121 | +
|
| 122 | +$betaVersion = $version->getTagNumber(); // 5 |
| 123 | +``` |
| 124 | + |
| 125 | +### Getting the branch name |
| 126 | + |
| 127 | +``` |
| 128 | +$version = VersionParser::create('1.5.2-Foobar'); |
| 129 | +
|
| 130 | +$hasBranch = $version->hasBranch(); // true |
| 131 | +$branchName = $version->getBranchName(); // Foobar |
| 132 | +``` |
| 133 | + |
| 134 | +This also works in combination with a release tag: |
| 135 | + |
| 136 | +``` |
| 137 | +$version = VersionParser::create('1.5.2-Foobar-RC1'); |
| 138 | +
|
| 139 | +$hasBranch = $version->hasBranch(); // true |
| 140 | +$branchName = $version->getBranchName(); // Foobar |
| 141 | +``` |
| 142 | + |
| 143 | +Branch names may contain numbers, but no hyphens. |
| 144 | + |
| 145 | +``` |
| 146 | +$version = VersionParser::create('1.5.2-Foobar45'); |
| 147 | +
|
| 148 | +$hasBranch = $version->hasBranch(); // true |
| 149 | +$branchName = $version->getBranchName(); // Foobar45 |
| 150 | +``` |
| 151 | + |
0 commit comments