Skip to content

Commit 8e1df8c

Browse files
authored
feat: add segment IN operator (#72)
feat: add segment `IN` operator
2 parents f1cc332 + 0b8f20c commit 8e1df8c

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

src/Engine/Segments/SegmentConditionModel.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,14 @@ public function matchesTraitValue($traitValue): bool
8080
{
8181
$condition = false;
8282
$castedValue = $this->value;
83+
$traitValueType = gettype($traitValue);
8384

84-
if ('boolean' === gettype($traitValue)) {
85+
if ($traitValueType == 'boolean') {
8586
$castedValue = filter_var($castedValue, FILTER_VALIDATE_BOOLEAN);
8687
} elseif ($this->operator === SegmentConditions::MODULO) {
8788
return $this->matchesModuloTraitValue($traitValue);
8889
} else {
89-
settype($castedValue, gettype($traitValue));
90+
settype($castedValue, $traitValueType);
9091
}
9192

9293
if (is_string($castedValue) && Semver::isSemver($castedValue)) {
@@ -126,6 +127,11 @@ public function matchesTraitValue($traitValue): bool
126127
$matchesCount = preg_match_all("/{$castedValue}/", (string) $traitValue);
127128
$condition = $matchesCount && $matchesCount > 0;
128129
break;
130+
case (SegmentConditions::IN):
131+
if (in_array($traitValueType, ['string', 'integer'])) {
132+
$condition = in_array((string) $traitValue, explode(',', (string) $this->value));
133+
}
134+
break;
129135
}
130136

131137
return $condition;

src/Engine/Segments/SegmentConditions.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ class SegmentConditions
1717
public const IS_SET = 'IS_SET';
1818
public const IS_NOT_SET = 'IS_NOT_SET';
1919
public const MODULO = 'MODULO';
20+
public const IN = 'IN';
2021
}

tests/Engine/Unit/Segments/SegmentModelsTest.php

100644100755
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,18 @@ public function conditionParametersTraitValues()
5959
[SegmentConditions::NOT_CONTAINS, 'bar', 'b', false],
6060
[SegmentConditions::NOT_CONTAINS, 'bar', 'bar', false],
6161
[SegmentConditions::NOT_CONTAINS, 'bar', 'baz', true],
62+
[SegmentConditions::IN, 'foo', '', false],
63+
[SegmentConditions::IN, 'ba', 'foo,bar', false],
64+
[SegmentConditions::IN, 'foo', 'foo,bar', true],
65+
[SegmentConditions::IN, 'bar', 'foo,bar', true],
66+
[SegmentConditions::IN, 'foo', 'foo', true],
67+
[SegmentConditions::IN, 1, '1,2,3,4', true],
68+
[SegmentConditions::IN, 1, '', false],
69+
[SegmentConditions::IN, 1, '1', true],
70+
// Flagsmith's engine does not evaluate `IN` condition for floats/doubles and booleans
71+
// due to ambiguous serialization across supported platforms.
72+
[SegmentConditions::IN, 1.5, '1.5', false],
73+
[SegmentConditions::IN, false, 'false', false],
6274
[SegmentConditions::REGEX, 'foo', '[a-z]+', true],
6375
[SegmentConditions::REGEX, 'FOO', '[a-z]+', false],
6476
[SegmentConditions::REGEX, '1.2.3', '\\d', true],

0 commit comments

Comments
 (0)