定制 brash-creative/popomapper 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱: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
  • 点击次数: 2
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

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

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固