PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backup, Restore, Migration & Clone / 3.8.2
WP STAGING – WordPress Backup, Restore, Migration & Clone v3.8.2
4.9.1 4.9.0 4.8.1 trunk 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.10.0 3.2.0 3.3.1 3.3.2 3.3.3 3.4.1 3.4.3 3.5.0 3.6.0 3.7.1 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.8.6 3.8.7 3.9.0 3.9.1 3.9.2 3.9.3 3.9.4 4.0.0 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.0 4.7.0 4.7.1 4.7.2 4.7.3 4.8.0
wp-staging / Framework / Utils / Version.php
wp-staging / Framework / Utils Last commit date
Cache 2 years ago DBPermissions.php 2 years ago DataEncoder.php 2 years ago Escape.php 2 years ago Glob.php 2 years ago Hooks.php 2 years ago Math.php 2 years ago PluginInfo.php 2 years ago Sanitize.php 2 years ago ServerVars.php 2 years ago SlashMode.php 5 years ago Strings.php 2 years ago Times.php 2 years ago Urls.php 2 years ago Version.php 2 years ago WpDefaultDirectories.php 2 years ago
Version.php
71 lines
1 <?php
2
3 namespace WPStaging\Framework\Utils;
4
5 use InvalidArgumentException;
6
7 /**
8 * Class Version
9 * Provide method to convert version number to integer and vice versa
10 * @package WPStaging\Framework\Utils
11 */
12 class Version
13 {
14 /**
15 * Convert version string format to integer format i.e. 1.0.0 to 10000
16 *
17 * @param string $versionString
18 * @return int
19 * @throws InvalidArgumentException
20 */
21 public function convertStringFormatToIntFormat(string $versionString): int
22 {
23 $versionParts = explode('.', $versionString);
24 if (count($versionParts) !== 3) {
25 throw new InvalidArgumentException('Invalid version string format');
26 }
27
28 foreach ($versionParts as $part) {
29 if (!is_numeric($part)) {
30 throw new InvalidArgumentException('Version parts must be positive integers');
31 }
32 }
33
34 $versionParts = array_map('intval', $versionParts);
35
36 if ($versionParts[0] < 0 || $versionParts[1] < 0 || $versionParts[2] < 0) {
37 throw new InvalidArgumentException('Version parts must be positive integers');
38 }
39
40 if ($versionParts[0] === 0 && $versionParts[1] === 0 && $versionParts[2] === 0) {
41 throw new InvalidArgumentException('Invalid version string format');
42 }
43
44 if ($versionParts[1] > 100 || $versionParts[2] > 100) {
45 throw new InvalidArgumentException('Version Minor and Patch parts must be less than 100');
46 }
47
48 return $versionParts[0] * 10000 + $versionParts[1] * 100 + $versionParts[2];
49 }
50
51 /**
52 * Convert version integer format to string format i.e. 10000 to 1.0.0
53 *
54 * @param int $version
55 * @return string
56 * @throws InvalidArgumentException
57 */
58 public function convertIntFormatToStringFormat(int $version): string
59 {
60 if ($version < 1) {
61 throw new InvalidArgumentException('Version must be a positive integer');
62 }
63
64 $major = floor($version / 10000);
65 $minor = floor(($version % 10000) / 100);
66 $patch = $version % 100;
67
68 return sprintf('%d.%d.%d', $major, $minor, $patch);
69 }
70 }
71