[TASK] Re-work/simplify copyright header in PHP files - Part 3
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Utility / VersionNumberUtility.php
1 <?php
2 namespace TYPO3\CMS\Core\Utility;
3
4 /**
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16 /**
17 * Class with helper functions for version number handling
18 *
19 * @author Susanne Moog <typo3@susanne-moog.de>
20 */
21 class VersionNumberUtility {
22
23 /**
24 * Returns an integer from a three part version number, eg '4.12.3' -> 4012003
25 *
26 * @param string $versionNumber Version number on format x.x.x
27 * @return integer Integer version of version number (where each part can count to 999)
28 */
29 static public function convertVersionNumberToInteger($versionNumber) {
30 $versionParts = explode('.', $versionNumber);
31 return (int)(((int)$versionParts[0] . str_pad((int)$versionParts[1], 3, '0', STR_PAD_LEFT)) . str_pad((int)$versionParts[2], 3, '0', STR_PAD_LEFT));
32 }
33
34 /**
35 * Returns the three part version number (string) from an integer, eg 4012003 -> '4.12.3'
36 *
37 * @param integer $versionInteger Integer representation of version number
38 * @return string Version number as format x.x.x
39 * @throws \InvalidArgumentException if $versionInteger is not an integer
40 */
41 static public function convertIntegerToVersionNumber($versionInteger) {
42 if (!is_int($versionInteger)) {
43 throw new \InvalidArgumentException('TYPO3\\CMS\\Core\\Utility\\VersionNumberUtility::convertIntegerToVersionNumber() supports an integer argument only!', 1334072223);
44 }
45 $versionString = str_pad($versionInteger, 9, '0', STR_PAD_LEFT);
46 $parts = array(
47 substr($versionString, 0, 3),
48 substr($versionString, 3, 3),
49 substr($versionString, 6, 3)
50 );
51 return (int)$parts[0] . '.' . (int)$parts[1] . '.' . (int)$parts[2];
52 }
53
54 /**
55 * Splits a version range into an array.
56 *
57 * If a single version number is given, it is considered a minimum value.
58 * If a dash is found, the numbers left and right are considered as minimum and maximum. Empty values are allowed.
59 * If no version can be parsed "0.0.0" — "0.0.0" is the result
60 *
61 * @param string $version A string with a version range.
62 * @return array
63 */
64 static public function splitVersionRange($version) {
65 $versionRange = array();
66 if (strstr($version, '-')) {
67 $versionRange = explode('-', $version, 2);
68 } else {
69 $versionRange[0] = $version;
70 $versionRange[1] = '';
71 }
72 if (!$versionRange[0]) {
73 $versionRange[0] = '0.0.0';
74 }
75 if (!$versionRange[1]) {
76 $versionRange[1] = '0.0.0';
77 }
78 return $versionRange;
79 }
80
81 /**
82 * Removes -dev -alpha -beta -RC states (also without '-' prefix) from a version number
83 * and replaces them by .0 and normalizes to a three part version number
84 *
85 * @return string
86 */
87 static public function getNumericTypo3Version() {
88 $t3version = static::getCurrentTypo3Version();
89 $t3version = preg_replace('/-?(dev|alpha|beta|RC).*$/', '', $t3version);
90 $parts = \TYPO3\CMS\Core\Utility\GeneralUtility::intExplode('.', $t3version . '..');
91 $t3version = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($parts[0], 0, 999) . '.' .
92 \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($parts[1], 0, 999) . '.' .
93 \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($parts[2], 0, 999);
94 return $t3version;
95 }
96
97 /**
98 * Wrapper function for TYPO3_version constant to make functions using
99 * the constant unit testable
100 *
101 * @return string
102 */
103 static public function getCurrentTypo3Version() {
104 return TYPO3_version;
105 }
106
107 /**
108 * This function converts version range strings (like '4.2.0-4.4.99') to an array
109 * (like array('4.2.0', '4.4.99'). It also forces each version part to be between
110 * 0 and 999
111 *
112 * @param string $versionsString
113 * @return array
114 */
115 static public function convertVersionsStringToVersionNumbers($versionsString) {
116 $versions = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode('-', $versionsString);
117 $versionsCount = count($versions);
118 for ($i = 0; $i < $versionsCount; $i++) {
119 $cleanedVersion = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode('.', $versions[$i]);
120 $cleanedVersionCount = count($cleanedVersion);
121 for ($j = 0; $j < $cleanedVersionCount; $j++) {
122 $cleanedVersion[$j] = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($cleanedVersion[$j], 0, 999);
123 }
124 $cleanedVersionString = implode('.', $cleanedVersion);
125 if (static::convertVersionNumberToInteger($cleanedVersionString) === 0) {
126 $cleanedVersionString = '';
127 }
128 $versions[$i] = $cleanedVersionString;
129 }
130 return $versions;
131 }
132
133 /**
134 * Parses the version number x.x.x and returns an array with the various parts.
135 * It also forces each … 0 to 999
136 *
137 * @param string $version Version code, x.x.x
138 * @return array
139 */
140 static public function convertVersionStringToArray($version) {
141 $parts = \TYPO3\CMS\Core\Utility\GeneralUtility::intExplode('.', $version . '..');
142 $parts[0] = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($parts[0], 0, 999);
143 $parts[1] = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($parts[1], 0, 999);
144 $parts[2] = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($parts[2], 0, 999);
145 $result = array();
146 $result['version'] = $parts[0] . '.' . $parts[1] . '.' . $parts[2];
147 $result['version_int'] = (int)($parts[0] * 1000000 + $parts[1] * 1000 + $parts[2]);
148 $result['version_main'] = $parts[0];
149 $result['version_sub'] = $parts[1];
150 $result['version_dev'] = $parts[2];
151 return $result;
152 }
153
154 /**
155 * Method to raise a version number
156 *
157 * @param string $raise one of "main", "sub", "dev" - the version part to raise by one
158 * @param string $version (like 4.1.20)
159 * @return string
160 * @throws \TYPO3\CMS\Core\Exception
161 */
162 static public function raiseVersionNumber($raise, $version) {
163 if (!in_array($raise, array('main', 'sub', 'dev'))) {
164 throw new \TYPO3\CMS\Core\Exception('RaiseVersionNumber expects one of "main", "sub" or "dev".', 1342639555);
165 }
166 $parts = \TYPO3\CMS\Core\Utility\GeneralUtility::intExplode('.', $version . '..');
167 $parts[0] = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($parts[0], 0, 999);
168 $parts[1] = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($parts[1], 0, 999);
169 $parts[2] = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($parts[2], 0, 999);
170 switch ((string) $raise) {
171 case 'main':
172 $parts[0]++;
173 $parts[1] = 0;
174 $parts[2] = 0;
175 break;
176 case 'sub':
177 $parts[1]++;
178 $parts[2] = 0;
179 break;
180 case 'dev':
181 $parts[2]++;
182 break;
183 }
184 return $parts[0] . '.' . $parts[1] . '.' . $parts[2];
185 }
186
187 }