VersionParser.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Lukas Reschke <lukas@statuscode.ch>
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Lukas Reschke <lukas@statuscode.ch>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OC\App\AppStore\Version;
  26. /**
  27. * Class VersionParser parses the versions as sent by the Nextcloud app store
  28. *
  29. * @package OC\App\AppStore
  30. */
  31. class VersionParser {
  32. /**
  33. * @param string $versionString
  34. * @return bool
  35. */
  36. private function isValidVersionString($versionString) {
  37. return (bool)preg_match('/^[0-9.]+$/', $versionString);
  38. }
  39. /**
  40. * Returns the version for a version string
  41. *
  42. * @param string $versionSpec
  43. * @return Version
  44. * @throws \Exception If the version cannot be parsed
  45. */
  46. public function getVersion($versionSpec) {
  47. // * indicates that the version is compatible with all versions
  48. if ($versionSpec === '*') {
  49. return new Version('', '');
  50. }
  51. // Count the amount of =, if it is one then it's either maximum or minimum
  52. // version. If it is two then it is maximum and minimum.
  53. $versionElements = explode(' ', $versionSpec);
  54. $firstVersion = isset($versionElements[0]) ? $versionElements[0] : '';
  55. $firstVersionNumber = substr($firstVersion, 2);
  56. $secondVersion = isset($versionElements[1]) ? $versionElements[1] : '';
  57. $secondVersionNumber = substr($secondVersion, 2);
  58. switch (count($versionElements)) {
  59. case 1:
  60. if (!$this->isValidVersionString($firstVersionNumber)) {
  61. break;
  62. }
  63. if (strpos($firstVersion, '>') === 0) {
  64. return new Version($firstVersionNumber, '');
  65. }
  66. return new Version('', $firstVersionNumber);
  67. case 2:
  68. if (!$this->isValidVersionString($firstVersionNumber) || !$this->isValidVersionString($secondVersionNumber)) {
  69. break;
  70. }
  71. return new Version($firstVersionNumber, $secondVersionNumber);
  72. }
  73. throw new \Exception(
  74. sprintf(
  75. 'Version cannot be parsed: %s',
  76. $versionSpec
  77. )
  78. );
  79. }
  80. }