-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from php-etl/feature/replace-expression
add a "mapValues" expression that will do a preg_match
- Loading branch information
Showing
3 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Kiboko\Component\ArrayExpressionLanguage; | ||
|
||
use Symfony\Component\ExpressionLanguage\ExpressionFunction; | ||
|
||
class MapValues extends ExpressionFunction | ||
{ | ||
public function __construct($name) | ||
{ | ||
parent::__construct( | ||
$name, | ||
$this->compile(...)->bindTo($this), | ||
$this->evaluate(...)->bindTo($this) | ||
); | ||
} | ||
|
||
private function compile(string $input, string $values): string | ||
{ | ||
return <<<PHP | ||
(function () use(\$input) { | ||
\$inputs = {$input}; | ||
\$patterns = []; | ||
\$replacements = []; | ||
foreach ({$values} as \$pattern => \$replacement) { | ||
\$patterns[] = '('.\$pattern.')'; | ||
\$replacements[] = \$replacement; | ||
} | ||
\$pattern = '/^' . implode('|', \$patterns) . '$/'; | ||
if (is_iterable(\$inputs)) { | ||
foreach (\$inputs as \$key => \$input) { | ||
preg_match(\$pattern, (string) \$input, \$matches); | ||
if (empty(\$matches)) { | ||
throw new \Exception(sprintf( | ||
'No replacement found for value "%s". Expected values: %s', | ||
\$input, | ||
implode(', ', array_keys(\$replacements)) | ||
)); | ||
} | ||
array_shift(\$matches); | ||
\$inputs[\$key] = \$replacements[array_keys(array_filter(\$matches))[0]]; | ||
} | ||
} | ||
return \$inputs; | ||
})() | ||
PHP; | ||
} | ||
|
||
private function evaluate(array $context, array $input, array $values) | ||
{ | ||
$inputs = $input; | ||
$patterns = []; | ||
$replacements = []; | ||
foreach ($values as $pattern => $replacement) { | ||
$patterns[] = '('.$pattern.')'; | ||
$replacements[] = $replacement; | ||
} | ||
|
||
$pattern = '/^' . implode('|', $patterns) . '$/'; | ||
|
||
if (is_iterable($inputs)) { | ||
foreach ($inputs as $key => $input) { | ||
preg_match($pattern, (string) $input, $matches); | ||
|
||
if (empty($matches)) { | ||
throw new \Exception(sprintf( | ||
'No replacement found for value "%s". Expected values: %s', | ||
$input, | ||
implode(', ', array_keys($replacements)) | ||
)); | ||
} | ||
|
||
array_shift($matches); | ||
$inputs[$key] = $replacements[array_keys(array_filter($matches))[0]]; | ||
} | ||
} | ||
|
||
return $inputs; | ||
} | ||
} |