Skip to content

Commit 5e4b68f

Browse files
Regex segment local evaluation with int trait (#72)
* add failling test * remove cast for regex operator * add non regression test * Simplify logic (#1) --------- Co-authored-by: Vincent Langlet <[email protected]>
1 parent 3f2b3e4 commit 5e4b68f

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

src/Engine/Segments/SegmentConditionModel.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public function matchesTraitValue($traitValue): bool
8383
$castedValue = $this->value;
8484
$traitValueType = gettype($traitValue);
8585

86-
if ($traitValueType == 'boolean') {
86+
if ($traitValueType === 'boolean') {
8787
$castedValue = filter_var($castedValue, FILTER_VALIDATE_BOOLEAN);
8888
} elseif ($this->operator === SegmentConditions::MODULO) {
8989
return $this->matchesModuloTraitValue($traitValue);
@@ -101,7 +101,7 @@ public function matchesTraitValue($traitValue): bool
101101

102102
switch ($this->operator) {
103103
case (SegmentConditions::EQUAL):
104-
$condition = $traitValue == $castedValue;
104+
$condition = $traitValue === $castedValue;
105105
break;
106106
case (SegmentConditions::GREATER_THAN):
107107
$condition = $traitValue > $castedValue;
@@ -116,21 +116,21 @@ public function matchesTraitValue($traitValue): bool
116116
$condition = $traitValue <= $castedValue;
117117
break;
118118
case (SegmentConditions::NOT_EQUAL):
119-
$condition = $traitValue != $castedValue;
119+
$condition = $traitValue !== $castedValue;
120120
break;
121121
case (SegmentConditions::CONTAINS):
122-
$condition = strpos($traitValue, (string) $castedValue) !== false;
122+
$condition = strpos($traitValue, (string) $this->value) !== false;
123123
break;
124124
case (SegmentConditions::NOT_CONTAINS):
125-
$condition = strpos($traitValue, (string) $castedValue) === false;
125+
$condition = strpos($traitValue, (string) $this->value) === false;
126126
break;
127127
case (SegmentConditions::REGEX):
128-
$matchesCount = preg_match_all("/{$castedValue}/", (string) $traitValue);
128+
$matchesCount = preg_match_all("/{$this->value}/", (string) $traitValue);
129129
$condition = $matchesCount && $matchesCount > 0;
130130
break;
131131
case (SegmentConditions::IN):
132132
if (in_array($traitValueType, ['string', 'integer'])) {
133-
$condition = in_array((string) $traitValue, explode(',', (string) $this->value));
133+
$condition = in_array((string) $traitValue, explode(',', (string) $this->value), true);
134134
}
135135
break;
136136
}

tests/Engine/Unit/Segments/SegmentModelsTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,13 @@ public function conditionParametersTraitValues()
5656
[SegmentConditions::CONTAINS, 'bar', 'b', true],
5757
[SegmentConditions::CONTAINS, 'bar', 'bar', true],
5858
[SegmentConditions::CONTAINS, 'bar', 'baz', false],
59+
[SegmentConditions::CONTAINS, 100, '1', true],
60+
[SegmentConditions::CONTAINS, 100, 'baz', false],
5961
[SegmentConditions::NOT_CONTAINS, 'bar', 'b', false],
6062
[SegmentConditions::NOT_CONTAINS, 'bar', 'bar', false],
6163
[SegmentConditions::NOT_CONTAINS, 'bar', 'baz', true],
64+
[SegmentConditions::NOT_CONTAINS, 100, '1', false],
65+
[SegmentConditions::NOT_CONTAINS, 100, 'baz', true],
6266
[SegmentConditions::IN, 'foo', '', false],
6367
[SegmentConditions::IN, 'ba', 'foo,bar', false],
6468
[SegmentConditions::IN, 'foo', 'foo,bar', true],
@@ -74,6 +78,8 @@ public function conditionParametersTraitValues()
7478
[SegmentConditions::REGEX, 'foo', '[a-z]+', true],
7579
[SegmentConditions::REGEX, 'FOO', '[a-z]+', false],
7680
[SegmentConditions::REGEX, '1.2.3', '\\d', true],
81+
[SegmentConditions::REGEX, 123, '^\\d*[13579]$', true],
82+
[SegmentConditions::REGEX, 122, '^\\d*[13579]$', false],
7783
[SegmentConditions::MODULO, 2, '2|0', true],
7884
[SegmentConditions::MODULO, 2.0, '2|0', true],
7985
[SegmentConditions::MODULO, 2.0, '2.0|0', true],

0 commit comments

Comments
 (0)