Skip to content

Commit fe268d6

Browse files
committed
add normalization of shortened versions
1 parent 2c0a5cd commit fe268d6

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/LaunchDarkly/Operators.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
class Operators
1212
{
1313
const RFC3339 = 'Y-m-d\TH:i:s.uP';
14+
const VERSION_NUMBERS_REGEX = '/^\\d+(\\.\\d+)?(\\.\\d+)?/';
1415

1516
/**
1617
* @param $op string
@@ -143,6 +144,21 @@ public static function parseSemVer($in) {
143144
try {
144145
return Parser::toVersion($in);
145146
} catch (InvalidStringRepresentationException $e) {
147+
// If minor or patch version was omitted, or both, normalize the version
148+
// by adding zeroes in their place.
149+
if (preg_match(Operators::VERSION_NUMBERS_REGEX, $in, $matches)) {
150+
$transformed = $matches[0];
151+
for ($i = 1; $i <= 3; $i++) {
152+
if ($i > count($matches)) {
153+
$transformed .= '.0';
154+
}
155+
}
156+
$transformed .= substr($in, strlen($matches[0]));
157+
try {
158+
return Parser::toVersion($transformed);
159+
} catch (InvalidStringRepresentationException $e) {
160+
}
161+
}
146162
return null;
147163
}
148164
}

tests/OperatorsTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,17 @@ public function testParseTime()
5959
public function testSemVer() {
6060
$this->assertTrue(Operators::apply("semVerEqual", "2.0.0", "2.0.0"));
6161
$this->assertTrue(Operators::apply("semVerEqual", "2.0", "2.0.0"));
62+
$this->assertTrue(Operators::apply("semVerEqual", "2", "2.0.0"));
63+
$this->assertTrue(Operators::apply("semVerEqual", "2-rc1", "2.0.0-rc1"));
64+
$this->assertTrue(Operators::apply("semVerEqual", "2+build2", "2.0.0+build2"));
6265
$this->assertFalse(Operators::apply("semVerEqual", "2.0.0", "2.0.1"));
6366
$this->assertTrue(Operators::apply("semVerLessThan", "2.0.0", "2.0.1"));
6467
$this->assertTrue(Operators::apply("semVerLessThan", "2.0", "2.0.1"));
6568
$this->assertFalse(Operators::apply("semVerLessThan", "2.0.1", "2.0.0"));
69+
$this->assertTrue(Operators::apply("semVerLessThan", "2.0.0-rc", "2.0.0-rc.beta"));
6670
$this->assertTrue(Operators::apply("semVerGreaterThan", "2.0.1", "2.0.0"));
6771
$this->assertFalse(Operators::apply("semVerGreaterThan", "2.0.0", "2.0.1"));
72+
$this->assertTrue(Operators::apply("semVerGreaterThan", "2.0.0-rc.1", "2.0.0-rc.0"));
6873
$this->assertFalse(Operators::apply("semVerLessThan", "2.0.0", "xbad%ver"));
6974
$this->assertFalse(Operators::apply("semVerGreaterThan", "2.0.0", "xbad%ver"));
7075
}

0 commit comments

Comments
 (0)