Skip to content

Commit eb28648

Browse files
author
Greg Bowler
authored
Use type-safe function in example (#66)
1 parent d3200bb commit eb28648

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,9 @@ $http->fetch("http://example.com/api/something.json")
5252
. count($json->results)
5353
. PHP_EOL;
5454

55+
// Notice that type-safe getters are available on all Json objects:
5556
echo "Name of first result: "
56-
. $json->results[0]->name
57+
. $json->results[0]->getString("name")
5758
. PHP_EOL;
5859
});
5960

example/01-basic-fetch.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
echo "PHP.Gt repository list:" . PHP_EOL;
2727

2828
foreach($json as $repo) {
29-
echo $repo->name . PHP_EOL;
29+
echo $repo->getString("name") . PHP_EOL;
3030
}
3131
});
3232

src/Response/Json.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,18 @@ public function offsetUnset($offset):void {
6666
throw new ImmutableObjectModificationException();
6767
}
6868

69-
/** @link https://php.net/manual/en/iterator.current.php */
69+
/**
70+
* @return int|float|bool|string|null|Json
71+
* @link https://php.net/manual/en/iterator.current.php
72+
*/
7073
public function current() {
7174
if(is_array($this->jsonObject)) {
72-
return $this->jsonObject[$this->iteratorKey];
75+
$obj = $this->jsonObject[$this->iteratorKey];
76+
if($obj instanceof StdClass) {
77+
return new self($obj);
78+
}
79+
80+
return $obj;
7381
}
7482

7583
$property = $this->iteratorPropertyNames[$this->iteratorKey];

0 commit comments

Comments
 (0)