Skip to content
This repository has been archived by the owner on Jun 19, 2020. It is now read-only.

baethon/jsonapi-encoder

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

jsonapi-encoder

Encode (or decode) your entites using json-api annotated schema

Dictionary

  • encode - serialize resource and return it by the api
  • decode - create resource based on the request data

Entity example

<?php

use Baethon\JsonApi\Encoder\Annotation as JsonApi;

/**
 * @JsonApi\Resource(name="user")
 */
class User
{

    /**
     * By default any attribute with @Field annotation can be encoded or decoded
     *
     * @JsonApi\Id
     * @JsonApi\Attribute if none type is set value from @var will be used
     * @var string
     */
    private $id;
    
    /**
     * This field can be only decoded
     *
     * @JsonApi\Attribute(type="password") custom types should be registered
     * @JsonApi\Decode
     * @var string
     */
    private $password;
    
    /**
     * This field will be returned by the API, but cannot be set based on request data
     *
     * @JsonApi\Attribute(type="date", format=DATE_ATOM) any further arguments from @Field annotation will be passed to
     *                                               the encode()/decode() method of type declaration class
     * @JsonApi\Encode
     * @var \DateTime
     */
    private $createdAt;
    
    /**
     * @JsonApi\Collection(name="comment")
     * @var Comment[]
     */
    private $comments;
    
    /**
     * @JsonApi\Item(name="address")
     * @var Address
     */
     private $address;
}

Usage example

<?php
$user = new User;

$manager = new Baethon\JsonApi\Manager([
    'cacheDir' => 'cache/',
    'autogenerateTransformers' => true,
]);
$encoder = new Baethon\JsonApi\Encoder($manager);
$encodedArray = $encoder->encodeItem($user);

/*
$encodedArray = [
    'data' => [
        'type' => 'user',
        'id' => '',
        'attributes' => [
            'createdAt' => ''
        ],
    ]
]
*/

$user->setAddress(new Address);

$includes = \Baethon\JsonApi\Request\Includes::createFromString('address');
$encodedArray = $encoder->encodeItem($user, $includes);

/*
$encodedArray = [
    'data' => [
        'type' => 'user',
        'id' => '',
        'attributes' => [
            'createdAt' => ''
        ],
        'relationships' => [
            'address' => [
                'data' => [
                    'id' => '',
                    'type' => 'address',
                ]
            ]
        ]
    ],
    'included' => [
        [
            'id' => '',
            'type' => 'address',
            'attributes' => [
            ]
        ]
    ]
]
*/

About

Encode (or decode) your entites using json-api annotated schema

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages