承接 xenolope/cartographer 相关项目开发

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

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

xenolope/cartographer

最新稳定版本:v0.6.1

Composer 安装命令:

composer require xenolope/cartographer

包简介

A super-simple library to map JSON documents to objects, similar to Java's Jackson

README 文档

README

Build Status

Cartographer is a super-simple library for deserializing JSON into POPOs, similar to FasterXML's jackson-databind package for Java.

Installation

The library can be installed with Composer, by including the following in your composer.json:

{
    "require": {
        "xenolope/cartographer": "~0.5"
    }
}

Usage

POPOs

Your POPOs must have a property and corresponding setter, with either the property having a @var ClassName docblock, or the setter having a type hint.

Setters are called directly, and properties are never touched, even if they're declared public (though this might be added in a later version).

Simple values are converted to the types given in their @var docblocks (using settype()), objects are created based on the class specified in the @var docblocks, and arrays of objects are also created, and can be specified with the @var ClassName[] notation.

class Contact
{

    /**
     * @var string
     */
    private $name;

    /**
     * @var Address
     */
    private $address;

    /**
     * Note, this property doesn't have a @var docblock, but the corresponding setter
     * below *does* have a type hint
     */
    private $secondaryAddress;

    /**
     * @var Note[]
     */
    private $notes;

    /**
     * @param string $name
     */
    public function setName($name)
    {
        $this->name = $name;
    }

    /**
     * @param Address $address
     */
    public function setAddress($address)
    {
        $this->address = $address;
    }

    /**
     * @param Address $secondaryAddress
     */
    public function setAddress(Address $secondaryAddress)
    {
        $this->secondaryAddress = $secondaryAddress;
    }

    /**
     * @param Note[] $notes
     */
    public function setNotes($notes)
    {
        $this->notes = $notes;
    }
}

Mapping

// Create a new instance of the Mapper
$mapper = new \Xenolope\Cartographer\Mapper();

// Map a JSON string to a POPO

// PHP 5.4
$object = $mapper->mapString(
    '{"name":"Liz Lemon","address":{"street":"168 Riverside Dr.","city":"New York"}}',
    'Vendor\Package\Entity\Contact'
);

// PHP >=5.5
$object = $mapper->mapString(
    '{"name":"Liz Lemon","address":{"street":"168 Riverside Dr.","city":"New York"}}',
    Contact::class
);

// Map an already decoded (to array) JSON document to a POPO

// This might happen automatically in your Request class, for example
$jsonDocument = json_decode(
    '{"name":"Liz Lemon","address":{"street":"168 Riverside Dr.","city":"New York"}}',
    true
);

// PHP 5.4
$object = $mapper->map($jsonDocument, 'Vendor\Package\Entity\Contact');

// PHP >= 5.5
$object = $mapper->map(
    '{"name":"Liz Lemon","address":{"street":"168 Riverside Dr.","city":"New York"}}',
    Contact::class
);

Roadmap

  • Add custom property mapping, for when JSON properties don't match with POPO properties
  • Maybe add in support for serializing a POPO to JSON

Thanks

This library was inspired by:

License

Cartographer is released under the MIT License; please see LICENSE for more information.

统计信息

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

GitHub 信息

  • Stars: 21
  • Watchers: 3
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-12-20