-
Notifications
You must be signed in to change notification settings - Fork 1
/
demo.php
77 lines (62 loc) · 1.64 KB
/
demo.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
use skrtdev\JSON2\{Decodeable, JSONProperty};
require 'vendor/autoload.php';
function api_call(...$args): string {
return file_get_contents('https://jsonplaceholder.typicode.com/'.implode('/', $args));
}
/** @var Post[] $posts */
$posts = json2_decode(api_call('posts'), Post::class);
class Post{
// use a different property name
#[JSONProperty(json: 'userId')]
protected int $user_id;
protected int $id;
protected string $title;
protected string $body;
protected User $user;
/**
* @var Comment[]
*/
protected array $comments;
/**
* @return User
* @throws ReflectionException
*/
public function getUser(): User
{
return $this->user ??= json2_decode(api_call('users', $this->user_id), User::class);
}
/**
* @return Comment[]
*/
public function getComments(): array
{
return $this->comments ??= json2_decode(api_call('posts', $this->id, 'comments'), Comment::class);
}
}
class User extends Decodeable { // you can also extend Decodeable class in order to use a normal constructor
protected int $id;
protected string $name;
protected string $username;
protected Address $address;
protected Company $company;
}
class Address{
protected Location $geo;
}
class Location{
protected float $lat;
protected float $lng;
}
class Company{
protected string $name;
#[JSONProperty(json: 'catchPhrase')]
protected string $catch_phrase;
}
class Comment{
#[JSONProperty(json: 'postId')]
protected string $post_id;
}
var_dump($posts);
var_dump($posts[30]->getUser());
var_dump($posts[6]->getComments());