function Message::mergeFromArray
Populates the message from a user-supplied PHP array. Array keys correspond to Message properties and nested message properties.
Example: ``` $message->mergeFromArray([ 'name' => 'This is a message name', 'interval' => [ 'startTime' => time() - 60, 'endTime' => time(), ] ]); ```
This method will trigger an error if it is passed data that cannot be converted to the correct type. For example, a StringValue field must receive data that is either a string or a StringValue object.
Parameters
array $array An array containing message properties and values.:
Return value
null
1 call to Message::mergeFromArray()
- Message::__construct in vendor/
google/ protobuf/ src/ Google/ Protobuf/ Internal/ Message.php - @ignore
File
-
vendor/
google/ protobuf/ src/ Google/ Protobuf/ Internal/ Message.php, line 1037
Class
- Message
- Parent class of all proto messages. Users should not instantiate this class or extend this class or its child classes by their own. See the comment of specific functions for more details.
Namespace
Google\Protobuf\InternalCode
protected function mergeFromArray(array $array) {
// Just call the setters for the field names
foreach ($array as $key => $value) {
$field = $this->desc
->getFieldByName($key);
if (is_null($field)) {
throw new \UnexpectedValueException('Invalid message property: ' . $key);
}
$setter = $field->getSetter();
if ($field->isMap()) {
$valueField = $field->getMessageType()
->getFieldByName('value');
if (!is_null($valueField) && $valueField->isWrapperType()) {
self::normalizeArrayElementsToMessageType($value, $valueField->getMessageType()
->getClass());
}
}
elseif ($field->isWrapperType()) {
$class = $field->getMessageType()
->getClass();
if ($field->isRepeated()) {
self::normalizeArrayElementsToMessageType($value, $class);
}
else {
self::normalizeToMessageType($value, $class);
}
}
$this->{$setter}($value);
}
}