Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.x] Add to_qs modifier #10196

Merged
merged 2 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/Modifiers/CoreModifiers.php
Original file line number Diff line number Diff line change
Expand Up @@ -2549,6 +2549,16 @@ public function toJson($value, $params)
return json_encode($value, $options);
}

/**
* Converts the data to a query string.
*
* @return string
*/
public function toQs($value)
{
return Arr::query($value);
}

/**
* Converts each tab in the string to some number of spaces, as defined by
* $param[0]. By default, each tab is converted to 4 consecutive spaces.
Expand Down
37 changes: 37 additions & 0 deletions tests/Modifiers/ToQsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Modifiers;

use Statamic\Modifiers\Modify;
use Tests\PreventSavingStacheItemsToDisk;
use Tests\TestCase;

class ToQsTest extends TestCase
{
use PreventSavingStacheItemsToDisk;

/**
* @test
*/
public function it_converts_to_query_string(): void
{
$input = [
'mode' => 'plaid',
'area' => [51, 52],
'hat' => null,
'transportation' => [
'bike' => true,
'delorian' => false,
],
];
$modified = $this->modify(value($input));

$expected = 'mode=plaid&area%5B0%5D=51&area%5B1%5D=52&transportation%5Bbike%5D=1&transportation%5Bdelorian%5D=0';
$this->assertEquals($expected, $modified);
}

private function modify($value, $options = [])
{
return Modify::value($value)->toQs($options)->fetch();
}
}
Loading