Skip to content

Commit 652a76a

Browse files
committed
Update json response
1 parent 5133ca4 commit 652a76a

File tree

4 files changed

+91
-6
lines changed

4 files changed

+91
-6
lines changed

example/01-basic-fetch.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
require(implode(DIRECTORY_SEPARATOR, ["..", "vendor", "autoload.php"]));
33

44
use Gt\Fetch\Http;
5-
use Gt\Fetch\BodyResponse;
5+
use Gt\Fetch\Response\BodyResponse;
66

77
/*
88
* This example fetches the list of repositories in the PhpGt organisation from

example/02-post-request.php

+7-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
require(implode(DIRECTORY_SEPARATOR, ["..", "vendor", "autoload.php"]));
33

44
use Gt\Fetch\Http;
5-
use Gt\Fetch\BodyResponse;
5+
use Gt\Fetch\Response\BodyResponse;
6+
use Gt\Fetch\Response\Json;
67

78
/*
89
* This example uses postman-echo.com do test the request/response.
@@ -12,7 +13,7 @@
1213
// Example: Post form data to the echo server.
1314

1415
$http = new Http();
15-
$http->fetch("https://postman-echo.com/post", [
16+
$http->fetch("http://g105b.com", [
1617
// All of the request parameters can be passed directly here, or alternatively
1718
// the fetch() function can take a PSR-7 RequestInterface object.
1819
"method" => "POST",
@@ -26,10 +27,12 @@
2627
]),
2728
])
2829
->then(function(BodyResponse $response) {
30+
die("what");
2931
if(!$response->ok) {
3032
echo "Error posting to Postman Echo." . PHP_EOL;
3133
exit(1);
3234
}
35+
die("this is nit nin");
3336
// Postman Echo servers respond with a JSON representation of the request
3437
// that was received.
3538
return $response->json();
@@ -44,4 +47,5 @@
4447
});
4548

4649
// To execute the above Promise(s), call wait() or all().
47-
$http->wait();
50+
$http->wait();
51+
die("done waiting");

src/Response/BodyResponse.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public function json(int $depth = 512, int $options = 0):Promise {
137137
$newPromise->reject($exception);
138138
}
139139

140-
$newPromise->resolve($json);
140+
$newPromise->resolve(new Json($json));
141141
});
142142

143143
return $newPromise;

src/Response/Json.php

+82-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,89 @@
11
<?php
22
namespace Gt\Fetch\Response;
33

4+
use ArrayAccess;
5+
use Iterator;
46
use StdClass;
57

6-
class Json extends StdClass {
8+
class Json extends StdClass implements ArrayAccess, Iterator {
9+
protected $jsonObject;
10+
protected $iteratorKey;
11+
protected $iteratorProperties;
712

13+
public function __construct($jsonObject) {
14+
$this->jsonObject = $jsonObject;
15+
16+
$this->iteratorKey = 0;
17+
18+
if(is_array($jsonObject)) {
19+
$this->iteratorProperties = array_keys($jsonObject);
20+
}
21+
else {
22+
$this->iteratorProperties = get_object_vars($jsonObject);
23+
}
24+
25+
}
26+
27+
public function __get(string $key) {
28+
return $this->jsonObject->$key;
29+
}
30+
31+
/** @link https://php.net/manual/en/arrayaccess.offsetexists.php */
32+
public function offsetExists($offset):bool {
33+
return isset($this->jsonObject[$offset]);
34+
}
35+
36+
/** @link https://php.net/manual/en/arrayaccess.offsetget.php */
37+
public function offsetGet($offset) {
38+
return $this->jsonObject[$offset];
39+
}
40+
41+
/** @link https://php.net/manual/en/arrayaccess.offsetset.php */
42+
public function offsetSet($offset, $value):void {
43+
$this->jsonObject[$offset] = $value;
44+
}
45+
46+
/** @link https://php.net/manual/en/arrayaccess.offsetunset.php */
47+
public function offsetUnset($offset):void {
48+
unset($this->jsonObject[$offset]);
49+
}
50+
51+
/** @link https://php.net/manual/en/iterator.current.php */
52+
public function current() {
53+
if(is_array($this->jsonObject)) {
54+
return $this->jsonObject[$this->iteratorKey];
55+
}
56+
57+
$property = $this->iteratorProperties[$this->iteratorKey];
58+
return $this->jsonObject->{$property};
59+
}
60+
61+
/** @link https://php.net/manual/en/iterator.next.php */
62+
public function next():void {
63+
$this->iteratorKey++;
64+
}
65+
66+
/** @link https://php.net/manual/en/iterator.key.php */
67+
public function key() {
68+
if(is_array($this->jsonObject)) {
69+
return $this->iteratorKey;
70+
}
71+
72+
return $this->iteratorProperties[$this->iteratorKey];
73+
}
74+
75+
/** @link https://php.net/manual/en/iterator.valid.php */
76+
public function valid() {
77+
if(is_array($this->jsonObject)) {
78+
return isset($this->jsonObject[$this->iteratorKey]);
79+
}
80+
81+
$property = $this->iteratorProperties[$this->iteratorKey];
82+
return isset($this->jsonObject->{$property});
83+
}
84+
85+
/** @link https://php.net/manual/en/iterator.rewind.php */
86+
public function rewind() {
87+
$this->iteratorKey = 0;
88+
}
889
}

0 commit comments

Comments
 (0)