Skip to content
/ json2 Public

JSON2 converts json and arrays in structured classes

License

Notifications You must be signed in to change notification settings

skrtdev/json2

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JSON2

JSON2 converts json and arrays in structured classes

Full example

<?php

use skrtdev\JSON2\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{

    #[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{
    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());

About

JSON2 converts json and arrays in structured classes

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Languages