han130634 / pb4php

Automatically exported from code.google.com/p/pb4php
0 stars 1 forks source link

Code review request #6

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Added two helper methods to pb_message.php
- get_value(): Convert PBMessage objects to PHP nested objects
- set_value(): Convert PHP nested objects to PBMessage object

In addition, it is also required to make little 
modification to pb_parser.php. See the following generated code as example.

<?php
class PB_User extends PBMessage
{
    var $wired_type = PBMessage::WIRED_LENGTH_DELIMITED;
    public function __construct($reader = null)
    {
        parent::__construct($reader);
        $this->names[1] = 'name';  // <--- Newly Generated Code
        $this->fields[1] = 'PBString';
        $this->values[1] = '';
    }
    function name()
    {
        return $this->_get_value(1);
    }
    function set_name($value)
    {
        return $this->_set_value(1, $value);
    }
}

* Protocol Buffers Message Example:
message PB_User {
    required string name = 1;
}

$pb = new PB_User();
$user = $pb->get_value();
echo $user->name;

$user->name = 'Test';
$pb = new PB_User();
$pb->set_value($user);

class pb_message {

public function get_value()
{
    unset($value);

    foreach ($this->names as $index => $_name)
    {
        if (is_array($this->values[$index]))
        {
            $value->$_name = array();
            foreach ($this->values[$index] as $_value)
            {
                array_push($value->$_name, $_value->get_value());
            }
        }
        else
        {
            $value->$_name = ($this->values[$index]) ? $this->values
[$index]->get_value() : null;
        }
    }

    return $value;
}

public function set_value($value)
{
    foreach ($this->names as $index => $_name)
    {
        if (is_array($this->values[$index]) AND is_array($value->$_name))
        {
            $this->values[$index] = array();
            foreach ($value->$_name as $_value)
            {
                $class_object = new $this->fields[$index]();
                $class_object->set_value($_value);
                $this->values[$index][] = $class_object;
            }
        }
        else
        {
            if ( ! is_object($this->values[$index]))
            {
                $this->values[$index] = new $this->fields[$index]();
            }

            $this->values[$index]->set_value($value->$_name);
        }
    }
}

}

Original issue reported on code.google.com by che...@gmail.com on 28 Apr 2009 at 6:48

GoogleCodeExporter commented 9 years ago
The followings contain some bug fixed for above two methods: get_value() and 
set_value()

    public function get_value()
    {
        unset($value);

        foreach ($this->names as $index => $_name)
        {
            if (is_array($this->values[$index]))
            {
                $value->$_name = array();
                foreach ($this->values[$index] as $_value)
                {
                    array_push($value->$_name, $_value->get_value());
                }
            }
            else
            {
                if (isset($this->values[$index]) AND ! empty($this->values[$index]))
                {
                    $value->$_name = $this->values[$index]->get_value();
                }
            }
        }

        return $value;
    }

    public function set_value($value)
    {
        foreach ($this->names as $index => $_name)
        {
            if ( ! isset($value->$_name))
                continue;

            if (is_array($value->$_name) AND is_array($this->values[$index]))
            {
                $this->values[$index] = array();
                foreach ($value->$_name as $_value)
                {
                    $class_object = new $this->fields[$index]();
                    $class_object->set_value($_value);
                    $this->values[$index][] = $class_object;
                }
            }
            else
            {
                if ( ! isset($this->values[$index]) OR empty($this->values[$index]))
                {
                    $this->values[$index] = new $this->fields[$index]();
                }
                $this->values[$index]->set_value($value->$_name);
            }
        }
    }

Original comment by che...@gmail.com on 7 Jul 2009 at 12:01