Passed
Push — 3.0 ( 08e8bd...36a57c )
by Rubén
03:42
created

VersionUtil::getVersionArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * sysPass
4
 *
5
 * @author    nuxsmin
6
 * @link      https://syspass.org
7
 * @copyright 2012-2018, Rubén Domínguez nuxsmin@$syspass.org
8
 *
9
 * This file is part of sysPass.
10
 *
11
 * sysPass is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU General Public License as published by
13
 * the Free Software Foundation, either version 3 of the License, or
14
 * (at your option) any later version.
15
 *
16
 * sysPass 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 General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 *  along with sysPass.  If not, see <http://www.gnu.org/licenses/>.
23
 */
24
25
namespace SP\Util;
26
27
use SP\Services\Install\Installer;
28
29
/**
30
 * Class VersionUtil
31
 *
32
 * @package SP\Util
33
 */
34
final class VersionUtil
35
{
36
    /**
37
     * Devolver versión normalizada en cadena
38
     *
39
     * @return string
40
     */
41
    public static function getVersionStringNormalized()
42
    {
43
        return implode('', Installer::VERSION) . '.' . Installer::BUILD;
44
    }
45
46
    /**
47
     * Comprobar si una versión necesita actualización
48
     *
49
     * @param string       $currentVersion
50
     * @param array|string $upgradeableVersion
51
     *
52
     * @return bool True si la versión es menor.
53
     */
54
    public static function checkVersion($currentVersion, $upgradeableVersion)
55
    {
56
        if (is_array($upgradeableVersion)) {
57
            $upgradeableVersion = array_pop($upgradeableVersion);
58
        }
59
60
        $currentVersion = self::normalizeVersionForCompare($currentVersion);
61
        $upgradeableVersion = self::normalizeVersionForCompare($upgradeableVersion);
62
63
        if (empty($currentVersion) || empty($upgradeableVersion)) {
64
            return false;
65
        }
66
67
        if (PHP_INT_SIZE > 4) {
68
            return version_compare($currentVersion, $upgradeableVersion) === -1;
69
        }
70
71
        list($currentVersion, $build) = explode('.', $currentVersion, 2);
72
        list($upgradeVersion, $upgradeBuild) = explode('.', $upgradeableVersion, 2);
73
74
        $versionRes = (int)$currentVersion < (int)$upgradeVersion;
75
76
        return (($versionRes && (int)$upgradeBuild === 0)
77
            || ($versionRes && (int)$build < (int)$upgradeBuild));
78
    }
79
80
    /**
81
     * Devuelve una versión normalizada para poder ser comparada
82
     *
83
     * @param string $versionIn
84
     *
85
     * @return string
86
     */
87
    public static function normalizeVersionForCompare($versionIn)
88
    {
89
        if (is_string($versionIn) && !empty($versionIn)) {
90
            list($version, $build) = explode('.', $versionIn);
91
92
            $nomalizedVersion = 0;
93
94
            foreach (str_split($version) as $key => $value) {
95
                $nomalizedVersion += (int)$value * (10 ** (3 - $key));
96
            }
97
98
            return $nomalizedVersion . '.' . $build;
99
        }
100
101
        return '';
102
    }
103
104
    /**
105
     * @param string $version
106
     *
107
     * @return float|int
108
     */
109
    public static function versionToInteger(string $version)
110
    {
111
        $intVersion = 0;
112
113
        foreach (str_split(str_replace('.', '', $version)) as $key => $value) {
114
            $intVersion += (int)$value * (10 ** (3 - $key));
115
        }
116
117
        return $intVersion;
118
    }
119
120
    /**
121
     * Devuelve la versión de sysPass.
122
     *
123
     * @param bool $retBuild devolver el número de compilación
124
     *
125
     * @return array con el número de versión
126
     */
127
    public static function getVersionArray($retBuild = false)
128
    {
129
        $version = array_values(Installer::VERSION);
130
131
        if ($retBuild === true) {
132
            $version[] = Installer::BUILD;
133
134
            return $version;
135
        }
136
137
        return $version;
138
    }
139
140
    /**
141
     * Devolver versión normalizada en array
142
     *
143
     * @return array
144
     */
145
    public static function getVersionArrayNormalized()
146
    {
147
        return [implode('', Installer::VERSION), Installer::BUILD];
148
    }
149
}