Skip to content

Commit

Permalink
added: better matching
Browse files Browse the repository at this point in the history
  • Loading branch information
Thijzer committed May 6, 2024
1 parent 28b5dc3 commit 83d438d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
9 changes: 8 additions & 1 deletion src/Component/Statement/ItemPlaceholder.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,16 @@ public static function extract(string $value): string
return rtrim(ltrim($value, '{'), '}');
}

/** Exact match "{test}" */
public static function replace(string $text, array $item): array|string|null
{
return preg_replace_callback('/\{(\w+)\}/', function ($matches) use ($item) {
return $item[self::extract($text)] ?? self::textReplace($text, $item) ?? $text;
}

/** Exact match in text "This is a {test}" (slower) */
public static function textReplace(string $text, array $item): array|string|null
{
return preg_replace_callback('/\{([a-zA-Z0-9.-_]+)\}/', function ($matches) use ($item) {
// $matches[1] is the key inside the brackets
return $item[$matches[1]] ?? $matches[0];
}, $text);
Expand Down
21 changes: 21 additions & 0 deletions tests/Component/Action/StatementActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -349,4 +349,25 @@ public function test_it_should_compare_with_different_operators(): void
]);
$this->assertSame('FALSE', $format->apply($item)['correct']);
}

public function test_it_should_compare_with_different_operators_with_weird_chars(): void
{
$format = new StatementAction();

$item = [
'correct' => 'FALSE',
'Number-high.A_*' => '10',
'Number-low.A_*' => '1',
'number_calc' => '10',
'sku' => '1',
];

$format->setOptions([
'when' => '{Number-high.A_*} GREATER_THAN {Number-low.A_*}',
'then' => [
'correct' => 'TRUE',
],
]);
$this->assertSame('TRUE', $format->apply($item)['correct']);
}
}
10 changes: 5 additions & 5 deletions tests/Component/Statement/ItemPlaceholderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,20 @@ public function testMatches()
$this->assertFalse(ItemPlaceholder::matches('value}'), 'Returns false if the value matches after extraction.');
}

public function testReplace()
public function testTextReplace()
{
$text = 'Hello, {name}!';
$item = ['name' => 'John'];
$this->assertEquals('Hello, John!', ItemPlaceholder::replace($text, $item), 'Replaces placeholder with value from array.');
$this->assertEquals('Hello, John!', ItemPlaceholder::textReplace($text, $item), 'Replaces placeholder with value from array.');

$textWithNoReplacement = 'Hello, {user}!';
$this->assertEquals('Hello, {user}!', ItemPlaceholder::replace($textWithNoReplacement, $item), 'Leaves placeholder if no matching key in array.');
$this->assertEquals('Hello, {user}!', ItemPlaceholder::textReplace($textWithNoReplacement, $item), 'Leaves placeholder if no matching key in array.');

$emptyText = '';
$this->assertEquals('', ItemPlaceholder::replace($emptyText, $item), 'Handles empty string correctly.');
$this->assertEquals('', ItemPlaceholder::textReplace($emptyText, $item), 'Handles empty string correctly.');

$textWithMultiplePlaceholders = '{greeting}, {name}!';
$itemMulti = ['greeting' => 'Hello', 'name' => 'John'];
$this->assertEquals('Hello, John!', ItemPlaceholder::replace($textWithMultiplePlaceholders, $itemMulti), 'Correctly replaces multiple placeholders.');
$this->assertEquals('Hello, John!', ItemPlaceholder::textReplace($textWithMultiplePlaceholders, $itemMulti), 'Correctly replaces multiple placeholders.');
}
}

0 comments on commit 83d438d

Please sign in to comment.