-
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.
- Loading branch information
Showing
4 changed files
with
157 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
|
||
namespace BenTools\QueryString\Renderer; | ||
|
||
use BenTools\QueryString\QueryString; | ||
|
||
final class FlatRenderer implements QueryStringRendererInterface | ||
{ | ||
/** | ||
* @var NativeRenderer | ||
*/ | ||
private $renderer; | ||
|
||
protected function __construct(QueryStringRendererInterface $renderer = null) | ||
{ | ||
$this->renderer = $renderer; | ||
} | ||
|
||
public static function factory(QueryStringRendererInterface $renderer = null) | ||
{ | ||
return new self($renderer ?? NativeRenderer::factory()); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function render(QueryString $queryString): string | ||
{ | ||
$separator = $this->getSeparator() ?? ini_get('arg_separator.output'); | ||
$parts = [[]]; | ||
|
||
foreach ($queryString->getParams() as $key => $value) { | ||
$parts[] = $this->getParts($key, $value); | ||
} | ||
|
||
return \implode($separator, \array_merge([], ...$parts)); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function getEncoding(): int | ||
{ | ||
return $this->renderer->getEncoding(); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function withEncoding(int $encoding): QueryStringRendererInterface | ||
{ | ||
return new self($this->renderer->withEncoding($encoding)); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function getSeparator(): ?string | ||
{ | ||
return $this->renderer->getSeparator(); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function withSeparator(?string $separator): QueryStringRendererInterface | ||
{ | ||
return new self($this->renderer->withSeparator($separator)); | ||
} | ||
|
||
private function getParts($key, $value): array | ||
{ | ||
if (\is_iterable($value)) { | ||
$parts = [[]]; | ||
foreach ($value as $sub) { | ||
$parts[] = $this->getParts($key, $sub); | ||
} | ||
|
||
return \array_merge([], ...$parts); | ||
} | ||
|
||
$encode = \PHP_QUERY_RFC1738 === $this->getEncoding() ? '\\urlencode' : '\\rawurlencode'; | ||
|
||
return [$key . '=' . \call_user_func($encode, $value)]; | ||
} | ||
} |
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,41 @@ | ||
<?php | ||
|
||
namespace BenTools\QueryString\Tests; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use function BenTools\QueryString\flat; | ||
use function BenTools\QueryString\query_string; | ||
|
||
class FlatRendererTest extends TestCase | ||
{ | ||
public function testRenderer() | ||
{ | ||
$data = [ | ||
'foo' => 'bar', | ||
'foos' => [ | ||
'bar', | ||
'foo bar', | ||
], | ||
'fruits' => [ | ||
'banana' => 'yellow', | ||
'strawberry' => 'red', | ||
], | ||
]; | ||
|
||
$qs = query_string($data); | ||
$renderer = flat(); | ||
|
||
$this->assertEquals('foo=bar&foos=bar&foos=foo%20bar&fruits=yellow&fruits=red', (string) $qs->withRenderer( | ||
$renderer | ||
)); | ||
|
||
$this->assertEquals('foo=bar&foos=bar&foos=foo+bar&fruits=yellow&fruits=red', (string) $qs->withRenderer( | ||
$renderer->withEncoding(PHP_QUERY_RFC1738) | ||
)); | ||
|
||
$this->assertEquals('foo=bar;foos=bar;foos=foo+bar;fruits=yellow;fruits=red', (string) $qs->withRenderer( | ||
$renderer->withEncoding(PHP_QUERY_RFC1738)->withSeparator(';') | ||
)); | ||
} | ||
|
||
} |