Http.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. // Copyright 2019 Hackware SpA <human@hackware.cl>
  3. // This file is part of "Hackware Web Services Payment" and licensed under
  4. // the terms of the GNU Affero General Public License version 3, or (at your
  5. // option) a later version. You should have received a copy of this license
  6. // along with the software. If not, see <https://www.gnu.org/licenses/>.
  7. namespace Hawese\Payment\Support;
  8. use Nyholm\Psr7\Factory\Psr17Factory as Psr17UriFactory;
  9. use Psr\Http\Message\ResponseInterface;
  10. class Http
  11. {
  12. /**
  13. * For GET requests, spaces are percent encoded (%20)
  14. *
  15. * @param array $queryParams associative array.
  16. */
  17. public static function buildUriQuery(array $queryParams): string
  18. {
  19. return http_build_query($queryParams, '', '&', PHP_QUERY_RFC3986);
  20. }
  21. /**
  22. * For application/x-www-form-urlencoded, spaces are encoded as + signs
  23. */
  24. public static function buildFormQuery(array $bodyParams): string
  25. {
  26. return http_build_query($bodyParams, '', '&', PHP_QUERY_RFC1738);
  27. }
  28. public static function getJsonBody(ResponseInterface &$response): object
  29. {
  30. return json_decode((string) $response->getBody());
  31. }
  32. public static function appendQueryParams(
  33. string $url,
  34. array $queryParams
  35. ): string {
  36. $uri = (new Psr17UriFactory())->createUri($url);
  37. $urlQueryString = $uri->getQuery();
  38. $queryParamsString = self::buildUriQuery($queryParams);
  39. return (string) $uri->withQuery(
  40. empty($urlQueryString)
  41. ? $queryParamsString
  42. : "$urlQueryString&$UqueryParams"
  43. );
  44. }
  45. }