Skip to content

Commit 4620209

Browse files
committed
ProtocolBuffers\Message::__constructor should accept array which contains message object #41
``` both are okay: $bar = new TestMessage(['users' => [ new TestUser(['id' => 1]) ]]); $bar = new TestMessage(['users' => [ ['id' => 1] ]]); ```
1 parent f53b49d commit 4620209

File tree

2 files changed

+184
-10
lines changed

2 files changed

+184
-10
lines changed

message.c

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -527,19 +527,33 @@ static void php_protocolbuffers_message_set(INTERNAL_FUNCTION_PARAMETERS, zval *
527527
continue;
528528
}
529529

530-
MAKE_STD_ZVAL(child);
531-
MAKE_STD_ZVAL(param);
532-
ZVAL_ZVAL(param, *element, 1, 0);
530+
if (Z_TYPE_PP(element) == IS_OBJECT) {
531+
if (scheme->ce != Z_OBJCE_PP(element)) {
532+
zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0 TSRMLS_CC, "expected %s class. given %s class", scheme->ce->name, Z_OBJCE_P(value)->name);
533+
return;
534+
} else {
535+
m = PHP_PROTOCOLBUFFERS_GET_OBJECT(php_protocolbuffers_message, *element);
536+
ZVAL_ZVAL(m->container, instance, 0, 0);
537+
538+
Z_ADDREF_PP(element);
539+
add_next_index_zval(outer, *element);
540+
}
541+
} else {
542+
MAKE_STD_ZVAL(child);
543+
MAKE_STD_ZVAL(param);
544+
ZVAL_ZVAL(param, *element, 1, 0);
545+
546+
object_init_ex(child, scheme->ce);
547+
php_protocolbuffers_properties_init(child, scheme->ce TSRMLS_CC);
533548

534-
object_init_ex(child, scheme->ce);
535-
php_protocolbuffers_properties_init(child, scheme->ce TSRMLS_CC);
549+
zend_call_method_with_1_params(&child, scheme->ce, NULL, ZEND_CONSTRUCTOR_FUNC_NAME, NULL, param);
550+
zval_ptr_dtor(&param);
536551

537-
zend_call_method_with_1_params(&child, scheme->ce, NULL, ZEND_CONSTRUCTOR_FUNC_NAME, NULL, param);
538-
zval_ptr_dtor(&param);
552+
m = PHP_PROTOCOLBUFFERS_GET_OBJECT(php_protocolbuffers_message, child);
553+
ZVAL_ZVAL(m->container, instance, 0, 0);
554+
add_next_index_zval(outer, child);
555+
}
539556

540-
m = PHP_PROTOCOLBUFFERS_GET_OBJECT(php_protocolbuffers_message, child);
541-
ZVAL_ZVAL(m->container, instance, 0, 0);
542-
add_next_index_zval(outer, child);
543557
}
544558
value = outer;
545559
should_free = 1;
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
--TEST--
2+
#41 constructor should accept object
3+
--FILE--
4+
<?php
5+
// @@protoc_insertion_point(namespace:.TestMessage)
6+
7+
/**
8+
* Generated by the protocol buffer compiler. DO NOT EDIT!
9+
* source: x.proto
10+
*
11+
* -*- magic methods -*-
12+
*
13+
* @method array getUsers()
14+
* @method void appendUsers(TestUser $value)
15+
*/
16+
class TestMessage extends ProtocolBuffersMessage
17+
{
18+
// @@protoc_insertion_point(traits:.TestMessage)
19+
20+
/**
21+
* @var array $users
22+
* @tag 1
23+
* @label optional
24+
* @type ProtocolBuffers::TYPE_MESSAGE
25+
* @see TestUser
26+
**/
27+
protected $users;
28+
29+
30+
// @@protoc_insertion_point(properties_scope:.TestMessage)
31+
32+
// @@protoc_insertion_point(class_scope:.TestMessage)
33+
34+
/**
35+
* get descriptor for protocol buffers
36+
*
37+
* @return ProtocolBuffersDescriptor
38+
*/
39+
public static function getDescriptor()
40+
{
41+
static $descriptor;
42+
43+
if (!isset($descriptor)) {
44+
$desc = new ProtocolBuffersDescriptorBuilder();
45+
$desc->addField(1, new ProtocolBuffersFieldDescriptor(array(
46+
"type" => ProtocolBuffers::TYPE_MESSAGE,
47+
"name" => "users",
48+
"required" => false,
49+
"optional" => false,
50+
"repeated" => true,
51+
"packable" => false,
52+
"default" => null,
53+
"message" => 'TestUser',
54+
)));
55+
// @@protoc_insertion_point(builder_scope:.TestMessage)
56+
57+
$descriptor = $desc->build();
58+
}
59+
return $descriptor;
60+
}
61+
62+
}
63+
64+
// @@protoc_insertion_point(namespace:.TestUser)
65+
66+
/**
67+
* Generated by the protocol buffer compiler. DO NOT EDIT!
68+
* source: x.proto
69+
*
70+
* -*- magic methods -*-
71+
*
72+
* @method string getId()
73+
* @method void setId(string $value)
74+
*/
75+
class TestUser extends ProtocolBuffersMessage
76+
{
77+
// @@protoc_insertion_point(traits:.TestUser)
78+
79+
/**
80+
* @var string $id
81+
* @tag 1
82+
* @label required
83+
* @type ProtocolBuffers::TYPE_INT64
84+
**/
85+
protected $id;
86+
87+
88+
// @@protoc_insertion_point(properties_scope:.TestUser)
89+
90+
// @@protoc_insertion_point(class_scope:.TestUser)
91+
92+
/**
93+
* get descriptor for protocol buffers
94+
*
95+
* @return ProtocolBuffersDescriptor
96+
*/
97+
public static function getDescriptor()
98+
{
99+
static $descriptor;
100+
101+
if (!isset($descriptor)) {
102+
$desc = new ProtocolBuffersDescriptorBuilder();
103+
$desc->addField(1, new ProtocolBuffersFieldDescriptor(array(
104+
"type" => ProtocolBuffers::TYPE_INT64,
105+
"name" => "id",
106+
"required" => true,
107+
"optional" => false,
108+
"repeated" => false,
109+
"packable" => false,
110+
"default" => null,
111+
)));
112+
// @@protoc_insertion_point(builder_scope:.TestUser)
113+
114+
$descriptor = $desc->build();
115+
}
116+
return $descriptor;
117+
}
118+
119+
}
120+
121+
$bar = new TestMessage(array('users' => array(
122+
new TestUser(array('id' => 1)),
123+
new TestUser(array('id' => 2)),
124+
new TestUser(array('id' => 3))
125+
)));
126+
127+
foreach ($bar->getUsers() as $user) {
128+
echo $user->getId() . PHP_EOL;
129+
}
130+
131+
$bar = new TestMessage(array('users' => array(
132+
array('id' => 1),
133+
array('id' => 2),
134+
array('id' => 3),
135+
)));
136+
137+
foreach ($bar->getUsers() as $user) {
138+
echo $user->getId() . PHP_EOL;
139+
}
140+
141+
$u1 = new TestUser(array('id' => 1));
142+
$u2 = new TestUser(array('id' => 2));
143+
$u3 = new TestUser(array('id' => 3));
144+
$users = array($u1, $u2, $u3);
145+
146+
$bar = new TestMessage(array('users' => $users));
147+
foreach ($bar->getUsers() as $user) {
148+
echo $user->getId() . PHP_EOL;
149+
}
150+
151+
--EXPECT--
152+
1
153+
2
154+
3
155+
1
156+
2
157+
3
158+
1
159+
2
160+
3

0 commit comments

Comments
 (0)