|
2 | 2 |
|
3 | 3 | declare(strict_types=1); |
4 | 4 |
|
5 | | -namespace Patchstack\VersionCompare; |
| 5 | +namespace Patchstack\VersionCompare\Comparators; |
6 | 6 |
|
7 | | -final class NormalizeVersionPair |
| 7 | +use Patchstack\VersionCompare\Contracts\VersionComparator; |
| 8 | + |
| 9 | +/** |
| 10 | + * Compares versions by treating each dot-separated segment as a decimal number. |
| 11 | + * |
| 12 | + * PHP's version_compare treats "3.5" < "3.41" (string comparison: "5" < "41"). |
| 13 | + * This comparator right-pads segments so "3.5" becomes "3.50", making 3.50 > 3.41. |
| 14 | + */ |
| 15 | +final class DecimalComparator implements VersionComparator |
8 | 16 | { |
| 17 | + public function compare(string $version1, string $version2, string $operator): bool |
| 18 | + { |
| 19 | + [$version1, $version2] = $this->padSegments($version1, $version2); |
| 20 | + |
| 21 | + return version_compare($version1, $version2, $operator); |
| 22 | + } |
| 23 | + |
9 | 24 | /** |
10 | | - * Normalize two version strings by right-padding numeric segments to equal length. |
11 | | - * |
12 | | - * This ensures decimal-style versioning compares correctly: |
13 | | - * e.g., "3.5" vs "3.41" becomes "3.50" vs "3.41". |
14 | | - * |
15 | 25 | * @return array{string, string} |
16 | 26 | */ |
17 | | - public function execute(string $version1, string $version2): array |
| 27 | + private function padSegments(string $version1, string $version2): array |
18 | 28 | { |
19 | 29 | $segments1 = explode('.', $version1); |
20 | 30 | $segments2 = explode('.', $version2); |
|
0 commit comments