├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── composer.json ├── doc ├── 00-Installing.md ├── 01-Building.md ├── 02-Comparing.md ├── 03-Dumping.md ├── 04-Parsing.md └── 05-Validating.md ├── phpunit.xml.dist └── src ├── lib └── Herrera │ └── Version │ ├── Builder.php │ ├── Comparator.php │ ├── Dumper.php │ ├── Exception │ ├── InvalidIdentifierException.php │ ├── InvalidNumberException.php │ ├── InvalidStringRepresentationException.php │ └── VersionException.php │ ├── Parser.php │ ├── Validator.php │ └── Version.php └── tests └── lib └── Herrera └── Version └── Tests ├── BuilderTest.php ├── ComparatorTest.php ├── DumperTest.php ├── Exception ├── InvalidIdentifierExceptionTest.php ├── InvalidNumberExceptionTest.php └── InvalidStringRepresentationExceptionTest.php ├── ParserTest.php ├── ValidatorTest.php └── VersionTest.php /.gitignore: -------------------------------------------------------------------------------- 1 | /.idea/ 2 | /bin/ 3 | /coverage/ 4 | /src/vendors/ 5 | 6 | /composer.lock 7 | /*.iml 8 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | php: 4 | - 5.3 5 | - 5.4 6 | - 5.5 7 | - 5.6 8 | - hhvm 9 | 10 | before_install: 11 | - composer self-update 12 | 13 | install: 14 | - composer update --dev --no-interaction --prefer-source 15 | 16 | script: 17 | - phpunit 18 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2013 Kevin Herrera 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 7 | of the Software, and to permit persons to whom the Software is furnished to do 8 | so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 15 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 16 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 17 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 18 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 19 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Version 2 | ======= 3 | 4 | [![Build Status]](http://travis-ci.org/herrera-io/php-version) 5 | 6 | Version is a library for creating, editing, and comparing semantic version 7 | numbers. Currently, v2.0.0 of the [Semantic Versioning][] specification 8 | is supported. 9 | 10 | ```php 11 | use Herrera\Version\Dumper; 12 | use Herrera\Version\Parser; 13 | 14 | $builder = Parser::toBuilder('1.2.3-alpha+2'); 15 | $builder->incrementMajor(); 16 | $builder->clearBuild(); 17 | $builder->clearPreRelease(); 18 | 19 | echo Dumper::toString($builder); // echoes "2.0.0" 20 | 21 | $finalVersion = $builder->getVersion(); 22 | ``` 23 | 24 | Documentation 25 | ------------- 26 | 27 | - [Installing][] 28 | - Usage 29 | - [Building][] 30 | - [Comparing][] 31 | - [Dumping][] 32 | - [Parsing][] 33 | - [Validating][] 34 | 35 | [Build Status]: https://secure.travis-ci.org/herrera-io/php-version.png?branch=master 36 | [Semantic Versioning]: http://semver.org/spec/v2.0.0.html 37 | [Installing]: doc/00-Installing.md 38 | [Building]: doc/01-Building.md 39 | [Comparing]: doc/02-Comparing.md 40 | [Dumping]: doc/03-Dumping.md 41 | [Parsing]: doc/04-Parsing.md 42 | [Validating]: doc/05-Validating.md 43 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "herrera-io/version", 3 | "description": "A library for creating, editing, and comparing semantic versioning numbers.", 4 | "keywords": ["semantic", "version"], 5 | "homepage": "http://github.com/herrera-io/php-version", 6 | "license": "MIT", 7 | "authors": [ 8 | { 9 | "name": "Kevin Herrera", 10 | "email": "kevin@herrera.io", 11 | "homepage": "http://kevin.herrera.io" 12 | } 13 | ], 14 | "support": { 15 | "forum": "https://groups.google.com/d/forum/herrera-io-users", 16 | "issues": "https://github.com/herrera-io/php-version/issues" 17 | }, 18 | "require": { 19 | "php": ">=5.3.3" 20 | }, 21 | "require-dev": { 22 | "herrera-io/phpunit-test-case": "1.*", 23 | "phpunit/phpunit": "3.7.*" 24 | }, 25 | "autoload": { 26 | "psr-0": { 27 | "Herrera\\Version": "src/lib" 28 | } 29 | }, 30 | "config": { 31 | "bin-dir": "bin", 32 | "vendor-dir": "src/vendors" 33 | }, 34 | "extra": { 35 | "branch-alias": { 36 | "dev-master": "1.2.x-dev" 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /doc/00-Installing.md: -------------------------------------------------------------------------------- 1 | Installing 2 | ========== 3 | 4 | Composer 5 | -------- 6 | 7 | The easiest way to install Version is by using [Composer][]: 8 | 9 | $ composer require herrera-io/version=~1.0 10 | 11 | You may then load it by requiring the Composer autoloader: 12 | 13 | ```php 14 | require 'vendor/autoload.php'; 15 | ``` 16 | 17 | PSR-0 18 | ----- 19 | 20 | You may use any class loader that supports [PSR-0][]. 21 | 22 | ```php 23 | $loader = new SplClassLoader(); 24 | $loader->add('Herrera\\Version', 'src/lib'); 25 | ``` 26 | 27 | [Composer]: http://getcomposer.org/ 28 | [PSR-0]: https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md -------------------------------------------------------------------------------- /doc/01-Building.md: -------------------------------------------------------------------------------- 1 | Building 2 | ======== 3 | 4 | The Version library provides a programmatic way of creating and manipulating 5 | semantic version numbers. Some methods will even automatically apply the logic 6 | defined by Semantic Versioning specification. 7 | 8 | Creating a Builder 9 | ------------------ 10 | 11 | ### Fresh Start 12 | 13 | - `Herrera\Version\Builder::create()` 14 | 15 | To create a fresh version builder, call `Builder::create()`: 16 | 17 | ```php 18 | $builder = Herrera\Version\Builder::create(); 19 | ``` 20 | 21 | The default version is `0.0.0`. 22 | 23 | Importing Existing Versions 24 | --------------------------- 25 | 26 | ### From Components (Advanced) 27 | 28 | - `Herrera\Version\Builder::importComponents(array $components)` 29 | 30 | If you have extracted the components of a version string representation using 31 | the `Herrera\Version\Parser::toComponents()` method, you can call the method, 32 | `importComponents()`: 33 | 34 | ```php 35 | $builder->importComponents($components); 36 | ``` 37 | 38 | Any version information missing from the given components array will be reset 39 | to their default values in the builder: 40 | 41 | - **Major Version**: `0` 42 | - **Minor Version**: `0` 43 | - **Patch Version**: `0` 44 | - **Pre-release Version**: none 45 | - **Build Metadata**: none 46 | 47 | ### From a String Representation 48 | 49 | - `Herrera\Version\Builder::importString(string $version)` 50 | 51 | To simplify the parsing and building of versions, the `importString()` method 52 | exists for your convenience: 53 | 54 | ```php 55 | $builder->importString('1.0.0-alpha.1+2'); 56 | ``` 57 | 58 | This is the equivalent of doing the following: 59 | 60 | ```php 61 | $builder->importComponents( 62 | Herrera\Version\Parser::toComponents('1.0.0-alpha.1+2') 63 | ); 64 | ``` 65 | 66 | The caveat about missing version information applies. 67 | 68 | ### From an Existing Version 69 | 70 | - `Herrera\Version\Builder::importVersion(Herrera\Version\Version $version)` 71 | 72 | If you already have an instance of `Herrera\Version\Version`, you can import 73 | that directly to the builder: 74 | 75 | ```php 76 | $builder->importVersion($version); 77 | ``` 78 | 79 | Changing Version Information 80 | ---------------------------- 81 | 82 | - `Herrera\Version\Builder::setMajor(int $number)` 83 | - `Herrera\Version\Builder::setMinor(int $number)` 84 | - `Herrera\Version\Builder::setPatch(int $number)` 85 | - `Herrera\Version\Builder::setPreRelease(array $identifiers)` 86 | - `Herrera\Version\Builder::setBuild(array $identifiers)` 87 | 88 | You may use any of the above methods to directly alter the version information. 89 | Note that you may only use integer values for `setMajor()`, `setMinor()`, and 90 | `setPatch()`. See the documentation for [intval()][] to understand how string 91 | numbers are converted to integers. 92 | 93 | To automatically apply the logic defined in the specification, you may use: 94 | 95 | - `Herrera\Version\Builder::incrementMajor(int $amount = 1)` — increments 96 | the major version number by `$amount`, and resets the minor and patch version 97 | numbers to zero 98 | - `Herrera\Version\Builder incrementMinor(int $amount = 1)` — increments 99 | the minor version number by `$amount`, and resets the patch version number 100 | to zero 101 | 102 | For the sake of completeness and convenience, the method 103 | 104 | - `Herrera\Version\Builder::incrementPatch(int $amount = 1)` 105 | 106 | has been added. It will simply increment the patch version number by the 107 | `$amount` specified. 108 | 109 | [intval()]: http://php.net/intval 110 | 111 | ### Resetting Identifiers 112 | 113 | A couple of convenience methods are available for resetting the pre-release 114 | version identifiers and build metadata identifiers: 115 | 116 | ```php 117 | $builder->clearBuild(); 118 | 119 | // same as 120 | 121 | $builder->setBuild(array()); 122 | ``` 123 | 124 | ```php 125 | $builder->clearPreRelease(); 126 | 127 | // same as 128 | 129 | $builder->setPreRelease(array()); 130 | ``` 131 | 132 | Getting the Final Version 133 | ------------------------- 134 | 135 | Once you have built your final version number, you may retrieve an instance 136 | of the `Herrera\Version\Version` class. This class maintains a read-only copy 137 | of the version information. 138 | 139 | ```php 140 | $version = $builder->getVersion(); 141 | ``` 142 | -------------------------------------------------------------------------------- /doc/02-Comparing.md: -------------------------------------------------------------------------------- 1 | Comparing 2 | ========= 3 | 4 | The `Herrera\Version\Comparator` class will compare any two instances of 5 | `Herrera\Version\Version`, which also includes `Herrera\Version\Builder`. 6 | 7 | Simple Comparison 8 | ----------------- 9 | 10 | To perform a simple comparison, you may call the `compareTo()` method: 11 | 12 | ```php 13 | use Herrera\Version\Comparator; 14 | use Herrera\Version\Version; 15 | 16 | $left = new Version(1, 0, 0); 17 | $right = new Version(1, 0, 0); 18 | 19 | $result = Comparator::compareTo($left, $right); // returns "0" (zero) 20 | ``` 21 | 22 | You may use any of the following class constants to check the result: 23 | 24 | - `Comparator::EQUAL_TO` (0, zero) 25 | - `Comparator::GREATER_THAN` (1, one) 26 | - `Comparator::LESS_THAN` (-1, negative one) 27 | 28 | Equality 29 | -------- 30 | 31 | To check if two versions are equal, you may call the `isEqualTo()` method: 32 | 33 | ```php 34 | $result = Comparator::isEqualTo($left, $right); // returns "true" 35 | ``` 36 | 37 | Greater Than 38 | --------- 39 | 40 | To check if one version is greater than another, you may call the 41 | `isGreaterThan()` method. The method will check of the first version (`$left`) 42 | is greater than the second (`$right`): 43 | 44 | ```php 45 | $left = new Version(2, 0, 0); 46 | $right = new Version(1, 0, 0); 47 | 48 | $result = Comparator::isGreaterThan($left, $right); // returns "true" 49 | ``` 50 | 51 | Less Than 52 | --------- 53 | 54 | To check if one version is less than another, you may call the `isGreaterThan()` 55 | method. The method will check of the first version (`$left`) is less than the 56 | second (`$right`): 57 | 58 | ```php 59 | $left = new Version(1, 0, 0); 60 | $right = new Version(2, 0, 0); 61 | 62 | $result = Comparator::isLessThan($left, $right); // returns "true" 63 | ``` 64 | -------------------------------------------------------------------------------- /doc/03-Dumping.md: -------------------------------------------------------------------------------- 1 | Dumping 2 | ======= 3 | 4 | To dump a `Herrera\Version\Version` instance as a string, you will need to use 5 | the `Herrera\Version\Dumper::toString()` method: 6 | 7 | ```php 8 | use Herrera\Version\Dumper; 9 | use Herrera\Version\Version; 10 | 11 | $version = new Version( 12 | 1, 13 | 2, 14 | 3, 15 | array('alpha', '1'), 16 | array('2') 17 | ); 18 | 19 | echo Dumper::toString($version); // echoes "1.2.3-alpha.1+2" 20 | ``` 21 | 22 | If you need the version data as a simple array, you may use the `toComponents()` 23 | method: 24 | 25 | ```php 26 | use Herrera\Version\Parser; 27 | 28 | $components = Dumper::toComponents($version); 29 | 30 | // is the equivalent to: 31 | 32 | $components = array( 33 | Parser::MAJOR => 1, 34 | Parser::MINOR => 2, 35 | Parser::PATCH => 3, 36 | Parser::PRE_RELEASE => array('alpha', '1'), 37 | Parser::BUILD => array('2') 38 | ); 39 | ``` 40 | -------------------------------------------------------------------------------- /doc/04-Parsing.md: -------------------------------------------------------------------------------- 1 | Parsing 2 | ======= 3 | 4 | There are three ways of parsing version information with the 5 | `Herrera\Version\Parser` class. 6 | 7 | String to Builder 8 | ---------------- 9 | 10 | - `Herrera\Version\Builder toBuilder(str $version)` 11 | 12 | The `toBuilder()` method will return a version builder for the given string 13 | representation: 14 | 15 | ```php 16 | use Herrera\Version\Parser; 17 | 18 | $builder = Parser::toBuilder('1.0.0-alpha.1+2'); 19 | ``` 20 | 21 | String to Components 22 | -------------------- 23 | 24 | If you just need the version string broken down into its components, you may 25 | use the `toComponenets()` method: 26 | 27 | ```php 28 | $componenets = Parser::toComponents('1.0.0-alpha.1+2'); 29 | 30 | // is the equivalent to: 31 | 32 | $components = array( 33 | Parser::MAJOR => 1, 34 | Parser::MINOR => 0, 35 | Parser::PATCH => 0, 36 | Parser::PRE_RELEASE => array('alpha', '1'), 37 | Parser::BUILD => array('2') 38 | ); 39 | ``` 40 | 41 | String to Version 42 | ----------------- 43 | 44 | If you simply need an instance of `Herrera\Version\Version`, you may use the 45 | `toVersion()` method: 46 | 47 | ```php 48 | $version = Parser::toVersion('1.0.0-alpha.1+2'); 49 | ``` 50 | -------------------------------------------------------------------------------- /doc/05-Validating.md: -------------------------------------------------------------------------------- 1 | Validating 2 | ========== 3 | 4 | The `Herrera\Version\Validator` class is meant to be used indirectly through 5 | the other classes. However, you may use it to validate version information 6 | according to the Semantic Versioning specification: 7 | 8 | - `bool isIdentifier(str $identifier)` — returns `true` if the identifier 9 | is valid, false if not. An identifier may belong to either a pre-release 10 | version number or build metadata 11 | - `bool isNumber(int|str $number)` — returns `true` if the version 12 | number is valid, or false if not 13 | - `bool isVersion(str $version)` — returns `true` if the string is a 14 | valid version string representation, `false` if not 15 | -------------------------------------------------------------------------------- /phpunit.xml.dist: -------------------------------------------------------------------------------- 1 | 2 | 12 | 13 | 14 | src/lib/ 15 | 16 | 17 | 18 | 19 | src/tests/ 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /src/lib/Herrera/Version/Builder.php: -------------------------------------------------------------------------------- 1 | 12 | */ 13 | class Builder extends Version 14 | { 15 | /** 16 | * Removes the build metadata identifiers. 17 | * 18 | * @return Builder The Version builder. 19 | */ 20 | public function clearBuild() 21 | { 22 | return $this->build = array(); 23 | } 24 | 25 | /** 26 | * Removes the pre-release version identifiers. 27 | * 28 | * @return Builder The Version builder. 29 | */ 30 | public function clearPreRelease() 31 | { 32 | $this->preRelease = array(); 33 | } 34 | 35 | /** 36 | * Creates a new Version builder. 37 | * 38 | * @return Builder The Version builder. 39 | */ 40 | public static function create() 41 | { 42 | return new Builder(); 43 | } 44 | 45 | /** 46 | * Returns a readonly Version instance. 47 | * 48 | * @return Version The readonly Version instance. 49 | */ 50 | public function getVersion() 51 | { 52 | return new Version( 53 | $this->major, 54 | $this->minor, 55 | $this->patch, 56 | $this->preRelease, 57 | $this->build 58 | ); 59 | } 60 | 61 | /** 62 | * Imports the version components. 63 | * 64 | * @param array $components The components. 65 | * 66 | * @return Builder The Version builder. 67 | */ 68 | public function importComponents(array $components) 69 | { 70 | if (isset($components[Parser::BUILD])) { 71 | $this->build = $components[Parser::BUILD]; 72 | } else { 73 | $this->build = array(); 74 | } 75 | 76 | if (isset($components[Parser::MAJOR])) { 77 | $this->major = $components[Parser::MAJOR]; 78 | } else { 79 | $this->major = 0; 80 | } 81 | 82 | if (isset($components[Parser::MINOR])) { 83 | $this->minor = $components[Parser::MINOR]; 84 | } else { 85 | $this->minor = 0; 86 | } 87 | 88 | if (isset($components[Parser::PATCH])) { 89 | $this->patch = $components[Parser::PATCH]; 90 | } else { 91 | $this->patch = 0; 92 | } 93 | 94 | if (isset($components[Parser::PRE_RELEASE])) { 95 | $this->preRelease = $components[Parser::PRE_RELEASE]; 96 | } else { 97 | $this->preRelease = array(); 98 | } 99 | 100 | return $this; 101 | } 102 | 103 | /** 104 | * Imports the version string representation. 105 | * 106 | * @param string $version The string representation. 107 | * 108 | * @return Builder The Version builder. 109 | */ 110 | public function importString($version) 111 | { 112 | return $this->importComponents(Parser::toComponents($version)); 113 | } 114 | 115 | /** 116 | * Imports an existing Version instance. 117 | * 118 | * @param Version $version A Version instance. 119 | * 120 | * @return Builder The Version builder. 121 | */ 122 | public function importVersion($version) 123 | { 124 | return $this 125 | ->setMajor($version->getMajor()) 126 | ->setMinor($version->getMinor()) 127 | ->setPatch($version->getPatch()) 128 | ->setPreRelease($version->getPreRelease()) 129 | ->setBuild($version->getBuild()); 130 | } 131 | 132 | /** 133 | * Increments the major version number and resets the minor and patch 134 | * version numbers to zero. 135 | * 136 | * @param integer $amount Increment by what amount? 137 | * 138 | * @return Builder The Version builder. 139 | */ 140 | public function incrementMajor($amount = 1) 141 | { 142 | $this->major += $amount; 143 | $this->minor = 0; 144 | $this->patch = 0; 145 | 146 | return $this; 147 | } 148 | 149 | /** 150 | * Increments the minor version number and resets the patch version number 151 | * to zero. 152 | * 153 | * @param integer $amount Increment by what amount? 154 | * 155 | * @return Builder The Version builder. 156 | */ 157 | public function incrementMinor($amount = 1) 158 | { 159 | $this->minor += $amount; 160 | $this->patch = 0; 161 | 162 | return $this; 163 | } 164 | 165 | /** 166 | * Increments the patch version number. 167 | * 168 | * @param integer $amount Increment by what amount? 169 | * 170 | * @return Builder The Version builder. 171 | */ 172 | public function incrementPatch($amount = 1) 173 | { 174 | $this->patch += $amount; 175 | 176 | return $this; 177 | } 178 | 179 | /** 180 | * Sets the build metadata identifiers. 181 | * 182 | * @param array $identifiers The build metadata identifiers. 183 | * 184 | * @return Builder The Version builder. 185 | * 186 | * @throws InvalidIdentifierException If an identifier is invalid. 187 | */ 188 | public function setBuild(array $identifiers) 189 | { 190 | foreach ($identifiers as $identifier) { 191 | if (!Validator::isIdentifier($identifier)) { 192 | throw new InvalidIdentifierException($identifier); 193 | } 194 | } 195 | 196 | $this->build = $identifiers; 197 | 198 | return $this; 199 | } 200 | 201 | /** 202 | * Sets the major version number. 203 | * 204 | * @param integer $number The major version number. 205 | * 206 | * @return Builder The Version builder. 207 | * 208 | * @throws InvalidNumberException If the number is invalid. 209 | */ 210 | public function setMajor($number) 211 | { 212 | if (!Validator::isNumber($number)) { 213 | throw new InvalidNumberException($number); 214 | } 215 | 216 | $this->major = intval($number); 217 | 218 | return $this; 219 | } 220 | 221 | /** 222 | * Sets the minor version number. 223 | * 224 | * @param integer $number The minor version number. 225 | * 226 | * @return Builder The Version builder. 227 | * 228 | * @throws InvalidNumberException If the number is invalid. 229 | */ 230 | public function setMinor($number) 231 | { 232 | if (!Validator::isNumber($number)) { 233 | throw new InvalidNumberException($number); 234 | } 235 | 236 | $this->minor = intval($number); 237 | 238 | return $this; 239 | } 240 | 241 | /** 242 | * Sets the patch version number. 243 | * 244 | * @param integer $number The patch version number. 245 | * 246 | * @return Builder The Version builder. 247 | * 248 | * @throws InvalidNumberException If the number is invalid. 249 | */ 250 | public function setPatch($number) 251 | { 252 | if (!Validator::isNumber($number)) { 253 | throw new InvalidNumberException($number); 254 | } 255 | 256 | $this->patch = intval($number); 257 | 258 | return $this; 259 | } 260 | 261 | /** 262 | * Sets the pre-release version identifiers. 263 | * 264 | * @param array $identifiers The pre-release version identifiers. 265 | * 266 | * @return Builder The Version builder. 267 | * 268 | * @throws InvalidIdentifierException If an identifier is invalid. 269 | */ 270 | public function setPreRelease(array $identifiers) 271 | { 272 | foreach ($identifiers as $identifier) { 273 | if (!Validator::isIdentifier($identifier)) { 274 | throw new InvalidIdentifierException($identifier); 275 | } 276 | } 277 | 278 | $this->preRelease = $identifiers; 279 | 280 | return $this; 281 | } 282 | } 283 | -------------------------------------------------------------------------------- /src/lib/Herrera/Version/Comparator.php: -------------------------------------------------------------------------------- 1 | 9 | */ 10 | class Comparator 11 | { 12 | /** 13 | * The version is equal to another. 14 | */ 15 | const EQUAL_TO = 0; 16 | 17 | /** 18 | * The version is greater than another. 19 | */ 20 | const GREATER_THAN = 1; 21 | 22 | /** 23 | * The version is less than another. 24 | */ 25 | const LESS_THAN = -1; 26 | 27 | /** 28 | * Compares one version with another. 29 | * 30 | * @param Version $left The left version to compare. 31 | * @param Version $right The right version to compare. 32 | * 33 | * @return integer Returns Comparator::EQUAL_TO if the two versions are 34 | * equal. If the left version is less than the right 35 | * version, Comparator::LESS_THAN is returned. If the left 36 | * version is greater than the right version, 37 | * Comparator::GREATER_THAN is returned. 38 | */ 39 | public static function compareTo(Version $left, Version $right) 40 | { 41 | switch (true) { 42 | case ($left->getMajor() < $right->getMajor()): 43 | return self::LESS_THAN; 44 | case ($left->getMajor() > $right->getMajor()): 45 | return self::GREATER_THAN; 46 | case ($left->getMinor() > $right->getMinor()): 47 | return self::GREATER_THAN; 48 | case ($left->getMinor() < $right->getMinor()): 49 | return self::LESS_THAN; 50 | case ($left->getPatch() > $right->getPatch()): 51 | return self::GREATER_THAN; 52 | case ($left->getPatch() < $right->getPatch()): 53 | return self::LESS_THAN; 54 | // @codeCoverageIgnoreStart 55 | } 56 | // @codeCoverageIgnoreEnd 57 | 58 | return self::compareIdentifiers( 59 | $left->getPreRelease(), 60 | $right->getPreRelease() 61 | ); 62 | } 63 | 64 | /** 65 | * Checks if the left version is equal to the right. 66 | * 67 | * @param Version $left The left version to compare. 68 | * @param Version $right The right version to compare. 69 | * 70 | * @return boolean TRUE if the left version is equal to the right, FALSE 71 | * if not. 72 | */ 73 | public static function isEqualTo(Version $left, Version $right) 74 | { 75 | return (self::EQUAL_TO === self::compareTo($left, $right)); 76 | } 77 | 78 | /** 79 | * Checks if the left version is greater than the right. 80 | * 81 | * @param Version $left The left version to compare. 82 | * @param Version $right The right version to compare. 83 | * 84 | * @return boolean TRUE if the left version is greater than the right, 85 | * FALSE if not. 86 | */ 87 | public static function isGreaterThan(Version $left, Version $right) 88 | { 89 | return (self::GREATER_THAN === self::compareTo($left, $right)); 90 | } 91 | 92 | /** 93 | * Checks if the left version is less than the right. 94 | * 95 | * @param Version $left The left version to compare. 96 | * @param Version $right The right version to compare. 97 | * 98 | * @return boolean TRUE if the left version is less than the right, 99 | * FALSE if not. 100 | */ 101 | public static function isLessThan(Version $left, Version $right) 102 | { 103 | return (self::LESS_THAN === self::compareTo($left, $right)); 104 | } 105 | 106 | /** 107 | * Compares the identifier components of the left and right versions. 108 | * 109 | * @param array $left The left identifiers. 110 | * @param array $right The right identifiers. 111 | * 112 | * @return integer Returns Comparator::EQUAL_TO if the two identifiers are 113 | * equal. If the left identifiers is less than the right 114 | * identifiers, Comparator::LESS_THAN is returned. If the 115 | * left identifiers is greater than the right identifiers, 116 | * Comparator::GREATER_THAN is returned. 117 | */ 118 | public static function compareIdentifiers(array $left, array $right) 119 | { 120 | if ($left && empty($right)) { 121 | return self::LESS_THAN; 122 | } elseif (empty($left) && $right) { 123 | return self::GREATER_THAN; 124 | } 125 | 126 | $l = $left; 127 | $r = $right; 128 | $x = self::GREATER_THAN; 129 | $y = self::LESS_THAN; 130 | 131 | if (count($l) < count($r)) { 132 | $l = $right; 133 | $r = $left; 134 | $x = self::LESS_THAN; 135 | $y = self::GREATER_THAN; 136 | } 137 | 138 | foreach (array_keys($l) as $i) { 139 | if (!isset($r[$i])) { 140 | return $x; 141 | } 142 | 143 | if ($l[$i] === $r[$i]) { 144 | continue; 145 | } 146 | 147 | if (true === ($li = (false != preg_match('/^\d+$/', $l[$i])))) { 148 | $l[$i] = intval($l[$i]); 149 | } 150 | 151 | if (true === ($ri = (false != preg_match('/^\d+$/', $r[$i])))) { 152 | $r[$i] = intval($r[$i]); 153 | } 154 | 155 | if ($li && $ri) { 156 | return ($l[$i] > $r[$i]) ? $x : $y; 157 | } elseif (!$li && $ri) { 158 | return $x; 159 | } elseif ($li && !$ri) { 160 | return $y; 161 | } 162 | 163 | $result = strcmp($l[$i], $r[$i]); 164 | 165 | if ($result > 0) { 166 | return $x; 167 | } elseif ($result < 0) { 168 | return $y; 169 | } 170 | } 171 | 172 | return self::EQUAL_TO; 173 | } 174 | } 175 | -------------------------------------------------------------------------------- /src/lib/Herrera/Version/Dumper.php: -------------------------------------------------------------------------------- 1 | 9 | */ 10 | class Dumper 11 | { 12 | /** 13 | * Returns the components of a Version instance. 14 | * 15 | * @param Version $version A version. 16 | * 17 | * @return array The components. 18 | */ 19 | public static function toComponents(Version $version) 20 | { 21 | return array( 22 | Parser::MAJOR => $version->getMajor(), 23 | Parser::MINOR => $version->getMinor(), 24 | Parser::PATCH => $version->getPatch(), 25 | Parser::PRE_RELEASE => $version->getPreRelease(), 26 | Parser::BUILD => $version->getBuild() 27 | ); 28 | } 29 | 30 | /** 31 | * Returns the string representation of a Version instance. 32 | * 33 | * @param Version $version A version. 34 | * 35 | * @return string The string representation. 36 | */ 37 | public static function toString(Version $version) 38 | { 39 | return sprintf( 40 | '%d.%d.%d%s%s', 41 | $version->getMajor(), 42 | $version->getMinor(), 43 | $version->getPatch(), 44 | $version->getPreRelease() 45 | ? '-' . join('.', $version->getPreRelease()) 46 | : '', 47 | $version->getBuild() 48 | ? '+' . join('.', $version->getBuild()) 49 | : '' 50 | ); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/lib/Herrera/Version/Exception/InvalidIdentifierException.php: -------------------------------------------------------------------------------- 1 | 9 | */ 10 | class InvalidIdentifierException extends VersionException 11 | { 12 | /** 13 | * The invalid identifier. 14 | * 15 | * @var string 16 | */ 17 | private $identifier; 18 | 19 | /** 20 | * Sets the invalid identifier. 21 | * 22 | * @param string $identifier The invalid identifier. 23 | */ 24 | public function __construct($identifier) 25 | { 26 | parent::__construct( 27 | sprintf( 28 | 'The identifier "%s" is invalid.', 29 | $identifier 30 | ) 31 | ); 32 | 33 | $this->identifier = $identifier; 34 | } 35 | 36 | /** 37 | * Returns the invalid identifier. 38 | * 39 | * @return string The invalid identifier. 40 | */ 41 | public function getIdentifier() 42 | { 43 | return $this->identifier; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/lib/Herrera/Version/Exception/InvalidNumberException.php: -------------------------------------------------------------------------------- 1 | 9 | */ 10 | class InvalidNumberException extends VersionException 11 | { 12 | /** 13 | * The invalid version number. 14 | * 15 | * @var mixed 16 | */ 17 | private $number; 18 | 19 | /** 20 | * Sets the invalid version number. 21 | * 22 | * @param mixed $number The invalid version number. 23 | */ 24 | public function __construct($number) 25 | { 26 | parent::__construct( 27 | sprintf( 28 | 'The version number "%s" is invalid.', 29 | $number 30 | ) 31 | ); 32 | 33 | $this->number = $number; 34 | } 35 | 36 | /** 37 | * Returns the invalid version number. 38 | * 39 | * @return mixed The invalid version number. 40 | */ 41 | public function getNumber() 42 | { 43 | return $this->number; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/lib/Herrera/Version/Exception/InvalidStringRepresentationException.php: -------------------------------------------------------------------------------- 1 | 9 | */ 10 | class InvalidStringRepresentationException extends VersionException 11 | { 12 | /** 13 | * The invalid string representation. 14 | * 15 | * @var string 16 | */ 17 | private $version; 18 | 19 | /** 20 | * Sets the invalid string representation. 21 | * 22 | * @param string $version The string representation. 23 | */ 24 | public function __construct($version) 25 | { 26 | parent::__construct( 27 | sprintf( 28 | 'The version string representation "%s" is invalid.', 29 | $version 30 | ) 31 | ); 32 | 33 | $this->version = $version; 34 | } 35 | 36 | /** 37 | * Returns the invalid string representation. 38 | * 39 | * @return string The invalid string representation. 40 | */ 41 | public function getVersion() 42 | { 43 | return $this->version; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/lib/Herrera/Version/Exception/VersionException.php: -------------------------------------------------------------------------------- 1 | 11 | */ 12 | class VersionException extends Exception 13 | { 14 | } 15 | -------------------------------------------------------------------------------- /src/lib/Herrera/Version/Parser.php: -------------------------------------------------------------------------------- 1 | 11 | */ 12 | class Parser 13 | { 14 | /** 15 | * The build metadata component. 16 | */ 17 | const BUILD = 'build'; 18 | 19 | /** 20 | * The major version number component. 21 | */ 22 | const MAJOR = 'major'; 23 | 24 | /** 25 | * The minor version number component. 26 | */ 27 | const MINOR = 'minor'; 28 | 29 | /** 30 | * The patch version number component. 31 | */ 32 | const PATCH = 'patch'; 33 | 34 | /** 35 | * The pre-release version number component. 36 | */ 37 | const PRE_RELEASE = 'pre'; 38 | 39 | /** 40 | * Returns a Version builder for the string representation. 41 | * 42 | * @param string $version The string representation. 43 | * 44 | * @return Builder A Version builder. 45 | */ 46 | public static function toBuilder($version) 47 | { 48 | return Builder::create()->importComponents( 49 | self::toComponents($version) 50 | ); 51 | } 52 | 53 | /** 54 | * Returns the components of the string representation. 55 | * 56 | * @param string $version The string representation. 57 | * 58 | * @return array The components of the version. 59 | * 60 | * @throws InvalidStringRepresentationException If the string representation 61 | * is invalid. 62 | */ 63 | public static function toComponents($version) 64 | { 65 | if (!Validator::isVersion($version)) { 66 | throw new InvalidStringRepresentationException($version); 67 | } 68 | 69 | if (false !== strpos($version, '+')) { 70 | list($version, $build) = explode('+', $version); 71 | 72 | $build = explode('.', $build); 73 | } 74 | 75 | if (false !== strpos($version, '-')) { 76 | list($version, $pre) = explode('-', $version); 77 | 78 | $pre = explode('.', $pre); 79 | } 80 | 81 | list( 82 | $major, 83 | $minor, 84 | $patch 85 | ) = explode('.', $version); 86 | 87 | return array( 88 | self::MAJOR => intval($major), 89 | self::MINOR => intval($minor), 90 | self::PATCH => intval($patch), 91 | self::PRE_RELEASE => isset($pre) ? $pre : array(), 92 | self::BUILD => isset($build) ? $build : array(), 93 | ); 94 | } 95 | 96 | /** 97 | * Returns a Version instance for the string representation. 98 | * 99 | * @param string $version The string representation. 100 | * 101 | * @return Version A Version instance. 102 | */ 103 | public static function toVersion($version) 104 | { 105 | $components = self::toComponents($version); 106 | 107 | return new Version( 108 | $components['major'], 109 | $components['minor'], 110 | $components['patch'], 111 | $components['pre'], 112 | $components['build'] 113 | ); 114 | } 115 | } 116 | -------------------------------------------------------------------------------- /src/lib/Herrera/Version/Validator.php: -------------------------------------------------------------------------------- 1 | 9 | */ 10 | class Validator 11 | { 12 | /** 13 | * The regular expression for a valid identifier. 14 | */ 15 | const IDENTIFIER_REGEX = '/^[0-9A-Za-z\-]+$/'; 16 | 17 | /** 18 | * The regular expression for a valid semantic version number. 19 | */ 20 | const VERSION_REGEX = '/^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?(?:\+([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?$/'; 21 | 22 | /** 23 | * Checks if a identifier is valid. 24 | * 25 | * @param string $identifier A identifier. 26 | * 27 | * @return boolean TRUE if the identifier is valid, FALSE If not. 28 | */ 29 | public static function isIdentifier($identifier) 30 | { 31 | return (true == preg_match(self::IDENTIFIER_REGEX, $identifier)); 32 | } 33 | 34 | /** 35 | * Checks if a number is a valid version number. 36 | * 37 | * @param integer $number A number. 38 | * 39 | * @return boolean TRUE if the number is valid, FALSE If not. 40 | */ 41 | public static function isNumber($number) 42 | { 43 | return (true == preg_match('/^(0|[1-9]\d*)$/', $number)); 44 | } 45 | 46 | /** 47 | * Checks if the string representation of a version number is valid. 48 | * 49 | * @param string $version The string representation. 50 | * 51 | * @return boolean TRUE if the string representation is valid, FALSE if not. 52 | */ 53 | public static function isVersion($version) 54 | { 55 | return (true == preg_match(self::VERSION_REGEX, $version)); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/lib/Herrera/Version/Version.php: -------------------------------------------------------------------------------- 1 | 9 | */ 10 | class Version 11 | { 12 | /** 13 | * The build metadata identifiers. 14 | * 15 | * @var array 16 | */ 17 | protected $build; 18 | 19 | /** 20 | * The major version number. 21 | * 22 | * @var integer 23 | */ 24 | protected $major; 25 | 26 | /** 27 | * The minor version number. 28 | * 29 | * @var integer 30 | */ 31 | protected $minor; 32 | 33 | /** 34 | * The patch version number. 35 | * 36 | * @var integer 37 | */ 38 | protected $patch; 39 | 40 | /** 41 | * The pre-release version identifiers. 42 | * 43 | * @var array 44 | */ 45 | protected $preRelease; 46 | 47 | /** 48 | * Sets the version information. 49 | * 50 | * @param integer $major The major version number. 51 | * @param integer $minor The minor version number. 52 | * @param integer $patch The patch version number. 53 | * @param array $pre The pre-release version identifiers. 54 | * @param array $build The build metadata identifiers. 55 | */ 56 | public function __construct( 57 | $major = 0, 58 | $minor = 0, 59 | $patch = 0, 60 | array $pre = array(), 61 | array $build = array() 62 | ) { 63 | $this->build = $build; 64 | $this->major = $major; 65 | $this->minor = $minor; 66 | $this->patch = $patch; 67 | $this->preRelease = $pre; 68 | } 69 | 70 | /** 71 | * Returns the build metadata identifiers. 72 | * 73 | * @return array The build metadata identifiers. 74 | */ 75 | public function getBuild() 76 | { 77 | return $this->build; 78 | } 79 | 80 | /** 81 | * Returns the major version number. 82 | * 83 | * @return integer The major version number. 84 | */ 85 | public function getMajor() 86 | { 87 | return $this->major; 88 | } 89 | 90 | /** 91 | * Returns the minor version number. 92 | * 93 | * @return integer The minor version number. 94 | */ 95 | public function getMinor() 96 | { 97 | return $this->minor; 98 | } 99 | 100 | /** 101 | * Returns the patch version number. 102 | * 103 | * @return integer The patch version number. 104 | */ 105 | public function getPatch() 106 | { 107 | return $this->patch; 108 | } 109 | 110 | /** 111 | * Returns the pre-release version identifiers. 112 | * 113 | * @return array The pre-release version identifiers. 114 | */ 115 | public function getPreRelease() 116 | { 117 | return $this->preRelease; 118 | } 119 | 120 | /** 121 | * Checks if the version number is stable. 122 | * 123 | * @return boolean TRUE if it is stable, FALSE if not. 124 | */ 125 | public function isStable() 126 | { 127 | return empty($this->preRelease) && $this->major !== 0; 128 | } 129 | 130 | /** 131 | * Returns string representation. 132 | * 133 | * @return string The string representation. 134 | */ 135 | public function __toString() 136 | { 137 | return Dumper::toString($this); 138 | } 139 | } 140 | -------------------------------------------------------------------------------- /src/tests/lib/Herrera/Version/Tests/BuilderTest.php: -------------------------------------------------------------------------------- 1 | setPropertyValue($this->builder, 'build', array('build', '1')); 20 | 21 | $this->builder->clearBuild(); 22 | 23 | $this->assertSame(array(), $this->builder->getBuild()); 24 | } 25 | 26 | public function testClearPreRelease() 27 | { 28 | $this->setPropertyValue($this->builder, 'preRelease', array('pre', '1')); 29 | 30 | $this->builder->clearPreRelease(); 31 | 32 | $this->assertSame(array(), $this->builder->getPreRelease()); 33 | } 34 | 35 | public function testCreate() 36 | { 37 | $this->assertInstanceOf('Herrera\\Version\\Version', Builder::create()); 38 | } 39 | 40 | public function testGetVersion() 41 | { 42 | $version = $this->builder->getVersion(); 43 | 44 | $this->assertInstanceOf('Herrera\\Version\\Version', $version); 45 | $this->assertSame(array(), $version->getBuild()); 46 | $this->assertSame(0, $version->getMajor()); 47 | $this->assertSame(0, $version->getMinor()); 48 | $this->assertSame(0, $version->getPatch()); 49 | $this->assertSame(array(), $version->getPreRelease()); 50 | } 51 | 52 | public function testImportComponents() 53 | { 54 | $this->assertSame( 55 | $this->builder, 56 | $this->builder->importComponents(array()) 57 | ); 58 | 59 | $this->assertSame(array(), $this->builder->getBuild()); 60 | $this->assertSame(0, $this->builder->getMajor()); 61 | $this->assertSame(0, $this->builder->getMinor()); 62 | $this->assertSame(0, $this->builder->getPatch()); 63 | $this->assertSame(array(), $this->builder->getPreRelease()); 64 | } 65 | 66 | public function testImportComponentsWithValues() 67 | { 68 | $this->assertSame( 69 | $this->builder, 70 | $this 71 | ->builder 72 | ->importComponents( 73 | array( 74 | Parser::MAJOR => 5, 75 | Parser::MINOR => 6, 76 | Parser::PATCH => 7, 77 | Parser::PRE_RELEASE => array('pre', '2', '3'), 78 | Parser::BUILD => array('build', '2', '3'), 79 | ) 80 | ) 81 | ); 82 | 83 | $this->assertSame(array('build', '2', '3'), $this->builder->getBuild()); 84 | $this->assertSame(5, $this->builder->getMajor()); 85 | $this->assertSame(6, $this->builder->getMinor()); 86 | $this->assertSame(7, $this->builder->getPatch()); 87 | $this->assertSame( 88 | array('pre', '2', '3'), 89 | $this->builder->getPreRelease() 90 | ); 91 | } 92 | 93 | public function testImportString() 94 | { 95 | $this->assertSame( 96 | $this->builder, 97 | $this->builder->importString('5.6.7-pre.2.3+build.2.3') 98 | ); 99 | 100 | $this->assertSame(array('build', '2', '3'), $this->builder->getBuild()); 101 | $this->assertSame(5, $this->builder->getMajor()); 102 | $this->assertSame(6, $this->builder->getMinor()); 103 | $this->assertSame(7, $this->builder->getPatch()); 104 | $this->assertSame( 105 | array('pre', '2', '3'), 106 | $this->builder->getPreRelease() 107 | ); 108 | } 109 | 110 | public function testImportVersion() 111 | { 112 | $this->assertSame( 113 | $this->builder, 114 | $this->builder->importVersion( 115 | new Version( 116 | 5, 117 | 6, 118 | 7, 119 | array('pre', '2', '3'), 120 | array('build', '2', '3') 121 | ) 122 | ) 123 | ); 124 | 125 | $this->assertSame(array('build', '2', '3'), $this->builder->getBuild()); 126 | $this->assertSame(5, $this->builder->getMajor()); 127 | $this->assertSame(6, $this->builder->getMinor()); 128 | $this->assertSame(7, $this->builder->getPatch()); 129 | $this->assertSame( 130 | array('pre', '2', '3'), 131 | $this->builder->getPreRelease() 132 | ); 133 | } 134 | 135 | public function testIncrementMajor() 136 | { 137 | $this->setPropertyValue($this->builder, 'major', 1); 138 | $this->setPropertyValue($this->builder, 'minor', 2); 139 | $this->setPropertyValue($this->builder, 'patch', 3); 140 | 141 | $this->assertSame($this->builder, $this->builder->incrementMajor()); 142 | $this->assertSame(2, $this->builder->getMajor()); 143 | $this->assertSame(0, $this->builder->getMinor()); 144 | $this->assertSame(0, $this->builder->getPatch()); 145 | } 146 | 147 | public function testIncrementMajorWithValue() 148 | { 149 | $this->setPropertyValue($this->builder, 'major', 1); 150 | $this->setPropertyValue($this->builder, 'minor', 2); 151 | $this->setPropertyValue($this->builder, 'patch', 3); 152 | 153 | $this->assertSame($this->builder, $this->builder->incrementMajor(4)); 154 | $this->assertSame(5, $this->builder->getMajor()); 155 | $this->assertSame(0, $this->builder->getMinor()); 156 | $this->assertSame(0, $this->builder->getPatch()); 157 | } 158 | 159 | public function testIncrementMinor() 160 | { 161 | $this->setPropertyValue($this->builder, 'minor', 2); 162 | $this->setPropertyValue($this->builder, 'patch', 3); 163 | 164 | $this->assertSame($this->builder, $this->builder->incrementMinor()); 165 | $this->assertSame(3, $this->builder->getMinor()); 166 | $this->assertSame(0, $this->builder->getPatch()); 167 | } 168 | 169 | public function testIncrementMinorWithValue() 170 | { 171 | $this->setPropertyValue($this->builder, 'minor', 2); 172 | $this->setPropertyValue($this->builder, 'patch', 3); 173 | 174 | $this->assertSame($this->builder, $this->builder->incrementMinor(4)); 175 | $this->assertSame(6, $this->builder->getMinor()); 176 | $this->assertSame(0, $this->builder->getPatch()); 177 | } 178 | 179 | public function testIncrementPatch() 180 | { 181 | $this->setPropertyValue($this->builder, 'patch', 3); 182 | 183 | $this->assertSame($this->builder, $this->builder->incrementPatch()); 184 | $this->assertSame(4, $this->builder->getPatch()); 185 | } 186 | 187 | public function testIncrementMinorWithPatch() 188 | { 189 | $this->setPropertyValue($this->builder, 'patch', 3); 190 | 191 | $this->assertSame($this->builder, $this->builder->incrementPatch(4)); 192 | $this->assertSame(7, $this->builder->getPatch()); 193 | } 194 | 195 | public function testSetBuild() 196 | { 197 | $build = array('build', '1'); 198 | 199 | $this->assertSame($this->builder, $this->builder->setBuild($build)); 200 | 201 | $this->assertEquals($build, $this->builder->getBuild()); 202 | } 203 | 204 | public function testSetBuildInvalid() 205 | { 206 | $this->setExpectedException( 207 | 'Herrera\\Version\\Exception\\InvalidIdentifierException', 208 | 'The identifier "+" is invalid.' 209 | ); 210 | 211 | $this->builder->setBuild(array('+')); 212 | } 213 | 214 | public function testSetMajor() 215 | { 216 | $this->assertSame($this->builder, $this->builder->setMajor(123)); 217 | 218 | $this->assertSame(123, $this->builder->getMajor()); 219 | } 220 | 221 | public function testSetMajorInvalid() 222 | { 223 | $this->setExpectedException( 224 | 'Herrera\\Version\\Exception\\InvalidNumberException', 225 | 'The version number "x" is invalid.' 226 | ); 227 | 228 | $this->builder->setMajor('x'); 229 | } 230 | 231 | public function testSetMinor() 232 | { 233 | $this->assertSame($this->builder, $this->builder->setMinor(123)); 234 | 235 | $this->assertSame(123, $this->builder->getMinor()); 236 | } 237 | 238 | public function testSetMinorInvalid() 239 | { 240 | $this->setExpectedException( 241 | 'Herrera\\Version\\Exception\\InvalidNumberException', 242 | 'The version number "x" is invalid.' 243 | ); 244 | 245 | $this->builder->setMinor('x'); 246 | } 247 | 248 | public function testSetPatch() 249 | { 250 | $this->assertSame($this->builder, $this->builder->setPatch(123)); 251 | 252 | $this->assertSame(123, $this->builder->getPatch()); 253 | } 254 | 255 | public function testSetPatchInvalid() 256 | { 257 | $this->setExpectedException( 258 | 'Herrera\\Version\\Exception\\InvalidNumberException', 259 | 'The version number "x" is invalid.' 260 | ); 261 | 262 | $this->builder->setPatch('x'); 263 | } 264 | 265 | public function testSetPreRelease() 266 | { 267 | $pre = array('pre', '1'); 268 | 269 | $this->assertSame($this->builder, $this->builder->setPreRelease($pre)); 270 | 271 | $this->assertEquals($pre, $this->builder->getPreRelease()); 272 | } 273 | 274 | public function testSetPreReleaseInvalid() 275 | { 276 | $this->setExpectedException( 277 | 'Herrera\\Version\\Exception\\InvalidIdentifierException', 278 | 'The identifier "+" is invalid.' 279 | ); 280 | 281 | $this->builder->setPreRelease(array('+')); 282 | } 283 | 284 | protected function setUp() 285 | { 286 | $this->builder = new Builder(); 287 | } 288 | } 289 | -------------------------------------------------------------------------------- /src/tests/lib/Herrera/Version/Tests/ComparatorTest.php: -------------------------------------------------------------------------------- 1 | getEqual(), 15 | $this->getGreater(), 16 | $this->getLess() 17 | ); 18 | } 19 | 20 | public function getEqual() 21 | { 22 | return array( 23 | array('0.0.0', '0.0.0', Comparator::EQUAL_TO), 24 | array('0.0.1', '0.0.1', Comparator::EQUAL_TO), 25 | array('0.1.0', '0.1.0', Comparator::EQUAL_TO), 26 | array('1.0.0', '1.0.0', Comparator::EQUAL_TO), 27 | array('1.1.1', '1.1.1', Comparator::EQUAL_TO), 28 | 29 | array('0.0.0-0', '0.0.0-0', Comparator::EQUAL_TO), 30 | array('0.0.0+0', '0.0.0+1', Comparator::EQUAL_TO), 31 | ); 32 | } 33 | 34 | public function getGreater() 35 | { 36 | return array( 37 | array('0.0.2', '0.0.1', Comparator::GREATER_THAN), 38 | array('0.2.0', '0.1.0', Comparator::GREATER_THAN), 39 | array('2.0.0', '1.0.0', Comparator::GREATER_THAN), 40 | 41 | array('0.0.0', '0.0.0-0', Comparator::GREATER_THAN), 42 | array('0.0.0-2', '0.0.0-1', Comparator::GREATER_THAN), 43 | array('0.0.0-a', '0.0.0-3', Comparator::GREATER_THAN), 44 | array('0.0.0-b', '0.0.0-a', Comparator::GREATER_THAN), 45 | 46 | array('0.0.0-a.b.c', '0.0.0-a.1', Comparator::GREATER_THAN), 47 | array('0.0.0-1.2.b', '0.0.0-1.2', Comparator::GREATER_THAN), 48 | 49 | array('0.0.0-rc', '0.0.0-beta', Comparator::GREATER_THAN), 50 | array('0.0.0-beta', '0.0.0-alpha', Comparator::GREATER_THAN), 51 | 52 | // semver.org precedence expectations 53 | array('1.0.0', '1.0.0-rc.1', Comparator::GREATER_THAN), 54 | array('1.0.0-rc.1', '1.0.0-beta.11', Comparator::GREATER_THAN), 55 | array('1.0.0-beta.11', '1.0.0-beta.2', Comparator::GREATER_THAN), 56 | array('1.0.0-beta.2', '1.0.0-beta', Comparator::GREATER_THAN), 57 | array('1.0.0-beta', '1.0.0-alpha.beta', Comparator::GREATER_THAN), 58 | array('1.0.0-alpha.beta', '1.0.0-alpha.1', Comparator::GREATER_THAN), 59 | array('1.0.0-alpha.1', '1.0.0-alpha', Comparator::GREATER_THAN), 60 | ); 61 | } 62 | 63 | public function getLess() 64 | { 65 | return array( 66 | array('0.0.1', '0.0.2', Comparator::LESS_THAN), 67 | array('0.1.0', '0.2.0', Comparator::LESS_THAN), 68 | array('1.0.0', '2.0.0', Comparator::LESS_THAN), 69 | 70 | array('0.0.0-0', '0.0.0', Comparator::LESS_THAN), 71 | array('0.0.0-1', '0.0.0-2', Comparator::LESS_THAN), 72 | array('0.0.0-3', '0.0.0-a', Comparator::LESS_THAN), 73 | array('0.0.0-a', '0.0.0-b', Comparator::LESS_THAN), 74 | 75 | array('0.0.0-a.1', '0.0.0-a.b.c', Comparator::LESS_THAN), 76 | array('0.0.0-1.2', '0.0.0-1.2.b', Comparator::LESS_THAN), 77 | 78 | array('0.0.0-alpha', '0.0.0-beta', Comparator::LESS_THAN), 79 | array('0.0.0-beta', '0.0.0-rc', Comparator::LESS_THAN), 80 | 81 | // semver.org precedence expectations 82 | array('1.0.0-alpha', '1.0.0-alpha.1', Comparator::LESS_THAN), 83 | array('1.0.0-alpha.1', '1.0.0-alpha.beta', Comparator::LESS_THAN), 84 | array('1.0.0-alpha.beta', '1.0.0-beta', Comparator::LESS_THAN), 85 | array('1.0.0-beta', '1.0.0-beta.2', Comparator::LESS_THAN), 86 | array('1.0.0-beta.2', '1.0.0-beta.11', Comparator::LESS_THAN), 87 | array('1.0.0-beta.11', '1.0.0-rc.1', Comparator::LESS_THAN), 88 | array('1.0.0-rc.1', '1.0.0', Comparator::LESS_THAN), 89 | ); 90 | } 91 | 92 | /** 93 | * @dataProvider getAll 94 | */ 95 | public function testCompareTo($left, $right, $expected) 96 | { 97 | $this->assertSame( 98 | $expected, 99 | Comparator::compareTo( 100 | Builder::create()->importString($left), 101 | Builder::create()->importString($right) 102 | ) 103 | ); 104 | } 105 | 106 | /** 107 | * @dataProvider getEqual 108 | */ 109 | public function testIsEqualTo($left, $right) 110 | { 111 | $this->assertTrue( 112 | Comparator::isEqualTo( 113 | Builder::create()->importString($left), 114 | Builder::create()->importString($right) 115 | ) 116 | ); 117 | } 118 | 119 | /** 120 | * @dataProvider getGreater 121 | */ 122 | public function testIsGreaterThan($left, $right) 123 | { 124 | $this->assertTrue( 125 | Comparator::isGreaterThan( 126 | Builder::create()->importString($left), 127 | Builder::create()->importString($right) 128 | ) 129 | ); 130 | } 131 | 132 | /** 133 | * @dataProvider getLess 134 | */ 135 | public function testIsLessThan($left, $right) 136 | { 137 | $this->assertTrue( 138 | Comparator::isLessThan( 139 | Builder::create()->importString($left), 140 | Builder::create()->importString($right) 141 | ) 142 | ); 143 | } 144 | } 145 | -------------------------------------------------------------------------------- /src/tests/lib/Herrera/Version/Tests/DumperTest.php: -------------------------------------------------------------------------------- 1 | assertSame( 20 | array( 21 | Parser::MAJOR => 1, 22 | Parser::MINOR => 2, 23 | Parser::PATCH => 3, 24 | Parser::PRE_RELEASE => array('pre', '1'), 25 | Parser::BUILD => array('build', '1'), 26 | ), 27 | Dumper::toComponents($this->version) 28 | ); 29 | } 30 | 31 | public function testToString() 32 | { 33 | $this->assertEquals( 34 | '1.2.3-pre.1+build.1', 35 | Dumper::toString($this->version) 36 | ); 37 | } 38 | 39 | protected function setUp() 40 | { 41 | $this->version = new Version( 42 | 1, 43 | 2, 44 | 3, 45 | array('pre', '1'), 46 | array('build', '1') 47 | ); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/tests/lib/Herrera/Version/Tests/Exception/InvalidIdentifierExceptionTest.php: -------------------------------------------------------------------------------- 1 | assertEquals( 15 | 'The identifier "test" is invalid.', 16 | $exception->getMessage() 17 | ); 18 | } 19 | 20 | /** 21 | * @depends testConstruct 22 | */ 23 | public function testGetIdentifier() 24 | { 25 | $exception = new InvalidIdentifierException('test'); 26 | 27 | $this->assertEquals('test', $exception->getIdentifier()); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/tests/lib/Herrera/Version/Tests/Exception/InvalidNumberExceptionTest.php: -------------------------------------------------------------------------------- 1 | assertEquals( 15 | 'The version number "test" is invalid.', 16 | $exception->getMessage() 17 | ); 18 | } 19 | 20 | /** 21 | * @depends testConstruct 22 | */ 23 | public function testGetNumber() 24 | { 25 | $exception = new InvalidNumberException('test'); 26 | 27 | $this->assertEquals('test', $exception->getNumber()); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/tests/lib/Herrera/Version/Tests/Exception/InvalidStringRepresentationExceptionTest.php: -------------------------------------------------------------------------------- 1 | assertEquals( 15 | 'The version string representation "test" is invalid.', 16 | $exception->getMessage() 17 | ); 18 | } 19 | 20 | /** 21 | * @depends testConstruct 22 | */ 23 | public function testGetStringRepresentation() 24 | { 25 | $exception = new InvalidStringRepresentationException('test'); 26 | 27 | $this->assertEquals('test', $exception->getVersion()); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/tests/lib/Herrera/Version/Tests/ParserTest.php: -------------------------------------------------------------------------------- 1 | assertSame(1, $builder->getMajor()); 15 | $this->assertSame(2, $builder->getMinor()); 16 | $this->assertSame(3, $builder->getPatch()); 17 | $this->assertSame(array('pre', '1'), $builder->getPreRelease()); 18 | $this->assertSame(array('build', '1'), $builder->getBuild()); 19 | } 20 | 21 | public function testToComponents() 22 | { 23 | $this->assertSame( 24 | array( 25 | Parser::MAJOR => 1, 26 | Parser::MINOR => 2, 27 | Parser::PATCH => 3, 28 | Parser::PRE_RELEASE => array('pre', '1'), 29 | Parser::BUILD => array('build', '1') 30 | ), 31 | Parser::toComponents('1.2.3-pre.1+build.1') 32 | ); 33 | } 34 | 35 | public function testToComponentsInvalid() 36 | { 37 | $this->setExpectedException( 38 | 'Herrera\\Version\\Exception\\InvalidStringRepresentationException', 39 | 'The version string representation "test" is invalid.' 40 | ); 41 | 42 | Parser::toComponents('test'); 43 | } 44 | 45 | public function testToVersion() 46 | { 47 | $version = Parser::toVersion('1.2.3-pre.1+build.1'); 48 | 49 | $this->assertSame(1, $version->getMajor()); 50 | $this->assertSame(2, $version->getMinor()); 51 | $this->assertSame(3, $version->getPatch()); 52 | $this->assertSame(array('pre', '1'), $version->getPreRelease()); 53 | $this->assertSame(array('build', '1'), $version->getBuild()); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/tests/lib/Herrera/Version/Tests/ValidatorTest.php: -------------------------------------------------------------------------------- 1 | assertFalse(Validator::isIdentifier($identifier)); 102 | } 103 | 104 | /** 105 | * @dataProvider getValidIdentifiers 106 | */ 107 | public function testIsIdentifierValid($identifier) 108 | { 109 | $this->assertTrue(Validator::isIdentifier($identifier)); 110 | } 111 | 112 | /** 113 | * @dataProvider getInvalidNumbers 114 | */ 115 | public function testIsNumberInvalid($number) 116 | { 117 | $this->assertFalse(Validator::isNumber($number)); 118 | } 119 | 120 | /** 121 | * @dataProvider getValidNumbers 122 | */ 123 | public function testIsNumberValid($number) 124 | { 125 | $this->assertTrue(Validator::isNumber($number)); 126 | } 127 | 128 | /** 129 | * @dataProvider getInvalidVersions 130 | */ 131 | public function testIsVersionInvalid($version) 132 | { 133 | $this->assertFalse(Validator::isVersion($version)); 134 | } 135 | 136 | /** 137 | * @dataProvider getValidVersions 138 | */ 139 | public function testIsVersionValid($version) 140 | { 141 | $this->assertTrue(Validator::isVersion($version)); 142 | } 143 | } 144 | -------------------------------------------------------------------------------- /src/tests/lib/Herrera/Version/Tests/VersionTest.php: -------------------------------------------------------------------------------- 1 | assertSame(0, $this->getPropertyValue($version, 'major')); 20 | $this->assertSame(0, $this->getPropertyValue($version, 'minor')); 21 | $this->assertSame(0, $this->getPropertyValue($version, 'patch')); 22 | $this->assertSame( 23 | array(), 24 | $this->getPropertyValue($version, 'preRelease') 25 | ); 26 | $this->assertSame(array(), $this->getPropertyValue($version, 'build')); 27 | } 28 | 29 | /** 30 | * @depends testConstruct 31 | */ 32 | public function testConstructWithValues() 33 | { 34 | $this->assertSame(1, $this->getPropertyValue($this->version, 'major')); 35 | $this->assertSame(2, $this->getPropertyValue($this->version, 'minor')); 36 | $this->assertSame(3, $this->getPropertyValue($this->version, 'patch')); 37 | $this->assertSame( 38 | array('pre', '1'), 39 | $this->getPropertyValue($this->version, 'preRelease') 40 | ); 41 | $this->assertSame( 42 | array('build', '1'), 43 | $this->getPropertyValue($this->version, 'build') 44 | ); 45 | } 46 | 47 | /** 48 | * @depends testConstruct 49 | * @depends testConstructWithValues 50 | */ 51 | public function testGetBuild() 52 | { 53 | $version = new Version(); 54 | 55 | $this->assertSame(array(), $version->getBuild()); 56 | $this->assertSame(array('build', '1'), $this->version->getBuild()); 57 | } 58 | 59 | /** 60 | * @depends testConstruct 61 | * @depends testConstructWithValues 62 | */ 63 | public function testGetMajor() 64 | { 65 | $version = new Version(); 66 | 67 | $this->assertSame(0, $version->getMajor()); 68 | $this->assertSame(1, $this->version->getMajor()); 69 | } 70 | 71 | /** 72 | * @depends testConstruct 73 | * @depends testConstructWithValues 74 | */ 75 | public function testGetMinor() 76 | { 77 | $version = new Version(); 78 | 79 | $this->assertSame(0, $version->getMinor()); 80 | $this->assertSame(2, $this->version->getMinor()); 81 | } 82 | 83 | /** 84 | * @depends testConstruct 85 | * @depends testConstructWithValues 86 | */ 87 | public function testGetPatch() 88 | { 89 | $version = new Version(); 90 | 91 | $this->assertSame(0, $version->getPatch()); 92 | $this->assertSame(3, $this->version->getPatch()); 93 | } 94 | 95 | /** 96 | * @depends testConstruct 97 | * @depends testConstructWithValues 98 | */ 99 | public function testGetPreRelease() 100 | { 101 | $version = new Version(); 102 | 103 | $this->assertSame(array(), $version->getPreRelease()); 104 | $this->assertSame(array('pre', '1'), $this->version->getPreRelease()); 105 | } 106 | 107 | public function testIsStable() 108 | { 109 | $this->assertFalse($this->version->isStable()); 110 | 111 | $version = new Version(); 112 | 113 | $this->assertFalse($version->isStable()); 114 | 115 | $version = new Version(1); 116 | 117 | $this->assertTrue($version->isStable()); 118 | } 119 | 120 | public function testToString() 121 | { 122 | $this->assertEquals('1.2.3-pre.1+build.1', (string) $this->version); 123 | } 124 | 125 | protected function setUp() 126 | { 127 | $this->version = new Version( 128 | 1, 129 | 2, 130 | 3, 131 | array('pre', '1'), 132 | array('build', '1') 133 | ); 134 | } 135 | } 136 | --------------------------------------------------------------------------------