Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Message.php

function Message::mergeFrom

Merges the contents of the specified message into current message.

This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Map fields key-value pairs are overwritten. Singular/Oneof sub-messages are recursively merged. All overwritten sub-messages are deep-copied.

Parameters

object $msg Protobuf message to be merged from.:

Return value

null

1 call to Message::mergeFrom()
Message::mergeFromJsonArray in vendor/google/protobuf/src/Google/Protobuf/Internal/Message.php

File

vendor/google/protobuf/src/Google/Protobuf/Internal/Message.php, line 683

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\Internal

Code

public function mergeFrom($msg) {
    if (get_class($this) !== get_class($msg)) {
        user_error("Cannot merge messages with different class.");
        return;
    }
    foreach ($this->desc
        ->getField() as $field) {
        $setter = $field->getSetter();
        $getter = $field->getGetter();
        if ($field->isMap()) {
            if (count($msg->{$getter}()) != 0) {
                $value_field = $field->getMessageType()
                    ->getFieldByNumber(2);
                foreach ($msg->{$getter}() as $key => $value) {
                    if ($value_field->getType() == GPBType::MESSAGE) {
                        $klass = $value_field->getMessageType()
                            ->getClass();
                        $copy = new $klass();
                        $copy->mergeFrom($value);
                        $this->kvUpdateHelper($field, $key, $copy);
                    }
                    else {
                        $this->kvUpdateHelper($field, $key, $value);
                    }
                }
            }
        }
        else {
            if ($field->getLabel() === GPBLabel::REPEATED) {
                if (count($msg->{$getter}()) != 0) {
                    foreach ($msg->{$getter}() as $tmp) {
                        if ($field->getType() == GPBType::MESSAGE) {
                            $klass = $field->getMessageType()
                                ->getClass();
                            $copy = new $klass();
                            $copy->mergeFrom($tmp);
                            $this->appendHelper($field, $copy);
                        }
                        else {
                            $this->appendHelper($field, $tmp);
                        }
                    }
                }
            }
            else {
                if ($field->getLabel() === GPBLabel::OPTIONAL) {
                    if ($msg->{$getter}() !== $this->defaultValue($field)) {
                        $tmp = $msg->{$getter}();
                        if ($field->getType() == GPBType::MESSAGE) {
                            if (is_null($this->{$getter}())) {
                                $klass = $field->getMessageType()
                                    ->getClass();
                                $new_msg = new $klass();
                                $this->{$setter}($new_msg);
                            }
                            $this->{$getter}()
                                ->mergeFrom($tmp);
                        }
                        else {
                            $this->{$setter}($tmp);
                        }
                    }
                }
            }
        }
    }
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal