Version.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. * @param array $matches
  41. */
  42. private function parseVersion(array $matches) {
  43. $this->major = new VersionNumber($matches['Major']);
  44. $this->minor = new VersionNumber($matches['Minor']);
  45. $this->patch = isset($matches['Patch']) ? new VersionNumber($matches['Patch']) : new VersionNumber(null);
  46. if (isset($matches['ReleaseType'])) {
  47. $preReleaseNumber = isset($matches['ReleaseTypeCount']) ? (int) $matches['ReleaseTypeCount'] : null;
  48. $this->preReleaseSuffix = new PreReleaseSuffix($matches['ReleaseType'], $preReleaseNumber);
  49. }
  50. }
  51. /**
  52. * @return PreReleaseSuffix
  53. */
  54. public function getPreReleaseSuffix()
  55. {
  56. return $this->preReleaseSuffix;
  57. }
  58. /**
  59. * @return string
  60. */
  61. public function getVersionString() {
  62. return $this->versionString;
  63. }
  64. /**
  65. * @param Version $version
  66. *
  67. * @return bool
  68. */
  69. public function isGreaterThan(Version $version) {
  70. if ($version->getMajor()->getValue() > $this->getMajor()->getValue()) {
  71. return false;
  72. }
  73. if ($version->getMajor()->getValue() < $this->getMajor()->getValue()) {
  74. return true;
  75. }
  76. if ($version->getMinor()->getValue() > $this->getMinor()->getValue()) {
  77. return false;
  78. }
  79. if ($version->getMinor()->getValue() < $this->getMinor()->getValue()) {
  80. return true;
  81. }
  82. if ($version->getPatch()->getValue() >= $this->getPatch()->getValue()) {
  83. return false;
  84. }
  85. if ($version->getPatch()->getValue() < $this->getPatch()->getValue()) {
  86. return true;
  87. }
  88. return false;
  89. }
  90. /**
  91. * @return VersionNumber
  92. */
  93. public function getMajor() {
  94. return $this->major;
  95. }
  96. /**
  97. * @return VersionNumber
  98. */
  99. public function getMinor() {
  100. return $this->minor;
  101. }
  102. /**
  103. * @return VersionNumber
  104. */
  105. public function getPatch() {
  106. return $this->patch;
  107. }
  108. /**
  109. * @param string $version
  110. *
  111. * @throws InvalidVersionException
  112. */
  113. private function ensureVersionStringIsValid($version) {
  114. $regex = '/^v?
  115. (?<Major>(0|(?:[1-9][0-9]*)))
  116. \\.
  117. (?<Minor>(0|(?:[1-9][0-9]*)))
  118. (\\.
  119. (?<Patch>(0|(?:[1-9][0-9]*)))
  120. )?
  121. (?:
  122. -
  123. (?<ReleaseType>(?:(dev|beta|b|RC|alpha|a|patch|p)))
  124. (?:
  125. (?<ReleaseTypeCount>[0-9])
  126. )?
  127. )?
  128. $/x';
  129. if (preg_match($regex, $version, $matches) !== 1) {
  130. throw new InvalidVersionException(
  131. sprintf("Version string '%s' does not follow SemVer semantics", $version)
  132. );
  133. }
  134. $this->parseVersion($matches);
  135. }
  136. }