承接 bobbysciacchitano/manuel 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

bobbysciacchitano/manuel

最新稳定版本:2.0.2

Composer 安装命令:

composer require bobbysciacchitano/manuel

包简介

A simple object serializer for PHP which supports flexible document structures.

README 文档

README

Build Status SensioLabsInsight

The easy to use PHP Serializer. Because translation shouldn't be a chore.

How to use

To serialize an object you first need to create a transformer. Transformers must implement the transform method and return an array. Transformers can accept any type of data or object that requires serialisation.

<?php namespace App\Transformer;

use TheObject;
use Manuel\Transformer\TransformerAbstract;

class MyTransformer extends TransformerAbstract {

    /**
     * @var string
     */
    protected $type = 'customer';

    /**
     * Transform object properties.
     *
     * @param TheObject $myObject
     * @return array
     */
    public function transform(TheObject $myObject)
    {
        return array(
            'id'           => (int) $myObject->id,
            'name'         => $myObject->name,
            'email'        => $myObject->email,
            'active'       => (bool) $myObject->is_active
        );
    }

    /**
     * Define other resources to be included in transformation.
     *
     * @param TheObject $myObject
     */
    public function resources(TheObject $myObject)
    {
        $this->addLink("/customer/{$myObject->id}/tasks");

        $this->addRelationship('organisation', $myObject->organisation_id);
    }

}

To serialize your data into a resource, you can wrap it in a Resource object. Wrapping the data into in a Resource tells Manuel whether you want to return a collection or a single resource. You can also declare your own resource types.

<?php

use Manuel\Manager;
use Manuel\Resource;
use App\Transformer;

$manager = new Manager(new JsonAPISerializer);

// Serialize a object
$translated = $manager->translate(new Resource\Item($data, new Transfomer\MyTransformer));

// Serialize an array or collection of objects
$translated = $manager->translate(new Resource\Collection($array, new Transfomer\MyTransformer));

The above transformer with the Json API serializer will generate the following representation:

{
    "data": {
        "id": 1,
        "type": "customer",
        "attributes": {
            "name": "Johnny",
            "email": "johnny@test.com",
            "active": true
        },
        "relationships": {
            "organisation": {
                "data": {
                    "id": 5,
                    "type": "organisation"
                }
            },
            "tasks": {
                "links": {
                    "related": "/customer/1/tasks"
                }
            }
        }
    }
}

Associations

Manuel can handle an variety of association types. It is the responsibility of the serializer to translate the relationship into the correct format.

Simple Relationship

Simple relationships can be used to contrive additional data or return a simple value. When used with the JsonAPI serializer, this type of resource relationship can be used to declare a reference to an object that is not embedded or side-loaded.

Linked Resources

Much like simple relationships, this type of resource can be used to create a link reference to another resource that can be loaded from the API.

Embedded Resources

Much like simple relationships, embedded resources can be used to nest another resource within the resource tree. Embedded resources can be either a Collection or Item and the serializer will attempt to serialize all relationships underneath.

    /**
     * Define other resources to be included in transformation.
     *
     * @param TheObject $myObject
     */
    public function resources(TheObject $myObject)
    {
        $this->addResource('test_item', new Item($data->item, new Transformer));

        $this->addResource('test_collection', new Collection($data->items, new Transformer));
    }

Sideloaded Resources

This type of resource will be included along side the main resource and references to the resource identifiers can be loaded as part of the relationship serialization.

    /**
     * Define other resources to be included in transformation.
     *
     * @param TheObject $myObject
     */
    public function resources(TheObject $myObject)
    {
        $this->addResource('test_item', new Item($data->item, new Transformer), true);

        $this->addResource('test_collection', new Collection($data->items, new Transformer), true);
    }

Serializers

Manuel out of the box includes a basic implementation of the JsonAPI serializer. The abstract serializer includes a flexible API which can form the basis for your own serializer.

统计信息

  • 总下载量: 311
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-10-31