<?php

namespace Google\Protobuf\Internal;





class AnyBase extends \Google\Protobuf\Internal\Message
{
    const TYPE_URL_PREFIX = 'type.googleapis.com/';

    









    public function unpack()
    {
         
        $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);

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

         
        $msg->mergeFromString($this->value);
        return $msg;
    }

    




    public function pack($msg)
    {
        if (!$msg instanceof Message) {
            trigger_error("Given parameter is not a message instance.",
                          E_USER_ERROR);
            return;
        }

         
        $this->value = $msg->serializeToString();

         
        $pool = \Google\Protobuf\Internal\DescriptorPool::getGeneratedPool();
        $desc = $pool->getDescriptorByClassName(get_class($msg));
        $fully_qualified_name = $desc->getFullName();
        $this->type_url = GPBUtil::TYPE_URL_PREFIX . $fully_qualified_name;
    }

    




    public function is($klass)
    {
        $pool = \Google\Protobuf\Internal\DescriptorPool::getGeneratedPool();
        $desc = $pool->getDescriptorByClassName($klass);
        $fully_qualified_name = $desc->getFullName();
        $type_url = GPBUtil::TYPE_URL_PREFIX . $fully_qualified_name;
        return $this->type_url === $type_url;
    }
}
