Version.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  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. class Version {
  12. /**
  13. * @var VersionNumber
  14. */
  15. private $major;
  16. /**
  17. * @var VersionNumber
  18. */
  19. private $minor;
  20. /**
  21. * @var VersionNumber
  22. */
  23. private $patch;
  24. /**
  25. * @var PreReleaseSuffix
  26. */
  27. private $preReleaseSuffix;
  28. /**
  29. * @var string
  30. */
  31. private $versionString = '';
  32. /**
  33. * @param string $versionString
  34. */
  35. public function __construct($versionString) {
  36. $this->ensureVersionStringIsValid($versionString);
  37. $this->versionString = $versionString;
  38. }
  39. /**
  40. * @return PreReleaseSuffix
  41. */
  42. public function getPreReleaseSuffix() {
  43. return $this->preReleaseSuffix;
  44. }
  45. /**
  46. * @return string
  47. */
  48. public function getVersionString() {
  49. return $this->versionString;
  50. }
  51. /**
  52. * @return bool
  53. */
  54. public function hasPreReleaseSuffix() {
  55. return $this->preReleaseSuffix !== null;
  56. }
  57. /**
  58. * @param Version $version
  59. *
  60. * @return bool
  61. */
  62. public function isGreaterThan(Version $version) {
  63. if ($version->getMajor()->getValue() > $this->getMajor()->getValue()) {
  64. return false;
  65. }
  66. if ($version->getMajor()->getValue() < $this->getMajor()->getValue()) {
  67. return true;
  68. }
  69. if ($version->getMinor()->getValue() > $this->getMinor()->getValue()) {
  70. return false;
  71. }
  72. if ($version->getMinor()->getValue() < $this->getMinor()->getValue()) {
  73. return true;
  74. }
  75. if ($version->getPatch()->getValue() > $this->getPatch()->getValue()) {
  76. return false;
  77. }
  78. if ($version->getPatch()->getValue() < $this->getPatch()->getValue()) {
  79. return true;
  80. }
  81. if (!$version->hasPreReleaseSuffix() && !$this->hasPreReleaseSuffix()) {
  82. return false;
  83. }
  84. if ($version->hasPreReleaseSuffix() && !$this->hasPreReleaseSuffix()) {
  85. return true;
  86. }
  87. if (!$version->hasPreReleaseSuffix() && $this->hasPreReleaseSuffix()) {
  88. return false;
  89. }
  90. return $this->getPreReleaseSuffix()->isGreaterThan($version->getPreReleaseSuffix());
  91. }
  92. /**
  93. * @return VersionNumber
  94. */
  95. public function getMajor() {
  96. return $this->major;
  97. }
  98. /**
  99. * @return VersionNumber
  100. */
  101. public function getMinor() {
  102. return $this->minor;
  103. }
  104. /**
  105. * @return VersionNumber
  106. */
  107. public function getPatch() {
  108. return $this->patch;
  109. }
  110. /**
  111. * @param array $matches
  112. */
  113. private function parseVersion(array $matches) {
  114. $this->major = new VersionNumber($matches['Major']);
  115. $this->minor = new VersionNumber($matches['Minor']);
  116. $this->patch = isset($matches['Patch']) ? new VersionNumber($matches['Patch']) : new VersionNumber(null);
  117. if (isset($matches['PreReleaseSuffix'])) {
  118. $this->preReleaseSuffix = new PreReleaseSuffix($matches['PreReleaseSuffix']);
  119. }
  120. }
  121. /**
  122. * @param string $version
  123. *
  124. * @throws InvalidVersionException
  125. */
  126. private function ensureVersionStringIsValid($version) {
  127. $regex = '/^v?
  128. (?<Major>(0|(?:[1-9][0-9]*)))
  129. \\.
  130. (?<Minor>(0|(?:[1-9][0-9]*)))
  131. (\\.
  132. (?<Patch>(0|(?:[1-9][0-9]*)))
  133. )?
  134. (?:
  135. -
  136. (?<PreReleaseSuffix>(?:(dev|beta|b|RC|alpha|a|patch|p)\.?\d*))
  137. )?
  138. $/x';
  139. if (preg_match($regex, $version, $matches) !== 1) {
  140. throw new InvalidVersionException(
  141. sprintf("Version string '%s' does not follow SemVer semantics", $version)
  142. );
  143. }
  144. $this->parseVersion($matches);
  145. }
  146. }