PreReleaseSuffix.php 1.8 KB

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