|
30 | 30 |
|
31 | 31 | use GuzzleHttp\Psr7\Utils;
|
32 | 32 | use OpenAPI\Client\Model\ModelInterface;
|
| 33 | +use function GuzzleHttp\Psr7\; |
| 34 | +use function is_array; |
| 35 | +use function is_bool; |
| 36 | +use function substr; |
| 37 | +use const PHP_QUERY_RFC1738; |
| 38 | +use const PHP_QUERY_RFC3986; |
33 | 39 |
|
34 | 40 | /**
|
35 | 41 | * ObjectSerializer Class Doc Comment
|
@@ -545,22 +551,66 @@ public static function deserialize($data, $class, $httpHeaders = null)
|
545 | 551 | }
|
546 | 552 |
|
547 | 553 | /**
|
548 |
| - * Native `http_build_query` wrapper. |
549 |
| - * @see https://www.php.net/manual/en/function.http-build-query |
| 554 | + * Build a query string from an array of key value pairs. |
550 | 555 | *
|
551 |
| - * @param array|object $data May be an array or object containing properties. |
552 |
| - * @param string $numeric_prefix If numeric indices are used in the base array and this parameter is provided, it will be prepended to the numeric index for elements in the base array only. |
553 |
| - * @param string|null $arg_separator arg_separator.output is used to separate arguments but may be overridden by specifying this parameter. |
554 |
| - * @param int $encoding_type Encoding type. By default, PHP_QUERY_RFC1738. |
| 556 | + * This function can use the return value of `parse()` to build a query |
| 557 | + * string. This function does not modify the provided keys when an array is |
| 558 | + * encountered (like `http_build_query()` would). |
555 | 559 | *
|
556 |
| - * @return string |
| 560 | + * The function is copied from https://github.com/guzzle/psr7/blob/a243f80a1ca7fe8ceed4deee17f12c1930efe662/src/Query.php#L59-L112 |
| 561 | + * with a modification which is described in https://github.com/guzzle/psr7/pull/603 |
| 562 | + * |
| 563 | + * @param array $params Query string parameters. |
| 564 | + * @param int|false $encoding Set to false to not encode, PHP_QUERY_RFC3986 |
| 565 | + * to encode using RFC3986, or PHP_QUERY_RFC1738 |
| 566 | + * to encode using RFC1738. |
| 567 | + * @param bool $treatBooleansAsInts When `true` values are cast to int (e.g. ['foo' => false] gives `foo=0`). |
| 568 | + * When `false` values are cast to strings (e.g. ['foo' => false] gives `foo=false`). |
557 | 569 | */
|
558 |
| - public static function buildQuery( |
559 |
| - $data, |
560 |
| - string $numeric_prefix = '', |
561 |
| - ?string $arg_separator = null, |
562 |
| - int $encoding_type = \PHP_QUERY_RFC3986 |
563 |
| - ): string { |
564 |
| - return \GuzzleHttp\Psr7\Query::build($data, $encoding_type); |
| 570 | + public static function buildQuery(array $params, $encoding = PHP_QUERY_RFC3986, bool $treatBooleansAsInts = true): string |
| 571 | + { |
| 572 | + if (!$params) { |
| 573 | + return ''; |
| 574 | + } |
| 575 | + |
| 576 | + if ($encoding === false) { |
| 577 | + $encoder = function (string $str): string { |
| 578 | + return $str; |
| 579 | + }; |
| 580 | + } elseif ($encoding === PHP_QUERY_RFC3986) { |
| 581 | + $encoder = 'rawurlencode'; |
| 582 | + } elseif ($encoding === PHP_QUERY_RFC1738) { |
| 583 | + $encoder = 'urlencode'; |
| 584 | + } else { |
| 585 | + throw new \InvalidArgumentException('Invalid type'); |
| 586 | + } |
| 587 | + |
| 588 | + $castBool = $treatBooleansAsInts |
| 589 | + ? function ($v) { return (int) $v; } |
| 590 | + : function ($v) { return $v ? 'true' : 'false'; }; |
| 591 | + |
| 592 | + $qs = ''; |
| 593 | + foreach ($params as $k => $v) { |
| 594 | + $k = $encoder((string) $k); |
| 595 | + if (!is_array($v)) { |
| 596 | + $qs .= $k; |
| 597 | + $v = is_bool($v) ? $castBool($v) : $v; |
| 598 | + if ($v !== null) { |
| 599 | + $qs .= '='.$encoder((string) $v); |
| 600 | + } |
| 601 | + $qs .= '&'; |
| 602 | + } else { |
| 603 | + foreach ($v as $vv) { |
| 604 | + $qs .= $k; |
| 605 | + $vv = is_bool($vv) ? $castBool($vv) : $vv; |
| 606 | + if ($vv !== null) { |
| 607 | + $qs .= '='.$encoder((string) $vv); |
| 608 | + } |
| 609 | + $qs .= '&'; |
| 610 | + } |
| 611 | + } |
| 612 | + } |
| 613 | + |
| 614 | + return $qs ? (string) substr($qs, 0, -1) : ''; |
565 | 615 | }
|
566 | 616 | }
|
0 commit comments