aboutsummaryrefslogtreecommitdiffhomepage
path: root/vendor/symfony/intl/Symfony/Component/Intl/Util/IcuVersion.php
blob: e305a07528411e83be2282b5a8e8d031bc2e9786 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php

/*
 * This file is part of the Symfony package.
 *
 * (c) Fabien Potencier <fabien@symfony.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Symfony\Component\Intl\Util;

/**
 * Facilitates the comparison of ICU version strings.
 *
 * @author Bernhard Schussek <bschussek@gmail.com>
 */
class IcuVersion
{
    /**
     * Compares two ICU versions with an operator.
     *
     * This method is identical to {@link version_compare()}, except that you
     * can pass the number of regarded version components in the last argument
     * $precision.
     *
     * Also, a single digit release version and a single digit major version
     * are contracted to a two digit release version. If no major version
     * is given, it is substituted by zero.
     *
     * Examples:
     *
     *     IcuVersion::compare('1.2.3', '1.2.4', '==')
     *     // => false
     *
     *     IcuVersion::compare('1.2.3', '1.2.4', '==', 2)
     *     // => true
     *
     *     IcuVersion::compare('1.2.3', '12.3', '==')
     *     // => true
     *
     *     IcuVersion::compare('1', '10', '==')
     *     // => true
     *
     * @param string       $version1  A version string.
     * @param string       $version2  A version string to compare.
     * @param string       $operator  The comparison operator.
     * @param integer|null $precision The number of components to compare. Pass
     *                                NULL to compare the versions unchanged.
     *
     * @return Boolean Whether the comparison succeeded.
     *
     * @see normalize()
     */
    public static function compare($version1, $version2, $operator, $precision = null)
    {
        $version1 = self::normalize($version1, $precision);
        $version2 = self::normalize($version2, $precision);

        return version_compare($version1, $version2, $operator);
    }

    /**
     * Normalizes a version string to the number of components given in the
     * parameter $precision.
     *
     * A single digit release version and a single digit major version are
     * contracted to a two digit release version. If no major version is given,
     * it is substituted by zero.
     *
     * Examples:
     *
     *     IcuVersion::normalize('1.2.3.4');
     *     // => '12.3.4'
     *
     *     IcuVersion::normalize('1.2.3.4', 1);
     *     // => '12'
     *
     *     IcuVersion::normalize('1.2.3.4', 2);
     *     // => '12.3'
     *
     * @param string       $version   An ICU version string.
     * @param integer|null $precision The number of components to include. Pass
     *                                NULL to return the version unchanged.
     *
     * @return string|null The normalized ICU version or NULL if it couldn't be
     *                     normalized.
     */
    public static function normalize($version, $precision)
    {
        $version = preg_replace('/^(\d)\.(\d)/', '$1$2', $version);

        if (1 === strlen($version)) {
            $version .= '0';
        }

        return Version::normalize($version, $precision);
    }

    /**
     * Must not be instantiated.
     */
    private function __construct() {}
}