function AnyBase::unpack
This method will try to resolve the type_url in Any message to get the targeted message type. If failed, an error will be thrown. Otherwise, the method will create a message of the targeted type and fill it with the decoded value in Any.
Return value
Message unpacked message
Throws
\Exception Type url needs to be type.googleapis.com/fully-qualified.
\Exception Class hasn't been added to descriptor pool.
\Exception cannot decode data in value field.
File
-
vendor/
google/ protobuf/ src/ Google/ Protobuf/ Internal/ AnyBase.php, line 23
Class
- AnyBase
- Base class for Google\Protobuf\Any, this contains hand-written convenience methods like pack() and unpack().
Namespace
Google\Protobuf\InternalCode
public function unpack() {
// Get fully qualified name from type url.
$url_prifix_len = strlen(GPBUtil::TYPE_URL_PREFIX);
if (substr($this->type_url, 0, $url_prifix_len) != GPBUtil::TYPE_URL_PREFIX) {
throw new \Exception("Type url needs to be type.googleapis.com/fully-qulified");
}
$fully_qualified_name = substr($this->type_url, $url_prifix_len);
// Create message according to fully qualified name.
$pool = \Google\Protobuf\Internal\DescriptorPool::getGeneratedPool();
$desc = $pool->getDescriptorByProtoName($fully_qualified_name);
if (is_null($desc)) {
throw new \Exception("Class " . $fully_qualified_name . " hasn't been added to descriptor pool");
}
$klass = $desc->getClass();
$msg = new $klass();
// Merge data into message.
$msg->mergeFromString($this->value);
return $msg;
}