VersionConstraintValue.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php declare(strict_types = 1);
  2. namespace PharIo\Version;
  3. class VersionConstraintValue {
  4. /** @var VersionNumber */
  5. private $major;
  6. /** @var VersionNumber */
  7. private $minor;
  8. /** @var VersionNumber */
  9. private $patch;
  10. /** @var string */
  11. private $label = '';
  12. /** @var string */
  13. private $buildMetaData = '';
  14. /** @var string */
  15. private $versionString = '';
  16. public function __construct(string $versionString) {
  17. $this->versionString = $versionString;
  18. $this->parseVersion($versionString);
  19. }
  20. public function getLabel(): string {
  21. return $this->label;
  22. }
  23. public function getBuildMetaData(): string {
  24. return $this->buildMetaData;
  25. }
  26. public function getVersionString(): string {
  27. return $this->versionString;
  28. }
  29. public function getMajor(): VersionNumber {
  30. return $this->major;
  31. }
  32. public function getMinor(): VersionNumber {
  33. return $this->minor;
  34. }
  35. public function getPatch(): VersionNumber {
  36. return $this->patch;
  37. }
  38. private function parseVersion(string $versionString): void {
  39. $this->extractBuildMetaData($versionString);
  40. $this->extractLabel($versionString);
  41. $this->stripPotentialVPrefix($versionString);
  42. $versionSegments = \explode('.', $versionString);
  43. $this->major = new VersionNumber(\is_numeric($versionSegments[0]) ? (int)$versionSegments[0] : null);
  44. $minorValue = isset($versionSegments[1]) && \is_numeric($versionSegments[1]) ? (int)$versionSegments[1] : null;
  45. $patchValue = isset($versionSegments[2]) && \is_numeric($versionSegments[2]) ? (int)$versionSegments[2] : null;
  46. $this->minor = new VersionNumber($minorValue);
  47. $this->patch = new VersionNumber($patchValue);
  48. }
  49. private function extractBuildMetaData(string &$versionString): void {
  50. if (\preg_match('/\+(.*)/', $versionString, $matches) === 1) {
  51. $this->buildMetaData = $matches[1];
  52. $versionString = \str_replace($matches[0], '', $versionString);
  53. }
  54. }
  55. private function extractLabel(string &$versionString): void {
  56. if (\preg_match('/-(.*)/', $versionString, $matches) === 1) {
  57. $this->label = $matches[1];
  58. $versionString = \str_replace($matches[0], '', $versionString);
  59. }
  60. }
  61. private function stripPotentialVPrefix(string &$versionString): void {
  62. if ($versionString[0] !== 'v') {
  63. return;
  64. }
  65. $versionString = \substr($versionString, 1);
  66. }
  67. }