26 lines
544 B
PHP
26 lines
544 B
PHP
<?php
|
|
|
|
namespace App\Support\Danal;
|
|
|
|
final class Nvp
|
|
{
|
|
public function build(array $data): string
|
|
{
|
|
$pairs = [];
|
|
foreach ($data as $k => $v) {
|
|
$pairs[] = $k . '=' . urlencode((string)$v);
|
|
}
|
|
return implode('&', $pairs);
|
|
}
|
|
|
|
public function parse(string $str): array
|
|
{
|
|
$out = [];
|
|
foreach (explode('&', $str) as $tok) {
|
|
$kv = explode('=', $tok, 2);
|
|
if (count($kv) === 2) $out[$kv[0]] = urldecode($kv[1]);
|
|
}
|
|
return $out;
|
|
}
|
|
}
|