brash-creative/popomapper 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

brash-creative/popomapper

最新稳定版本:1.2.0

Composer 安装命令:

composer require brash-creative/popomapper

包简介

Plain old PHP object JSON mapper - Map json or array structures to a POPO

README 文档

README

Takes data held in a JSON or array format, and maps it to an application's PHP objects using docblock annotations for parameter types.

Parameter types include all simple types (string, int, etc...), complex types like ArrayObject & DateTime, and nested application objects.

Usage

<?php
use Brash\PopoMapper\Mapper;

$mapper     = new Mapper();
$object     = $mapper->map($data, new Object());

The object mapper will detect if the data passed is a single array, or a multi-dimensional array of object data.

Example - Single data object

JSON for a basic client object, with nested purchases

<?php
$data   = '{
    "id": 1,
    "name": "Geoffrey",
    "purchases": [
        {
            "id": 1,
            "name": "Gromit"
        },
        {
            "id": 2,
            "name": "Whatsitsname"
        }
    ]
}';

Client object

<?php
class Client {
    /**
     * @var int
     */
    private $id;

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

    /**
     * @var Purchase[]
     */
    private $purchases;

    /**
     * @param int $id
     *
     * @return $this
     */
    public function setId($id)
    {
        $this->id = $id;
        return $this;
    }

    /**
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

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

    /**
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * @param Purchase[] $purchases
     *
     * @return $this
     */
    public function setPurchases($purchases)
    {
        $this->purchases = $purchases;
        return $this;
    }

    /**
     * @return Purchase[]
     */
    public function getPurchases()
    {
        return $this->purchases;
    }
}

Purchase object

<?php
class Purchase {
    /**
     * @var int
     */
    private $id;

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

    /**
     * @param int $id
     *
     * @return $this
     */
    public function setId($id)
    {
        $this->id = $id;
        return $this;
    }

    /**
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

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

    /**
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }
}

Application code

<?php
$mapper     = new Mapper();
$client     = $mapper->mapSingle($data, new Client());

Example - Multiple data objects

To return an ArrayObject of multiple mapped objects, simply pass an array of data.

<?php
$data   = '[
    {
        "id": 1,
        "name": "Client 1"
    },
    {
        "id": 2,
        "name": "Client 2"
    }
]'

Application code

<?php
$mapper     = new Mapper();
$client     = $mapper->mapMulti($data, new ArrayObject(), new Client());

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: Unknown
  • 更新时间: 2014-11-04