PreReleaseSuffix.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. namespace PharIo\Version;
  3. class PreReleaseSuffix {
  4. private $valueScoreMap = [
  5. 'dev' => 0,
  6. 'a' => 1,
  7. 'alpha' => 1,
  8. 'b' => 2,
  9. 'beta' => 2,
  10. 'rc' => 3,
  11. 'p' => 4,
  12. 'patch' => 4,
  13. ];
  14. /**
  15. * @var string
  16. */
  17. private $value;
  18. /**
  19. * @var int
  20. */
  21. private $valueScore;
  22. /**
  23. * @var int
  24. */
  25. private $number = 0;
  26. /**
  27. * @param string $value
  28. */
  29. public function __construct($value) {
  30. $this->parseValue($value);
  31. }
  32. /**
  33. * @return string
  34. */
  35. public function getValue() {
  36. return $this->value;
  37. }
  38. /**
  39. * @return int|null
  40. */
  41. public function getNumber() {
  42. return $this->number;
  43. }
  44. /**
  45. * @param PreReleaseSuffix $suffix
  46. *
  47. * @return bool
  48. */
  49. public function isGreaterThan(PreReleaseSuffix $suffix) {
  50. if ($this->valueScore > $suffix->valueScore) {
  51. return true;
  52. }
  53. if ($this->valueScore < $suffix->valueScore) {
  54. return false;
  55. }
  56. return $this->getNumber() > $suffix->getNumber();
  57. }
  58. /**
  59. * @param $value
  60. *
  61. * @return int
  62. */
  63. private function mapValueToScore($value) {
  64. if (array_key_exists($value, $this->valueScoreMap)) {
  65. return $this->valueScoreMap[$value];
  66. }
  67. return 0;
  68. }
  69. private function parseValue($value) {
  70. $regex = '/-?(dev|beta|b|rc|alpha|a|patch|p)\.?(\d*).*$/i';
  71. if (preg_match($regex, $value, $matches) !== 1) {
  72. throw new InvalidPreReleaseSuffixException(sprintf('Invalid label %s', $value));
  73. }
  74. $this->value = $matches[1];
  75. if (isset($matches[2])) {
  76. $this->number = (int)$matches[2];
  77. }
  78. $this->valueScore = $this->mapValueToScore($this->value);
  79. }
  80. }