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 version strings.
17 * @author Bernhard Schussek <bschussek@gmail.com>
22 * Compares two 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
30 * Version::compare('1.2.3', '1.2.4', '==')
33 * Version::compare('1.2.3', '1.2.4', '==', 2)
36 * @param string $version1 A version string.
37 * @param string $version2 A version string to compare.
38 * @param string $operator The comparison operator.
39 * @param integer|null $precision The number of components to compare. Pass
40 * NULL to compare the versions unchanged.
42 * @return Boolean Whether the comparison succeeded.
46 public static function compare($version1, $version2, $operator, $precision = null)
48 $version1 = self
::normalize($version1, $precision);
49 $version2 = self
::normalize($version2, $precision);
51 return version_compare($version1, $version2, $operator);
55 * Normalizes a version string to the number of components given in the
56 * parameter $precision.
60 * Version::normalize('1.2.3', 1);
63 * Version::normalize('1.2.3', 2);
66 * @param string $version A version string.
67 * @param integer|null $precision The number of components to include. Pass
68 * NULL to return the version unchanged.
70 * @return string|null The normalized version or NULL if it couldn't be
73 public static function normalize($version, $precision)
75 if (null === $precision) {
81 for ($i = 2; $i <= $precision; ++
$i) {
82 $pattern = sprintf('[^\.]+(\.%s)?', $pattern);
85 if (!preg_match('/^' . $pattern . '/', $version, $matches)) {
93 * Must not be instantiated.
95 private function __construct() {}