mirror of
https://github.com/usetrmnl/byos_laravel.git
synced 2026-01-13 15:07:49 +00:00
feat: allow to url_encode array in polling url
This commit is contained in:
parent
aa8d3d1428
commit
40ceba267a
3 changed files with 52 additions and 0 deletions
20
app/Liquid/Filters/StandardFilters.php
Normal file
20
app/Liquid/Filters/StandardFilters.php
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
<?php
|
||||
|
||||
namespace App\Liquid\Filters;
|
||||
|
||||
class StandardFilters extends \Keepsuit\Liquid\Filters\StandardFilters
|
||||
{
|
||||
/**
|
||||
* Converts any URL-unsafe characters in a string to the
|
||||
* [percent-encoded](https://developer.mozilla.org/en-US/docs/Glossary/percent-encoding) equivalent.
|
||||
*/
|
||||
public function urlEncode(string|int|float|array|null $input): string
|
||||
{
|
||||
|
||||
if (is_array($input)) {
|
||||
$input = json_encode($input);
|
||||
}
|
||||
|
||||
return parent::urlEncode($input);
|
||||
}
|
||||
}
|
||||
|
|
@ -7,6 +7,7 @@ use App\Liquid\Filters\Data;
|
|||
use App\Liquid\Filters\Date;
|
||||
use App\Liquid\Filters\Localization;
|
||||
use App\Liquid\Filters\Numbers;
|
||||
use App\Liquid\Filters\StandardFilters;
|
||||
use App\Liquid\Filters\StringMarkup;
|
||||
use App\Liquid\Filters\Uniqueness;
|
||||
use App\Liquid\Tags\TemplateTag;
|
||||
|
|
@ -265,6 +266,7 @@ class Plugin extends Model
|
|||
|
||||
// Use the Liquid template engine to resolve variables
|
||||
$environment = App::make('liquid.environment');
|
||||
$environment->filterRegistry->register(StandardFilters::class);
|
||||
$liquidTemplate = $environment->parseString($template);
|
||||
$context = $environment->newRenderContext(data: $variables);
|
||||
|
||||
|
|
|
|||
|
|
@ -2,7 +2,9 @@
|
|||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Liquid\Filters\StandardFilters;
|
||||
use App\Models\Plugin;
|
||||
use Keepsuit\Liquid\Environment;
|
||||
|
||||
/**
|
||||
* Tests for the Liquid where filter functionality
|
||||
|
|
@ -92,3 +94,31 @@ LIQUID
|
|||
// Should not contain the low tide data
|
||||
$this->assertStringNotContainsString('"type":"L"', $result);
|
||||
});
|
||||
|
||||
it('encodes arrays for url_encode as JSON with spaces after commas and then percent-encodes', function () {
|
||||
/** @var Environment $env */
|
||||
$env = app('liquid.environment');
|
||||
$env->filterRegistry->register(StandardFilters::class);
|
||||
|
||||
$template = $env->parseString('{{ categories | url_encode }}');
|
||||
|
||||
$output = $template->render($env->newRenderContext([
|
||||
'categories' => ['common', 'obscure'],
|
||||
]));
|
||||
|
||||
expect($output)->toBe('%5B%22common%22%2C%22obscure%22%5D');
|
||||
});
|
||||
|
||||
it('keeps scalar url_encode behavior intact', function () {
|
||||
/** @var Environment $env */
|
||||
$env = app('liquid.environment');
|
||||
$env->filterRegistry->register(StandardFilters::class);
|
||||
|
||||
$template = $env->parseString('{{ text | url_encode }}');
|
||||
|
||||
$output = $template->render($env->newRenderContext([
|
||||
'text' => 'hello world',
|
||||
]));
|
||||
|
||||
expect($output)->toBe('hello+world');
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue