protobuf-php / protobuf

PHP implementation of Google's Protocol Buffers
MIT License
236 stars 29 forks source link

Object Constructors from compiler #7

Closed luisgon1 closed 8 years ago

luisgon1 commented 8 years ago

Can the compiler generate a constructor for the proto messages with the required params?

FabioBatSilva commented 8 years ago

The constructor already receive the payload and is used to parse messages. Another option would be to provide a create method, something like :

message MyMessage
{
    required string foo = 1;
}
<?php

$message = MyMessage:create([
  'foo' => 'bar'
]);

// throws InvalidArgumentException
$message = MyMessage:create([
]);
luisgon1 commented 8 years ago

A create method would help. We would have less code and it would be cleaner.

FabioBatSilva commented 8 years ago

Now you should be able to use fromArray

<?php

$person = Person::fromArray([
    'id'    => 1234,
    'name'  => 'Fabio',
    'phone' => [
        PhoneNumber::fromArray([
            'number' => '123123123',
            'type'   => PhoneType::HOME()
        ]),
        PhoneNumber::fromArray([
            'number' => '321321321',
            'type'   => PhoneType::MOBILE()
        ])
    ]
]);
luisgon1 commented 8 years ago

Perfect, Thank You