edvakf/php-thrift-mapper

将 PHP 数组转换为 Apache Thrift 结构类型。

dev-master 2015-09-10 19:14 UTC

This package is not auto-updated.

Last update: 2024-10-02 09:36:36 UTC


README

将 PHP 数组转换为 Apache Thrift 结构类型。

Build Status Coverage Status

这是什么?

一个 Thrift 结构;

struct Bonk
{
  1: string message,
  2: i32 type
}

生成如下 PHP 源代码。

class Bonk {
  static $_TSPEC;

  /**
   * @var string
   */
  public $message = null;
  /**
   * @var int
   */
  public $type = null;

  public function __construct($vals=null) {
    if (!isset(self::$_TSPEC)) {
      self::$_TSPEC = array(
        1 => array(
          'var' => 'message',
          'type' => TType::STRING,
          ),
        2 => array(
          'var' => 'type',
          'type' => TType::I32,
          ),
        );
    }
    if (is_array($vals)) {
      if (isset($vals['message'])) {
        $this->message = $vals['message'];
      }
      if (isset($vals['type'])) {
        $this->type = $vals['type'];
      }
    }
  }

  public function getName() {
    return 'Bonk';
  }

现在,如果我想将我的 PHP 数组转换为这个类,没有简单的方法。

这就是 ThriftMapper 的作用

它使用 PHP 数组填充 Thrift 对象。

$ary = [
  "message" => "Hello!",
  "type" => 123,
];

$bonk = ThriftMapper::map(new Bonk(), $ary);

var_dump($bonk);

这段代码输出:

object(ThriftTest\Bonk)#19 (2) {
  ["message"]=>
  string(6) "Hello!"
  ["type"]=>
  int(123)
}