protobuf-php/protobuf

Object Constructors from compiler

luisgon1 opened this issue · 4 comments

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

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([
]);

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

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()
        ])
    ]
]);

Perfect,
Thank You