Skip to content

Commit

Permalink
Add url_decode Twig Filter
Browse files Browse the repository at this point in the history
  • Loading branch information
bobdenotter committed Aug 19, 2020
1 parent 9297dc2 commit 29b5527
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion ecs.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
return static function (ContainerConfigurator $containerConfigurator): void {
$parameters = $containerConfigurator->parameters();

$parameters->set('sets', ['clean-code', 'common', 'php70', 'php71', 'psr12', 'symfony', 'symfony-risky']);
$parameters->set('sets', ['clean-code', 'common', 'php-70', 'php-71', 'psr-12', 'symfony', 'symfony-risky']);

$parameters->set('paths', [
__DIR__ . '/src',
Expand Down
20 changes: 20 additions & 0 deletions src/Twig/TextExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public function getFilters(): array
new TwigFilter('ucwords', [$this, 'ucwords']),
new TwigFilter('preg_replace', [$this, 'pregReplace']),
new TwigFilter('format_bytes', [$this, 'formatBytes']),
new TwigFilter('url_decode', [$this, 'urlDecode']),
];
}

Expand Down Expand Up @@ -84,4 +85,23 @@ public function formatBytes(int $bytes, int $precision = 2): string

return round($bytes, $precision) . ' ' . $units[$pow];
}

public function urlDecode(string $string)
{
if (! mb_strpos($string, '=')) {
return urldecode($string);
}

$params = [];

foreach (explode('&', $string) as $chunk) {
$param = explode('=', $chunk);

if ($param) {
$params[urldecode($param[0])] = urldecode($param[1]);
}
}

return $params;
}
}

0 comments on commit 29b5527

Please sign in to comment.