diff --git a/example/01-basic-fetch.php b/example/01-basic-fetch.php index bf9b730..3a82f8a 100644 --- a/example/01-basic-fetch.php +++ b/example/01-basic-fetch.php @@ -2,7 +2,7 @@ require(implode(DIRECTORY_SEPARATOR, ["..", "vendor", "autoload.php"])); use Gt\Fetch\Http; -use Gt\Fetch\BodyResponse; +use Gt\Fetch\Response\BodyResponse; /* * This example fetches the list of repositories in the PhpGt organisation from diff --git a/example/02-post-request.php b/example/02-post-request.php index b577814..ed23ed4 100644 --- a/example/02-post-request.php +++ b/example/02-post-request.php @@ -2,7 +2,8 @@ require(implode(DIRECTORY_SEPARATOR, ["..", "vendor", "autoload.php"])); use Gt\Fetch\Http; -use Gt\Fetch\BodyResponse; +use Gt\Fetch\Response\BodyResponse; +use Gt\Fetch\Response\Json; /* * This example uses postman-echo.com do test the request/response. @@ -12,7 +13,7 @@ // Example: Post form data to the echo server. $http = new Http(); -$http->fetch("https://postman-echo.com/post", [ +$http->fetch("http://g105b.com", [ // All of the request parameters can be passed directly here, or alternatively // the fetch() function can take a PSR-7 RequestInterface object. "method" => "POST", @@ -26,10 +27,12 @@ ]), ]) ->then(function(BodyResponse $response) { + die("what"); if(!$response->ok) { echo "Error posting to Postman Echo." . PHP_EOL; exit(1); } + die("this is nit nin"); // Postman Echo servers respond with a JSON representation of the request // that was received. return $response->json(); @@ -44,4 +47,5 @@ }); // To execute the above Promise(s), call wait() or all(). -$http->wait(); \ No newline at end of file +$http->wait(); +die("done waiting"); \ No newline at end of file diff --git a/src/Response/BodyResponse.php b/src/Response/BodyResponse.php index bd13f10..9ba006b 100644 --- a/src/Response/BodyResponse.php +++ b/src/Response/BodyResponse.php @@ -137,7 +137,7 @@ public function json(int $depth = 512, int $options = 0):Promise { $newPromise->reject($exception); } - $newPromise->resolve($json); + $newPromise->resolve(new Json($json)); }); return $newPromise; diff --git a/src/Response/Json.php b/src/Response/Json.php index eb9a1ad..5ffebb5 100644 --- a/src/Response/Json.php +++ b/src/Response/Json.php @@ -1,8 +1,89 @@ jsonObject = $jsonObject; + + $this->iteratorKey = 0; + + if(is_array($jsonObject)) { + $this->iteratorProperties = array_keys($jsonObject); + } + else { + $this->iteratorProperties = get_object_vars($jsonObject); + } + + } + + public function __get(string $key) { + return $this->jsonObject->$key; + } + + /** @link https://php.net/manual/en/arrayaccess.offsetexists.php */ + public function offsetExists($offset):bool { + return isset($this->jsonObject[$offset]); + } + + /** @link https://php.net/manual/en/arrayaccess.offsetget.php */ + public function offsetGet($offset) { + return $this->jsonObject[$offset]; + } + + /** @link https://php.net/manual/en/arrayaccess.offsetset.php */ + public function offsetSet($offset, $value):void { + $this->jsonObject[$offset] = $value; + } + + /** @link https://php.net/manual/en/arrayaccess.offsetunset.php */ + public function offsetUnset($offset):void { + unset($this->jsonObject[$offset]); + } + + /** @link https://php.net/manual/en/iterator.current.php */ + public function current() { + if(is_array($this->jsonObject)) { + return $this->jsonObject[$this->iteratorKey]; + } + + $property = $this->iteratorProperties[$this->iteratorKey]; + return $this->jsonObject->{$property}; + } + + /** @link https://php.net/manual/en/iterator.next.php */ + public function next():void { + $this->iteratorKey++; + } + + /** @link https://php.net/manual/en/iterator.key.php */ + public function key() { + if(is_array($this->jsonObject)) { + return $this->iteratorKey; + } + + return $this->iteratorProperties[$this->iteratorKey]; + } + + /** @link https://php.net/manual/en/iterator.valid.php */ + public function valid() { + if(is_array($this->jsonObject)) { + return isset($this->jsonObject[$this->iteratorKey]); + } + + $property = $this->iteratorProperties[$this->iteratorKey]; + return isset($this->jsonObject->{$property}); + } + + /** @link https://php.net/manual/en/iterator.rewind.php */ + public function rewind() { + $this->iteratorKey = 0; + } } \ No newline at end of file