Any.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. <?php
  2. # Generated by the protocol buffer compiler. DO NOT EDIT!
  3. # source: google/protobuf/any.proto
  4. namespace Google\Protobuf;
  5. use Google\Protobuf\Internal\GPBType;
  6. use Google\Protobuf\Internal\Message;
  7. use Google\Protobuf\Internal\RepeatedField;
  8. use Google\Protobuf\Internal\GPBUtil;
  9. /**
  10. * `Any` contains an arbitrary serialized protocol buffer message along with a
  11. * URL that describes the type of the serialized message.
  12. * Protobuf library provides support to pack/unpack Any values in the form
  13. * of utility functions or additional generated methods of the Any type.
  14. * Example 1: Pack and unpack a message in C++.
  15. * Foo foo = ...;
  16. * Any any;
  17. * any.PackFrom(foo);
  18. * ...
  19. * if (any.UnpackTo(&foo)) {
  20. * ...
  21. * }
  22. * Example 2: Pack and unpack a message in Java.
  23. * Foo foo = ...;
  24. * Any any = Any.pack(foo);
  25. * ...
  26. * if (any.is(Foo.class)) {
  27. * foo = any.unpack(Foo.class);
  28. * }
  29. * Example 3: Pack and unpack a message in Python.
  30. * foo = Foo(...)
  31. * any = Any()
  32. * any.Pack(foo)
  33. * ...
  34. * if any.Is(Foo.DESCRIPTOR):
  35. * any.Unpack(foo)
  36. * ...
  37. * Example 4: Pack and unpack a message in Go
  38. * foo := &pb.Foo{...}
  39. * any, err := ptypes.MarshalAny(foo)
  40. * ...
  41. * foo := &pb.Foo{}
  42. * if err := ptypes.UnmarshalAny(any, foo); err != nil {
  43. * ...
  44. * }
  45. * The pack methods provided by protobuf library will by default use
  46. * 'type.googleapis.com/full.type.name' as the type URL and the unpack
  47. * methods only use the fully qualified type name after the last '/'
  48. * in the type URL, for example "foo.bar.com/x/y.z" will yield type
  49. * name "y.z".
  50. * JSON
  51. * ====
  52. * The JSON representation of an `Any` value uses the regular
  53. * representation of the deserialized, embedded message, with an
  54. * additional field `&#64;type` which contains the type URL. Example:
  55. * package google.profile;
  56. * message Person {
  57. * string first_name = 1;
  58. * string last_name = 2;
  59. * }
  60. * {
  61. * "&#64;type": "type.googleapis.com/google.profile.Person",
  62. * "firstName": <string>,
  63. * "lastName": <string>
  64. * }
  65. * If the embedded message type is well-known and has a custom JSON
  66. * representation, that representation will be embedded adding a field
  67. * `value` which holds the custom JSON in addition to the `&#64;type`
  68. * field. Example (for message [google.protobuf.Duration][]):
  69. * {
  70. * "&#64;type": "type.googleapis.com/google.protobuf.Duration",
  71. * "value": "1.212s"
  72. * }
  73. *
  74. * Generated from protobuf message <code>google.protobuf.Any</code>
  75. */
  76. class Any extends \Google\Protobuf\Internal\Message
  77. {
  78. /**
  79. * A URL/resource name that uniquely identifies the type of the serialized
  80. * protocol buffer message. The last segment of the URL's path must represent
  81. * the fully qualified name of the type (as in
  82. * `path/google.protobuf.Duration`). The name should be in a canonical form
  83. * (e.g., leading "." is not accepted).
  84. * In practice, teams usually precompile into the binary all types that they
  85. * expect it to use in the context of Any. However, for URLs which use the
  86. * scheme `http`, `https`, or no scheme, one can optionally set up a type
  87. * server that maps type URLs to message definitions as follows:
  88. * * If no scheme is provided, `https` is assumed.
  89. * * An HTTP GET on the URL must yield a [google.protobuf.Type][]
  90. * value in binary format, or produce an error.
  91. * * Applications are allowed to cache lookup results based on the
  92. * URL, or have them precompiled into a binary to avoid any
  93. * lookup. Therefore, binary compatibility needs to be preserved
  94. * on changes to types. (Use versioned type names to manage
  95. * breaking changes.)
  96. * Note: this functionality is not currently available in the official
  97. * protobuf release, and it is not used for type URLs beginning with
  98. * type.googleapis.com.
  99. * Schemes other than `http`, `https` (or the empty scheme) might be
  100. * used with implementation specific semantics.
  101. *
  102. * Generated from protobuf field <code>string type_url = 1;</code>
  103. */
  104. private $type_url = '';
  105. /**
  106. * Must be a valid serialized protocol buffer of the above specified type.
  107. *
  108. * Generated from protobuf field <code>bytes value = 2;</code>
  109. */
  110. private $value = '';
  111. const TYPE_URL_PREFIX = 'type.googleapis.com/';
  112. /**
  113. * Constructor.
  114. *
  115. * @param array $data {
  116. * Optional. Data for populating the Message object.
  117. *
  118. * @type string $type_url
  119. * A URL/resource name that uniquely identifies the type of the serialized
  120. * protocol buffer message. The last segment of the URL's path must represent
  121. * the fully qualified name of the type (as in
  122. * `path/google.protobuf.Duration`). The name should be in a canonical form
  123. * (e.g., leading "." is not accepted).
  124. * In practice, teams usually precompile into the binary all types that they
  125. * expect it to use in the context of Any. However, for URLs which use the
  126. * scheme `http`, `https`, or no scheme, one can optionally set up a type
  127. * server that maps type URLs to message definitions as follows:
  128. * * If no scheme is provided, `https` is assumed.
  129. * * An HTTP GET on the URL must yield a [google.protobuf.Type][]
  130. * value in binary format, or produce an error.
  131. * * Applications are allowed to cache lookup results based on the
  132. * URL, or have them precompiled into a binary to avoid any
  133. * lookup. Therefore, binary compatibility needs to be preserved
  134. * on changes to types. (Use versioned type names to manage
  135. * breaking changes.)
  136. * Note: this functionality is not currently available in the official
  137. * protobuf release, and it is not used for type URLs beginning with
  138. * type.googleapis.com.
  139. * Schemes other than `http`, `https` (or the empty scheme) might be
  140. * used with implementation specific semantics.
  141. * @type string $value
  142. * Must be a valid serialized protocol buffer of the above specified type.
  143. * }
  144. */
  145. public function __construct($data = NULL) {
  146. \GPBMetadata\Google\Protobuf\Any::initOnce();
  147. parent::__construct($data);
  148. }
  149. /**
  150. * A URL/resource name that uniquely identifies the type of the serialized
  151. * protocol buffer message. The last segment of the URL's path must represent
  152. * the fully qualified name of the type (as in
  153. * `path/google.protobuf.Duration`). The name should be in a canonical form
  154. * (e.g., leading "." is not accepted).
  155. * In practice, teams usually precompile into the binary all types that they
  156. * expect it to use in the context of Any. However, for URLs which use the
  157. * scheme `http`, `https`, or no scheme, one can optionally set up a type
  158. * server that maps type URLs to message definitions as follows:
  159. * * If no scheme is provided, `https` is assumed.
  160. * * An HTTP GET on the URL must yield a [google.protobuf.Type][]
  161. * value in binary format, or produce an error.
  162. * * Applications are allowed to cache lookup results based on the
  163. * URL, or have them precompiled into a binary to avoid any
  164. * lookup. Therefore, binary compatibility needs to be preserved
  165. * on changes to types. (Use versioned type names to manage
  166. * breaking changes.)
  167. * Note: this functionality is not currently available in the official
  168. * protobuf release, and it is not used for type URLs beginning with
  169. * type.googleapis.com.
  170. * Schemes other than `http`, `https` (or the empty scheme) might be
  171. * used with implementation specific semantics.
  172. *
  173. * Generated from protobuf field <code>string type_url = 1;</code>
  174. * @return string
  175. */
  176. public function getTypeUrl()
  177. {
  178. return $this->type_url;
  179. }
  180. /**
  181. * A URL/resource name that uniquely identifies the type of the serialized
  182. * protocol buffer message. The last segment of the URL's path must represent
  183. * the fully qualified name of the type (as in
  184. * `path/google.protobuf.Duration`). The name should be in a canonical form
  185. * (e.g., leading "." is not accepted).
  186. * In practice, teams usually precompile into the binary all types that they
  187. * expect it to use in the context of Any. However, for URLs which use the
  188. * scheme `http`, `https`, or no scheme, one can optionally set up a type
  189. * server that maps type URLs to message definitions as follows:
  190. * * If no scheme is provided, `https` is assumed.
  191. * * An HTTP GET on the URL must yield a [google.protobuf.Type][]
  192. * value in binary format, or produce an error.
  193. * * Applications are allowed to cache lookup results based on the
  194. * URL, or have them precompiled into a binary to avoid any
  195. * lookup. Therefore, binary compatibility needs to be preserved
  196. * on changes to types. (Use versioned type names to manage
  197. * breaking changes.)
  198. * Note: this functionality is not currently available in the official
  199. * protobuf release, and it is not used for type URLs beginning with
  200. * type.googleapis.com.
  201. * Schemes other than `http`, `https` (or the empty scheme) might be
  202. * used with implementation specific semantics.
  203. *
  204. * Generated from protobuf field <code>string type_url = 1;</code>
  205. * @param string $var
  206. * @return $this
  207. */
  208. public function setTypeUrl($var)
  209. {
  210. GPBUtil::checkString($var, True);
  211. $this->type_url = $var;
  212. return $this;
  213. }
  214. /**
  215. * Must be a valid serialized protocol buffer of the above specified type.
  216. *
  217. * Generated from protobuf field <code>bytes value = 2;</code>
  218. * @return string
  219. */
  220. public function getValue()
  221. {
  222. return $this->value;
  223. }
  224. /**
  225. * Must be a valid serialized protocol buffer of the above specified type.
  226. *
  227. * Generated from protobuf field <code>bytes value = 2;</code>
  228. * @param string $var
  229. * @return $this
  230. */
  231. public function setValue($var)
  232. {
  233. GPBUtil::checkString($var, False);
  234. $this->value = $var;
  235. return $this;
  236. }
  237. /**
  238. * This method will try to resolve the type_url in Any message to get the
  239. * targeted message type. If failed, an error will be thrown. Otherwise,
  240. * the method will create a message of the targeted type and fill it with
  241. * the decoded value in Any.
  242. * @return unpacked message
  243. * @throws Exception Type url needs to be type.googleapis.com/fully-qulified.
  244. * @throws Exception Class hasn't been added to descriptor pool.
  245. * @throws Exception cannot decode data in value field.
  246. */
  247. public function unpack()
  248. {
  249. // Get fully qualifed name from type url.
  250. $url_prifix_len = strlen(GPBUtil::TYPE_URL_PREFIX);
  251. if (substr($this->type_url, 0, $url_prifix_len) !=
  252. GPBUtil::TYPE_URL_PREFIX) {
  253. throw new \Exception(
  254. "Type url needs to be type.googleapis.com/fully-qulified");
  255. }
  256. $fully_qualifed_name =
  257. substr($this->type_url, $url_prifix_len);
  258. // Create message according to fully qualified name.
  259. $pool = \Google\Protobuf\Internal\DescriptorPool::getGeneratedPool();
  260. $desc = $pool->getDescriptorByProtoName( ".".$fully_qualifed_name);
  261. if (is_null($desc)) {
  262. throw new \Exception("Class ".$fully_qualifed_name
  263. ." hasn't been added to descriptor pool");
  264. }
  265. $klass = $desc->getClass();
  266. $msg = new $klass();
  267. // Merge data into message.
  268. $msg->mergeFromString($this->value);
  269. return $msg;
  270. }
  271. /**
  272. * The type_url will be created according to the given message’s type and
  273. * the value is encoded data from the given message..
  274. * @param message: A proto message.
  275. */
  276. public function pack($msg)
  277. {
  278. if (!$msg instanceof Message) {
  279. trigger_error("Given parameter is not a message instance.",
  280. E_USER_ERROR);
  281. return;
  282. }
  283. // Set value using serialzed message.
  284. $this->value = $msg->serializeToString();
  285. // Set type url.
  286. $pool = \Google\Protobuf\Internal\DescriptorPool::getGeneratedPool();
  287. $desc = $pool->getDescriptorByClassName(get_class($msg));
  288. $fully_qualifed_name = $desc->getFullName();
  289. $this->type_url = GPBUtil::TYPE_URL_PREFIX.substr(
  290. $fully_qualifed_name, 1, strlen($fully_qualifed_name));
  291. }
  292. /**
  293. * This method returns whether the type_url in any_message is corresponded
  294. * to the given class.
  295. * @param klass: The fully qualified PHP class name of a proto message type.
  296. */
  297. public function is($klass)
  298. {
  299. $pool = \Google\Protobuf\Internal\DescriptorPool::getGeneratedPool();
  300. $desc = $pool->getDescriptorByClassName($klass);
  301. $fully_qualifed_name = $desc->getFullName();
  302. $type_url = GPBUtil::TYPE_URL_PREFIX.substr(
  303. $fully_qualifed_name, 1, strlen($fully_qualifed_name));
  304. return $this->type_url === $type_url;
  305. }
  306. }