Skip to content

Commit

Permalink
Update json response
Browse files Browse the repository at this point in the history
  • Loading branch information
g105b committed May 20, 2019
1 parent 5133ca4 commit 652a76a
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 6 deletions.
2 changes: 1 addition & 1 deletion example/01-basic-fetch.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 7 additions & 3 deletions example/02-post-request.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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",
Expand All @@ -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();
Expand All @@ -44,4 +47,5 @@
});

// To execute the above Promise(s), call wait() or all().
$http->wait();
$http->wait();
die("done waiting");
2 changes: 1 addition & 1 deletion src/Response/BodyResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
83 changes: 82 additions & 1 deletion src/Response/Json.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,89 @@
<?php
namespace Gt\Fetch\Response;

use ArrayAccess;
use Iterator;
use StdClass;

class Json extends StdClass {
class Json extends StdClass implements ArrayAccess, Iterator {
protected $jsonObject;
protected $iteratorKey;
protected $iteratorProperties;

public function __construct($jsonObject) {
$this->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;
}
}

0 comments on commit 652a76a

Please sign in to comment.