Skip to content

Commit

Permalink
Regex segment local evaluation with int trait (#72)
Browse files Browse the repository at this point in the history
* add failling test

* remove cast for regex operator

* add non regression test

* Simplify logic (#1)

---------

Co-authored-by: Vincent Langlet <[email protected]>
  • Loading branch information
floranpagliai and VincentLanglet authored May 24, 2024
1 parent 3f2b3e4 commit 5e4b68f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/Engine/Segments/SegmentConditionModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function matchesTraitValue($traitValue): bool
$castedValue = $this->value;
$traitValueType = gettype($traitValue);

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

switch ($this->operator) {
case (SegmentConditions::EQUAL):
$condition = $traitValue == $castedValue;
$condition = $traitValue === $castedValue;
break;
case (SegmentConditions::GREATER_THAN):
$condition = $traitValue > $castedValue;
Expand All @@ -116,21 +116,21 @@ public function matchesTraitValue($traitValue): bool
$condition = $traitValue <= $castedValue;
break;
case (SegmentConditions::NOT_EQUAL):
$condition = $traitValue != $castedValue;
$condition = $traitValue !== $castedValue;
break;
case (SegmentConditions::CONTAINS):
$condition = strpos($traitValue, (string) $castedValue) !== false;
$condition = strpos($traitValue, (string) $this->value) !== false;
break;
case (SegmentConditions::NOT_CONTAINS):
$condition = strpos($traitValue, (string) $castedValue) === false;
$condition = strpos($traitValue, (string) $this->value) === false;
break;
case (SegmentConditions::REGEX):
$matchesCount = preg_match_all("/{$castedValue}/", (string) $traitValue);
$matchesCount = preg_match_all("/{$this->value}/", (string) $traitValue);
$condition = $matchesCount && $matchesCount > 0;
break;
case (SegmentConditions::IN):
if (in_array($traitValueType, ['string', 'integer'])) {
$condition = in_array((string) $traitValue, explode(',', (string) $this->value));
$condition = in_array((string) $traitValue, explode(',', (string) $this->value), true);
}
break;
}
Expand Down
6 changes: 6 additions & 0 deletions tests/Engine/Unit/Segments/SegmentModelsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,13 @@ public function conditionParametersTraitValues()
[SegmentConditions::CONTAINS, 'bar', 'b', true],
[SegmentConditions::CONTAINS, 'bar', 'bar', true],
[SegmentConditions::CONTAINS, 'bar', 'baz', false],
[SegmentConditions::CONTAINS, 100, '1', true],
[SegmentConditions::CONTAINS, 100, 'baz', false],
[SegmentConditions::NOT_CONTAINS, 'bar', 'b', false],
[SegmentConditions::NOT_CONTAINS, 'bar', 'bar', false],
[SegmentConditions::NOT_CONTAINS, 'bar', 'baz', true],
[SegmentConditions::NOT_CONTAINS, 100, '1', false],
[SegmentConditions::NOT_CONTAINS, 100, 'baz', true],
[SegmentConditions::IN, 'foo', '', false],
[SegmentConditions::IN, 'ba', 'foo,bar', false],
[SegmentConditions::IN, 'foo', 'foo,bar', true],
Expand All @@ -74,6 +78,8 @@ public function conditionParametersTraitValues()
[SegmentConditions::REGEX, 'foo', '[a-z]+', true],
[SegmentConditions::REGEX, 'FOO', '[a-z]+', false],
[SegmentConditions::REGEX, '1.2.3', '\\d', true],
[SegmentConditions::REGEX, 123, '^\\d*[13579]$', true],
[SegmentConditions::REGEX, 122, '^\\d*[13579]$', false],
[SegmentConditions::MODULO, 2, '2|0', true],
[SegmentConditions::MODULO, 2.0, '2|0', true],
[SegmentConditions::MODULO, 2.0, '2.0|0', true],
Expand Down

0 comments on commit 5e4b68f

Please sign in to comment.