1
0

CompareVersion.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\App;
  8. use InvalidArgumentException;
  9. use function explode;
  10. class CompareVersion {
  11. private const REGEX_MAJOR = '/^\d+$/';
  12. private const REGEX_MAJOR_MINOR = '/^\d+\.\d+$/';
  13. private const REGEX_MAJOR_MINOR_PATCH = '/^\d+\.\d+\.\d+(?!\.\d+)/';
  14. private const REGEX_ACTUAL = '/^\d+(\.\d+){1,2}/';
  15. /**
  16. * Checks if the given server version fulfills the given (app) version requirements.
  17. *
  18. * Version requirements can be 'major.minor.patch', 'major.minor' or just 'major',
  19. * so '13.0.1', '13.0' and '13' are valid.
  20. *
  21. * @param string $actual version as major.minor.patch notation
  22. * @param string $required version where major is required and minor and patch are optional
  23. * @param string $comparator passed to `version_compare`
  24. * @return bool whether the requirement is fulfilled
  25. * @throws InvalidArgumentException if versions specified in an invalid format
  26. */
  27. public function isCompatible(string $actual, string $required,
  28. string $comparator = '>='): bool {
  29. if (!preg_match(self::REGEX_ACTUAL, $actual, $matches)) {
  30. throw new InvalidArgumentException("version specification $actual is invalid");
  31. }
  32. $cleanActual = $matches[0];
  33. if (preg_match(self::REGEX_MAJOR, $required) === 1) {
  34. return $this->compareMajor($cleanActual, $required, $comparator);
  35. } elseif (preg_match(self::REGEX_MAJOR_MINOR, $required) === 1) {
  36. return $this->compareMajorMinor($cleanActual, $required, $comparator);
  37. } elseif (preg_match(self::REGEX_MAJOR_MINOR_PATCH, $required) === 1) {
  38. return $this->compareMajorMinorPatch($cleanActual, $required, $comparator);
  39. } else {
  40. throw new InvalidArgumentException("required version $required is invalid");
  41. }
  42. }
  43. private function compareMajor(string $actual, string $required,
  44. string $comparator) {
  45. $actualMajor = explode('.', $actual)[0];
  46. $requiredMajor = explode('.', $required)[0];
  47. return version_compare($actualMajor, $requiredMajor, $comparator);
  48. }
  49. private function compareMajorMinor(string $actual, string $required,
  50. string $comparator) {
  51. $actualMajor = explode('.', $actual)[0];
  52. $actualMinor = explode('.', $actual)[1];
  53. $requiredMajor = explode('.', $required)[0];
  54. $requiredMinor = explode('.', $required)[1];
  55. return version_compare("$actualMajor.$actualMinor",
  56. "$requiredMajor.$requiredMinor", $comparator);
  57. }
  58. private function compareMajorMinorPatch($actual, $required, $comparator) {
  59. $actualMajor = explode('.', $actual)[0];
  60. $actualMinor = explode('.', $actual)[1];
  61. $actualPatch = explode('.', $actual)[2];
  62. $requiredMajor = explode('.', $required)[0];
  63. $requiredMinor = explode('.', $required)[1];
  64. $requiredPatch = explode('.', $required)[2];
  65. return version_compare("$actualMajor.$actualMinor.$actualPatch",
  66. "$requiredMajor.$requiredMinor.$requiredPatch", $comparator);
  67. }
  68. }