Skip to content

Commit f606df6

Browse files
authored
Merge pull request #229 from splitio/sdks-8322-semver
Semver class implementation
2 parents 7510a45 + b0a6045 commit f606df6

19 files changed

+1196
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
namespace SplitIO\Exception;
3+
4+
class SemverParseException extends \LogicException
5+
{
6+
7+
}

src/SplitIO/Grammar/Condition/Matcher.php

+35
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,14 @@
44
use SplitIO\Exception\UnsupportedMatcherException;
55
use SplitIO\Grammar\Condition\Matcher\All;
66
use SplitIO\Grammar\Condition\Matcher\Between;
7+
use SplitIO\Grammar\Condition\Matcher\BetweenSemver;
78
use SplitIO\Grammar\Condition\Matcher\EqualTo;
9+
use SplitIO\Grammar\Condition\Matcher\EqualToSemver;
810
use SplitIO\Grammar\Condition\Matcher\GreaterThanOrEqualTo;
11+
use SplitIO\Grammar\Condition\Matcher\GreaterThanOrEqualToSemver;
12+
use SplitIO\Grammar\Condition\Matcher\InListSemver;
913
use SplitIO\Grammar\Condition\Matcher\LessThanOrEqualTo;
14+
use SplitIO\Grammar\Condition\Matcher\LessThanOrEqualToSemver;
1015
use SplitIO\Grammar\Condition\Matcher\Segment;
1116
use SplitIO\Grammar\Condition\Matcher\Whitelist;
1217
use SplitIO\Grammar\Condition\Matcher\StartsWith;
@@ -40,6 +45,11 @@ class Matcher
4045
const IN_SPLIT_TREATMENT = 'IN_SPLIT_TREATMENT';
4146
const EQUAL_TO_BOOLEAN = 'EQUAL_TO_BOOLEAN';
4247
const MATCHES_STRING = 'MATCHES_STRING';
48+
const EQUAL_TO_SEMVER = 'EQUAL_TO_SEMVER';
49+
const GREATER_THAN_OR_EQUAL_TO_SEMVER = 'GREATER_THAN_OR_EQUAL_TO_SEMVER';
50+
const LESS_THAN_OR_EQUAL_TO_SEMVER = 'LESS_THAN_OR_EQUAL_TO_SEMVER';
51+
const BETWEEN_SEMVER = 'BETWEEN_SEMVER';
52+
const IN_LIST_SEMVER = 'IN_LIST_SEMVER';
4353

4454
public static function factory($matcher)
4555
{
@@ -130,6 +140,31 @@ public static function factory($matcher)
130140
is_string($matcher['stringMatcherData']) ?
131141
$matcher['stringMatcherData'] : null;
132142
return new Regex($data, $negate, $attribute);
143+
case self::EQUAL_TO_SEMVER:
144+
$data = isset($matcher['stringMatcherData']) &&
145+
is_string($matcher['stringMatcherData']) ?
146+
$matcher['stringMatcherData'] : null;
147+
return new EqualToSemver($data, $negate, $attribute);
148+
case self::GREATER_THAN_OR_EQUAL_TO_SEMVER:
149+
$data = isset($matcher['stringMatcherData']) &&
150+
is_string($matcher['stringMatcherData']) ?
151+
$matcher['stringMatcherData'] : null;
152+
return new GreaterThanOrEqualToSemver($data, $negate, $attribute);
153+
case self::LESS_THAN_OR_EQUAL_TO_SEMVER:
154+
$data = isset($matcher['stringMatcherData']) &&
155+
is_string($matcher['stringMatcherData']) ?
156+
$matcher['stringMatcherData'] : null;
157+
return new LessThanOrEqualToSemver($data, $negate, $attribute);
158+
case self::BETWEEN_SEMVER:
159+
$data = (isset($matcher['betweenStringMatcherData']) &&
160+
is_array($matcher['betweenStringMatcherData']))
161+
? $matcher['betweenStringMatcherData'] : null;
162+
return new BetweenSemver($data, $negate, $attribute);
163+
case self::IN_LIST_SEMVER:
164+
$data = (isset($matcher['whitelistMatcherData']['whitelist']) &&
165+
is_array($matcher['whitelistMatcherData']['whitelist']))
166+
? $matcher['whitelistMatcherData']['whitelist'] : null;
167+
return new InListSemver($data, $negate, $attribute);
133168
// @codeCoverageIgnoreStart
134169
default:
135170
throw new UnsupportedMatcherException("Unable to create matcher for matcher type: " . $matcherType);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
namespace SplitIO\Grammar\Condition\Matcher;
3+
4+
use SplitIO\Grammar\Condition\Matcher;
5+
use SplitIO\Grammar\Semver\Semver;
6+
use SplitIO\Grammar\Semver\SemverComparer;
7+
use SplitIO\Split as SplitApp;
8+
9+
class BetweenSemver extends AbstractMatcher
10+
{
11+
protected $startTarget = null;
12+
protected $endTarget = null;
13+
14+
public function __construct($data, $negate = false, $attribute = null)
15+
{
16+
parent::__construct(Matcher::BETWEEN_SEMVER, $negate, $attribute);
17+
18+
$this->startTarget = Semver::build($data['start']);
19+
$this->endTarget = Semver::build($data['end']);
20+
}
21+
22+
/**
23+
*
24+
* @param mixed $key
25+
*/
26+
protected function evalKey($key)
27+
{
28+
if ($key == null || !is_string($key) || $this->startTarget == null || $this->endTarget == null) {
29+
return false;
30+
}
31+
32+
$keySemver = Semver::build($key);
33+
if ($keySemver == null) {
34+
return false;
35+
}
36+
37+
$result = SemverComparer::do($keySemver, $this->startTarget) >= 0
38+
&& SemverComparer::do($keySemver, $this->endTarget) <= 0;
39+
40+
SplitApp::logger()->debug($this->startTarget->getVersion() . " <= "
41+
. $keySemver->getVersion() . " <= " . $this->endTarget->getVersion()
42+
. " | Result: " . $result);
43+
44+
return $result;
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
namespace SplitIO\Grammar\Condition\Matcher;
3+
4+
use SplitIO\Grammar\Condition\Matcher;
5+
use SplitIO\Grammar\Semver\Semver;
6+
use SplitIO\Grammar\Semver\SemverComparer;
7+
use SplitIO\Split as SplitApp;
8+
9+
class EqualToSemver extends AbstractMatcher
10+
{
11+
private $toCompare;
12+
13+
public function __construct($toCompare, $negate = false, $attribute = null)
14+
{
15+
parent::__construct(Matcher::EQUAL_TO_SEMVER, $negate, $attribute);
16+
17+
$this->toCompare = Semver::build($toCompare);
18+
}
19+
20+
/**
21+
*
22+
* @param mixed $key
23+
*/
24+
protected function evalKey($key)
25+
{
26+
if ($key == null || $this->toCompare == null || !is_string($key)) {
27+
return false;
28+
}
29+
30+
$keySemver = Semver::build($key);
31+
if ($keySemver == null) {
32+
return false;
33+
}
34+
35+
$result = SemverComparer::equals($this->toCompare, $keySemver);
36+
37+
SplitApp::logger()->debug($this->toCompare->getVersion() . " == "
38+
. $keySemver->getVersion() . " | Result: " . $result);
39+
40+
return $result;
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
namespace SplitIO\Grammar\Condition\Matcher;
3+
4+
use SplitIO\Grammar\Condition\Matcher;
5+
use SplitIO\Grammar\Semver\Semver;
6+
use SplitIO\Grammar\Semver\SemverComparer;
7+
use SplitIO\Split as SplitApp;
8+
9+
class GreaterThanOrEqualToSemver extends AbstractMatcher
10+
{
11+
private $target;
12+
13+
public function __construct($toCompare, $negate = false, $attribute = null)
14+
{
15+
parent::__construct(Matcher::GREATER_THAN_OR_EQUAL_TO_SEMVER, $negate, $attribute);
16+
17+
$this->target = Semver::build($toCompare);
18+
}
19+
20+
/**
21+
*
22+
* @param mixed $key
23+
*/
24+
protected function evalKey($key)
25+
{
26+
if ($key == null || $this->target == null || !is_string($key)) {
27+
return false;
28+
}
29+
30+
$keySemver = Semver::build($key);
31+
if ($keySemver == null) {
32+
return false;
33+
}
34+
35+
$result = SemverComparer::do($keySemver, $this->target) >= 0;
36+
37+
SplitApp::logger()->debug($this->target->getVersion() . " >= "
38+
. $keySemver->getVersion() . " | Result: " . $result);
39+
40+
return $result;
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
namespace SplitIO\Grammar\Condition\Matcher;
3+
4+
use SplitIO\Grammar\Condition\Matcher;
5+
use SplitIO\Grammar\Semver\Semver;
6+
use SplitIO\Grammar\Semver\SemverComparer;
7+
8+
class InListSemver extends AbstractMatcher
9+
{
10+
private $targetList;
11+
12+
public function __construct($targetList, $negate = false, $attribute = null)
13+
{
14+
$this->targetList = array();
15+
parent::__construct(Matcher::IN_LIST_SEMVER, $negate, $attribute);
16+
17+
if (is_array($targetList)) {
18+
foreach ($targetList as $item) {
19+
$toAdd = Semver::build($item);
20+
21+
if ($toAdd != null) {
22+
array_push($this->targetList, $toAdd);
23+
}
24+
}
25+
}
26+
}
27+
28+
/**
29+
*
30+
* @param mixed $key
31+
*/
32+
protected function evalKey($key)
33+
{
34+
if ($key == null || !is_string($key) || count($this->targetList) == 0) {
35+
return false;
36+
}
37+
38+
$keySemver = Semver::build($key);
39+
if ($keySemver == null) {
40+
return false;
41+
}
42+
43+
foreach ($this->targetList as $item) {
44+
if (SemverComparer::equals($keySemver, $item)) {
45+
return true;
46+
}
47+
}
48+
49+
return false;
50+
}
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
namespace SplitIO\Grammar\Condition\Matcher;
3+
4+
use SplitIO\Grammar\Condition\Matcher;
5+
use SplitIO\Grammar\Semver\Semver;
6+
use SplitIO\Grammar\Semver\SemverComparer;
7+
use SplitIO\Split as SplitApp;
8+
9+
class LessThanOrEqualToSemver extends AbstractMatcher
10+
{
11+
private $target;
12+
13+
public function __construct($toCompare, $negate = false, $attribute = null)
14+
{
15+
parent::__construct(Matcher::LESS_THAN_OR_EQUAL_TO_SEMVER, $negate, $attribute);
16+
17+
$this->target = Semver::build($toCompare);
18+
}
19+
20+
/**
21+
*
22+
* @param mixed $key
23+
*/
24+
protected function evalKey($key)
25+
{
26+
if ($key == null || $this->target == null || !is_string($key)) {
27+
return false;
28+
}
29+
30+
$keySemver = Semver::build($key);
31+
if ($keySemver == null) {
32+
return false;
33+
}
34+
35+
$result = SemverComparer::do($keySemver, $this->target) <= 0;
36+
37+
SplitApp::logger()->debug($this->target->getVersion() . " <= "
38+
. $keySemver->getVersion() . " | Result: " . $result);
39+
40+
return $result;
41+
}
42+
}

0 commit comments

Comments
 (0)