]>
Commit | Line | Data |
---|---|---|
4f5b44bd NL |
1 | <?php |
2 | ||
3 | /* | |
4 | * This file is part of the Symfony package. | |
5 | * | |
6 | * (c) Fabien Potencier <fabien@symfony.com> | |
7 | * | |
8 | * For the full copyright and license information, please view the LICENSE | |
9 | * file that was distributed with this source code. | |
10 | */ | |
11 | ||
12 | namespace Symfony\Component\Intl\Util; | |
13 | ||
14 | /** | |
15 | * Facilitates the comparison of ICU version strings. | |
16 | * | |
17 | * @author Bernhard Schussek <bschussek@gmail.com> | |
18 | */ | |
19 | class IcuVersion | |
20 | { | |
21 | /** | |
22 | * Compares two ICU versions with an operator. | |
23 | * | |
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 | |
26 | * $precision. | |
27 | * | |
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. | |
31 | * | |
32 | * Examples: | |
33 | * | |
34 | * IcuVersion::compare('1.2.3', '1.2.4', '==') | |
35 | * // => false | |
36 | * | |
37 | * IcuVersion::compare('1.2.3', '1.2.4', '==', 2) | |
38 | * // => true | |
39 | * | |
40 | * IcuVersion::compare('1.2.3', '12.3', '==') | |
41 | * // => true | |
42 | * | |
43 | * IcuVersion::compare('1', '10', '==') | |
44 | * // => true | |
45 | * | |
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. | |
51 | * | |
52 | * @return Boolean Whether the comparison succeeded. | |
53 | * | |
54 | * @see normalize() | |
55 | */ | |
56 | public static function compare($version1, $version2, $operator, $precision = null) | |
57 | { | |
58 | $version1 = self::normalize($version1, $precision); | |
59 | $version2 = self::normalize($version2, $precision); | |
60 | ||
61 | return version_compare($version1, $version2, $operator); | |
62 | } | |
63 | ||
64 | /** | |
65 | * Normalizes a version string to the number of components given in the | |
66 | * parameter $precision. | |
67 | * | |
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. | |
71 | * | |
72 | * Examples: | |
73 | * | |
74 | * IcuVersion::normalize('1.2.3.4'); | |
75 | * // => '12.3.4' | |
76 | * | |
77 | * IcuVersion::normalize('1.2.3.4', 1); | |
78 | * // => '12' | |
79 | * | |
80 | * IcuVersion::normalize('1.2.3.4', 2); | |
81 | * // => '12.3' | |
82 | * | |
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. | |
86 | * | |
87 | * @return string|null The normalized ICU version or NULL if it couldn't be | |
88 | * normalized. | |
89 | */ | |
90 | public static function normalize($version, $precision) | |
91 | { | |
92 | $version = preg_replace('/^(\d)\.(\d)/', '$1$2', $version); | |
93 | ||
94 | if (1 === strlen($version)) { | |
95 | $version .= '0'; | |
96 | } | |
97 | ||
98 | return Version::normalize($version, $precision); | |
99 | } | |
100 | ||
101 | /** | |
102 | * Must not be instantiated. | |
103 | */ | |
104 | private function __construct() {} | |
105 | } |