4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Intl\Util
;
15 * Facilitates the comparison of ICU version strings.
17 * @author Bernhard Schussek <bschussek@gmail.com>
22 * Compares two ICU versions with an operator.
24 * This method is identical to {@link version_compare()}, except that you
25 * can pass the number of regarded version components in the last argument
28 * Also, a single digit release version and a single digit major version
29 * are contracted to a two digit release version. If no major version
30 * is given, it is substituted by zero.
34 * IcuVersion::compare('1.2.3', '1.2.4', '==')
37 * IcuVersion::compare('1.2.3', '1.2.4', '==', 2)
40 * IcuVersion::compare('1.2.3', '12.3', '==')
43 * IcuVersion::compare('1', '10', '==')
46 * @param string $version1 A version string.
47 * @param string $version2 A version string to compare.
48 * @param string $operator The comparison operator.
49 * @param integer|null $precision The number of components to compare. Pass
50 * NULL to compare the versions unchanged.
52 * @return Boolean Whether the comparison succeeded.
56 public static function compare($version1, $version2, $operator, $precision = null)
58 $version1 = self
::normalize($version1, $precision);
59 $version2 = self
::normalize($version2, $precision);
61 return version_compare($version1, $version2, $operator);
65 * Normalizes a version string to the number of components given in the
66 * parameter $precision.
68 * A single digit release version and a single digit major version are
69 * contracted to a two digit release version. If no major version is given,
70 * it is substituted by zero.
74 * IcuVersion::normalize('1.2.3.4');
77 * IcuVersion::normalize('1.2.3.4', 1);
80 * IcuVersion::normalize('1.2.3.4', 2);
83 * @param string $version An ICU version string.
84 * @param integer|null $precision The number of components to include. Pass
85 * NULL to return the version unchanged.
87 * @return string|null The normalized ICU version or NULL if it couldn't be
90 public static function normalize($version, $precision)
92 $version = preg_replace('/^(\d)\.(\d)/', '$1$2', $version);
94 if (1 === strlen($version)) {
98 return Version::normalize($version, $precision);
102 * Must not be instantiated.
104 private function __construct() {}