VersionParser.php 2.5 KB

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