edvakf/php-thrift-mapper

Convert a PHP array into an Apache Thrift struct type.

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

This package is not auto-updated.

Last update: 2024-04-17 07:18:25 UTC


README

Convert a PHP array into an Apache Thrift struct type.

Build Status Coverage Status

What is this?

A Thrift struct;

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

generates a PHP source like the following.

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';
  }

Now, if I want to convert my PHP array to this class, there is no easy way.

Here comes the ThriftMapper

It populates the Thrift object with the PHP array.

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

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

var_dump($bonk);

This code outputs;

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