You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CompareVersion.php 3.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2018 Christoph Wurst <christoph@winzerhof-wurst.at>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OC\App;
  25. use InvalidArgumentException;
  26. use function explode;
  27. class CompareVersion {
  28. private const REGEX_MAJOR = '/^\d+$/';
  29. private const REGEX_MAJOR_MINOR = '/^\d+\.\d+$/';
  30. private const REGEX_MAJOR_MINOR_PATCH = '/^\d+\.\d+\.\d+(?!\.\d+)/';
  31. private const REGEX_ACTUAL = '/^\d+(\.\d+){1,2}/';
  32. /**
  33. * Checks if the given server version fulfills the given (app) version requirements.
  34. *
  35. * Version requirements can be 'major.minor.patch', 'major.minor' or just 'major',
  36. * so '13.0.1', '13.0' and '13' are valid.
  37. *
  38. * @param string $actual version as major.minor.patch notation
  39. * @param string $required version where major is requried and minor and patch are optional
  40. * @param string $comparator passed to `version_compare`
  41. * @return bool whether the requirement is fulfilled
  42. * @throws InvalidArgumentException if versions specified in an invalid format
  43. */
  44. public function isCompatible(string $actual, string $required,
  45. string $comparator = '>='): bool {
  46. if (!preg_match(self::REGEX_ACTUAL, $actual, $matches)) {
  47. throw new InvalidArgumentException("version specification $actual is invalid");
  48. }
  49. $cleanActual = $matches[0];
  50. if (preg_match(self::REGEX_MAJOR, $required) === 1) {
  51. return $this->compareMajor($cleanActual, $required, $comparator);
  52. } elseif (preg_match(self::REGEX_MAJOR_MINOR, $required) === 1) {
  53. return $this->compareMajorMinor($cleanActual, $required, $comparator);
  54. } elseif (preg_match(self::REGEX_MAJOR_MINOR_PATCH, $required) === 1) {
  55. return $this->compareMajorMinorPatch($cleanActual, $required, $comparator);
  56. } else {
  57. throw new InvalidArgumentException("required version $required is invalid");
  58. }
  59. }
  60. private function compareMajor(string $actual, string $required,
  61. string $comparator) {
  62. $actualMajor = explode('.', $actual)[0];
  63. $requiredMajor = explode('.', $required)[0];
  64. return version_compare($actualMajor, $requiredMajor, $comparator);
  65. }
  66. private function compareMajorMinor(string $actual, string $required,
  67. string $comparator) {
  68. $actualMajor = explode('.', $actual)[0];
  69. $actualMinor = explode('.', $actual)[1];
  70. $requiredMajor = explode('.', $required)[0];
  71. $requiredMinor = explode('.', $required)[1];
  72. return version_compare("$actualMajor.$actualMinor",
  73. "$requiredMajor.$requiredMinor", $comparator);
  74. }
  75. private function compareMajorMinorPatch($actual, $required, $comparator) {
  76. $actualMajor = explode('.', $actual)[0];
  77. $actualMinor = explode('.', $actual)[1];
  78. $actualPatch = explode('.', $actual)[2];
  79. $requiredMajor = explode('.', $required)[0];
  80. $requiredMinor = explode('.', $required)[1];
  81. $requiredPatch = explode('.', $required)[2];
  82. return version_compare("$actualMajor.$actualMinor.$actualPatch",
  83. "$requiredMajor.$requiredMinor.$requiredPatch", $comparator);
  84. }
  85. }