]> git.immae.eu Git - github/wallabag/wallabag.git/blob - vendor/symfony/intl/Symfony/Component/Intl/Util/Version.php
5f6a43374b8bdf66763d41476766597f7886d4d0
[github/wallabag/wallabag.git] / vendor / symfony / intl / Symfony / Component / Intl / Util / Version.php
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 version strings.
16 *
17 * @author Bernhard Schussek <bschussek@gmail.com>
18 */
19 class Version
20 {
21 /**
22 * Compares two 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 * Examples:
29 *
30 * Version::compare('1.2.3', '1.2.4', '==')
31 * // => false
32 *
33 * Version::compare('1.2.3', '1.2.4', '==', 2)
34 * // => true
35 *
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.
41 *
42 * @return Boolean Whether the comparison succeeded.
43 *
44 * @see normalize()
45 */
46 public static function compare($version1, $version2, $operator, $precision = null)
47 {
48 $version1 = self::normalize($version1, $precision);
49 $version2 = self::normalize($version2, $precision);
50
51 return version_compare($version1, $version2, $operator);
52 }
53
54 /**
55 * Normalizes a version string to the number of components given in the
56 * parameter $precision.
57 *
58 * Examples:
59 *
60 * Version::normalize('1.2.3', 1);
61 * // => '1'
62 *
63 * Version::normalize('1.2.3', 2);
64 * // => '1.2'
65 *
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.
69 *
70 * @return string|null The normalized version or NULL if it couldn't be
71 * normalized.
72 */
73 public static function normalize($version, $precision)
74 {
75 if (null === $precision) {
76 return $version;
77 }
78
79 $pattern = '[^\.]+';
80
81 for ($i = 2; $i <= $precision; ++$i) {
82 $pattern = sprintf('[^\.]+(\.%s)?', $pattern);
83 }
84
85 if (!preg_match('/^' . $pattern . '/', $version, $matches)) {
86 return null;
87 }
88
89 return $matches[0];
90 }
91
92 /**
93 * Must not be instantiated.
94 */
95 private function __construct() {}
96 }