3v4l.org

run code in 300+ PHP versions simultaneously
<?php namespace WoohooLabs\Yang\JsonApi\Request; use Psr\Http\Message\RequestInterface; class RequestBuilder { /** * @var \Psr\Http\Message\RequestInterface */ protected $request; /** * @var string */ protected $method; /** * @var array */ protected $queryString; /** * @param \Psr\Http\Message\RequestInterface $request */ public function __construct() { $this->initialize(); } public function initialize() { $this->method = "GET"; $this->queryString = []; } public function fetch() { $this->method = "GET"; } public function create() { $this->method = "POST"; } public function update() { $this->method = "PATCH"; } public function delete() { $this->method = "DELETE"; } /** * @param array|string $fields */ public function fields($fields) { $this->setQueryParam("fields", $fields); } /** * @param array|string $fields */ public function sort($fields) { $this->setQueryParam("sort", $fields); } /** * @param array|string $paginate */ public function paginate($paginate) { $this->setQueryParam("page", $paginate); } /** * @param array|string $filter */ public function filter($filter) { $this->setQueryParam("filter", $filter); } /** * @param array|string $includes */ public function includes($includes) { if (is_array($includes)) { $this->queryString["includes"] = implode(",", $includes); } else { $this->queryString["includes"] = $includes; } } /** * @return \Psr\Http\Message\RequestInterface */ public function getRequest() { $request = $this->request->withMethod($this->method); $request = $request->withUri($request->getUri()->withQuery($this->getQueryString())); return $request; } /** * @return string */ public function getQueryString() { return http_build_query($this->queryString); } /** * @param string $name * @param array|string $queryParam */ protected function setQueryParam($name, $queryParam) { if (is_array($queryParam)) { foreach ($queryParam as $key => $value) { $this->queryString[$name][$key] = $queryParam; } } else { $this->queryString[$name] = $queryParam; } } /** * @param array $array * @return bool */ protected function isAssociativeArray(array $array) { return (bool)count(array_filter(array_keys($array), 'is_string')); } } $builder = new RequestBuilder(); $builder->fields(["kaka" => "1,2,3,4", "shohoh" => "6,5,4,3"]); echo url_decode($builder->getQueryString());

preferences:
74.26 ms | 402 KiB | 5 Q