Skip to content

Commit e12f6c6

Browse files
author
Tyler King
committed
Added JSON serialize
1 parent 9cd420d commit e12f6c6

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
# 9.1.1
44

5-
+ Implemented `Iterable` and `Countable` on `ResponseAccess`.
6-
+ Added methods `keys` and `values` to allow for getting keys and values easier.
5+
+ Implemented `Iterable` and `Countable` on `ResponseAccess`
6+
+ Added methods `keys` and `values` to allow for getting keys and values easier
7+
+ Add JSON serilize method
78

89
# 9.1.0
910

src/Osiset/BasicShopifyAPI/ResponseAccess.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
use Iterator;
66
use Countable;
77
use ArrayAccess;
8+
use JsonSerializable;
89

910
/**
1011
* Response data object for accessing.
1112
*/
12-
class ResponseAccess implements ArrayAccess, Iterator, Countable
13+
class ResponseAccess implements ArrayAccess, Iterator, Countable, JsonSerializable
1314
{
1415
/**
1516
* The response data.
@@ -220,6 +221,16 @@ public function values(): array
220221
return array_values($this->container);
221222
}
222223

224+
/**
225+
* Return a JSON serializable array.
226+
*
227+
* @return array
228+
*/
229+
public function jsonSerialize(): array
230+
{
231+
return $this->container;
232+
}
233+
223234
/**
224235
* Check if errors are in response.
225236
*

test/ResponseAccessTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,18 @@ public function testKeysAndValues(): void
8282
$this->assertEquals(['names'], $resp->keys());
8383
$this->assertEquals(array_values($names), $resp->names->values());
8484
}
85+
86+
public function testJsonSerialize(): void
87+
{
88+
$names = [
89+
'John',
90+
'Tim',
91+
'Tommy',
92+
];
93+
$resp = new ResponseAccess([
94+
'names' => $names,
95+
]);
96+
97+
$this->assertEquals(json_encode(['names' => $names]), json_encode($resp));
98+
}
8599
}

0 commit comments

Comments
 (0)