Utils.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. <?php
  2. namespace AlibabaCloud\Tea\Utils;
  3. use AlibabaCloud\Tea\Model;
  4. use GuzzleHttp\Psr7\Stream;
  5. use Psr\Http\Message\StreamInterface;
  6. class Utils
  7. {
  8. private static $defaultUserAgent = '';
  9. /**
  10. * Convert a string(utf8) to bytes.
  11. *
  12. * @param string $string
  13. *
  14. * @return array the return bytes
  15. */
  16. public static function toBytes($string)
  17. {
  18. $bytes = [];
  19. for ($i = 0; $i < \strlen($string); ++$i) {
  20. $bytes[] = \ord($string[$i]);
  21. }
  22. return $bytes;
  23. }
  24. /**
  25. * Convert a bytes to string(utf8).
  26. *
  27. * @param array $bytes
  28. *
  29. * @return string the return string
  30. */
  31. public static function toString($bytes)
  32. {
  33. $str = '';
  34. foreach ($bytes as $ch) {
  35. $str .= \chr($ch);
  36. }
  37. return $str;
  38. }
  39. /**
  40. * Parse it by JSON format.
  41. *
  42. * @param string $jsonString
  43. *
  44. * @return array the parsed result
  45. */
  46. public static function parseJSON($jsonString)
  47. {
  48. return json_decode($jsonString, true);
  49. }
  50. /**
  51. * Read data from a readable stream, and compose it to a bytes.
  52. *
  53. * @param StreamInterface $stream the readable stream
  54. *
  55. * @return array the bytes result
  56. */
  57. public static function readAsBytes($stream)
  58. {
  59. $str = self::readAsString($stream);
  60. return self::toBytes($str);
  61. }
  62. /**
  63. * Read data from a readable stream, and compose it to a string.
  64. *
  65. * @param StreamInterface $stream the readable stream
  66. *
  67. * @return string the string result
  68. */
  69. public static function readAsString($stream)
  70. {
  71. if ($stream->isSeekable()) {
  72. $stream->rewind();
  73. }
  74. return $stream->getContents();
  75. }
  76. /**
  77. * Read data from a readable stream, and parse it by JSON format.
  78. *
  79. * @param StreamInterface $stream the readable stream
  80. *
  81. * @return array the parsed result
  82. */
  83. public static function readAsJSON($stream)
  84. {
  85. return self::parseJSON(self::readAsString($stream));
  86. }
  87. /**
  88. * Generate a nonce string.
  89. *
  90. * @return string the nonce string
  91. */
  92. public static function getNonce()
  93. {
  94. return md5(uniqid() . uniqid(md5(microtime(true)), true));
  95. }
  96. /**
  97. * Get an UTC format string by current date, e.g. 'Thu, 06 Feb 2020 07:32:54 GMT'.
  98. *
  99. * @return string the UTC format string
  100. */
  101. public static function getDateUTCString()
  102. {
  103. return gmdate('D, d M Y H:i:s T');
  104. }
  105. /**
  106. * If not set the real, use default value.
  107. *
  108. * @param string $real
  109. * @param string $default
  110. *
  111. * @return string
  112. */
  113. public static function defaultString($real, $default = '')
  114. {
  115. return null === $real ? $default : $real;
  116. }
  117. /**
  118. * If not set the real, use default value.
  119. *
  120. * @param int $real
  121. * @param int $default
  122. *
  123. * @return int the return number
  124. */
  125. public static function defaultNumber($real, $default = 0)
  126. {
  127. if (null === $real) {
  128. return $default;
  129. }
  130. return (int) $real;
  131. }
  132. /**
  133. * Format a map to form string, like a=a%20b%20c.
  134. *
  135. * @param array|object $query
  136. *
  137. * @return string the form string
  138. */
  139. public static function toFormString($query)
  140. {
  141. if (null === $query) {
  142. return '';
  143. }
  144. if (\is_object($query)) {
  145. $query = json_decode(self::toJSONString($query), true);
  146. }
  147. return str_replace('+', '%20', http_build_query($query));
  148. }
  149. /**
  150. * If not set the real, use default value.
  151. *
  152. * @param array|Model $object
  153. *
  154. * @return string the return string
  155. */
  156. public static function toJSONString($object)
  157. {
  158. if (is_string($object)) {
  159. return $object;
  160. }
  161. if ($object instanceof Model) {
  162. $object = $object->toMap();
  163. }
  164. return json_encode($object);
  165. }
  166. /**
  167. * Check the string is empty?
  168. *
  169. * @param string $val
  170. *
  171. * @return bool if string is null or zero length, return true
  172. *
  173. * @deprecated
  174. */
  175. public static function _empty($val)
  176. {
  177. return empty($val);
  178. }
  179. /**
  180. * Check the string is empty?
  181. *
  182. * @param string $val
  183. *
  184. * @return bool if string is null or zero length, return true
  185. *
  186. * @deprecated
  187. */
  188. public static function emptyWithSuffix($val)
  189. {
  190. return empty($val);
  191. }
  192. /**
  193. * Check the string is empty?
  194. *
  195. * @param string $val
  196. *
  197. * @return bool if string is null or zero length, return true
  198. */
  199. public static function empty_($val)
  200. {
  201. return empty($val);
  202. }
  203. /**
  204. * Check one string equals another one?
  205. *
  206. * @param int $left
  207. * @param int $right
  208. *
  209. * @return bool if equals, return true
  210. */
  211. public static function equalString($left, $right)
  212. {
  213. return $left === $right;
  214. }
  215. /**
  216. * Check one number equals another one?
  217. *
  218. * @param int $left
  219. * @param int $right
  220. *
  221. * @return bool if equals, return true
  222. */
  223. public static function equalNumber($left, $right)
  224. {
  225. return $left === $right;
  226. }
  227. /**
  228. * Check one value is unset.
  229. *
  230. * @param mixed $value
  231. *
  232. * @return bool if unset, return true
  233. */
  234. public static function isUnset(&$value = null)
  235. {
  236. return !isset($value) || null === $value;
  237. }
  238. /**
  239. * Stringify the value of map.
  240. *
  241. * @param array $map
  242. *
  243. * @return array the new stringified map
  244. */
  245. public static function stringifyMapValue($map)
  246. {
  247. if (null === $map) {
  248. return [];
  249. }
  250. foreach ($map as &$node) {
  251. if (is_numeric($node)) {
  252. $node = (string) $node;
  253. } elseif (null === $node) {
  254. $node = '';
  255. } elseif (\is_bool($node)) {
  256. $node = true === $node ? 'true' : 'false';
  257. } elseif (\is_object($node)) {
  258. $node = json_decode(json_encode($node), true);
  259. }
  260. }
  261. return $map;
  262. }
  263. /**
  264. * Anyify the value of map.
  265. *
  266. * @param array $m
  267. *
  268. * @return array the new anyfied map
  269. */
  270. public static function anyifyMapValue($m)
  271. {
  272. return $m;
  273. }
  274. /**
  275. * Assert a value, if it is a boolean, return it, otherwise throws.
  276. *
  277. * @param mixed $value
  278. *
  279. * @return bool the boolean value
  280. */
  281. public static function assertAsBoolean($value)
  282. {
  283. if (\is_bool($value)) {
  284. return $value;
  285. }
  286. throw new \InvalidArgumentException('It is not a boolean value.');
  287. }
  288. /**
  289. * Assert a value, if it is a string, return it, otherwise throws.
  290. *
  291. * @param mixed $value
  292. *
  293. * @return string the string value
  294. */
  295. public static function assertAsString($value)
  296. {
  297. if (\is_string($value)) {
  298. return $value;
  299. }
  300. throw new \InvalidArgumentException('It is not a string value.');
  301. }
  302. private static function is_bytes($value)
  303. {
  304. if (!\is_array($value)) {
  305. return false;
  306. }
  307. $i = 0;
  308. foreach ($value as $k => $ord) {
  309. if ($k !== $i) {
  310. return false;
  311. }
  312. if (!\is_int($ord)) {
  313. return false;
  314. }
  315. if ($ord < 0 || $ord > 255) {
  316. return false;
  317. }
  318. ++$i;
  319. }
  320. return true;
  321. }
  322. /**
  323. * Assert a value, if it is a bytes, return it, otherwise throws.
  324. *
  325. * @param mixed $value
  326. *
  327. * @return bytes the bytes value
  328. */
  329. public static function assertAsBytes($value)
  330. {
  331. if (self::is_bytes($value)) {
  332. return $value;
  333. }
  334. throw new \InvalidArgumentException('It is not a bytes value.');
  335. }
  336. /**
  337. * Assert a value, if it is a number, return it, otherwise throws.
  338. *
  339. * @param mixed $value
  340. *
  341. * @return bool the number value
  342. */
  343. public static function assertAsNumber($value)
  344. {
  345. if (\is_numeric($value)) {
  346. return $value;
  347. }
  348. throw new \InvalidArgumentException('It is not a number value.');
  349. }
  350. /**
  351. * Assert a value, if it is a map, return it, otherwise throws.
  352. *
  353. * @param $any
  354. *
  355. * @return array the map value
  356. */
  357. public static function assertAsMap($any)
  358. {
  359. if (\is_array($any)) {
  360. return $any;
  361. }
  362. throw new \InvalidArgumentException('It is not a map value.');
  363. }
  364. public static function assertAsArray($any){
  365. if (\is_array($any)) {
  366. return $any;
  367. }
  368. throw new \InvalidArgumentException('It is not a array value.');
  369. }
  370. /**
  371. * Get user agent, if it userAgent is not null, splice it with defaultUserAgent and return, otherwise return
  372. * defaultUserAgent.
  373. *
  374. * @param string $userAgent
  375. *
  376. * @return string the string value
  377. */
  378. public static function getUserAgent($userAgent = '')
  379. {
  380. if (empty(self::$defaultUserAgent)) {
  381. self::$defaultUserAgent = sprintf('AlibabaCloud (%s; %s) PHP/%s Core/3.1 TeaDSL/1', PHP_OS, \PHP_SAPI, PHP_VERSION);
  382. }
  383. if (!empty($userAgent)) {
  384. return self::$defaultUserAgent . ' ' . $userAgent;
  385. }
  386. return self::$defaultUserAgent;
  387. }
  388. /**
  389. * If the code between 200 and 300, return true, or return false.
  390. *
  391. * @param int $code
  392. *
  393. * @return bool
  394. */
  395. public static function is2xx($code)
  396. {
  397. return $code >= 200 && $code < 300;
  398. }
  399. /**
  400. * If the code between 300 and 400, return true, or return false.
  401. *
  402. * @param int $code
  403. *
  404. * @return bool
  405. */
  406. public static function is3xx($code)
  407. {
  408. return $code >= 300 && $code < 400;
  409. }
  410. /**
  411. * If the code between 400 and 500, return true, or return false.
  412. *
  413. * @param int $code
  414. *
  415. * @return bool
  416. */
  417. public static function is4xx($code)
  418. {
  419. return $code >= 400 && $code < 500;
  420. }
  421. /**
  422. * If the code between 500 and 600, return true, or return false.
  423. *
  424. * @param int $code
  425. *
  426. * @return bool
  427. */
  428. public static function is5xx($code)
  429. {
  430. return $code >= 500 && $code < 600;
  431. }
  432. /**
  433. * Validate model.
  434. *
  435. * @param Model $model
  436. */
  437. public static function validateModel($model)
  438. {
  439. if (null !== $model) {
  440. $model->validate();
  441. }
  442. }
  443. /**
  444. * Model transforms to map[string]any.
  445. *
  446. * @param Model $model
  447. *
  448. * @return array
  449. */
  450. public static function toMap($model)
  451. {
  452. if (null === $model) {
  453. return [];
  454. }
  455. $map = $model->toMap();
  456. $names = $model->getName();
  457. $vars = get_object_vars($model);
  458. foreach ($vars as $k => $v) {
  459. if (false !== strpos($k, 'Shrink') && !isset($names[$k])) {
  460. // A field that has the suffix `Shrink` and is not a Model class property.
  461. $targetKey = ucfirst(substr($k, 0, \strlen($k) - 6));
  462. if (isset($map[$targetKey])) {
  463. // $targetKey exists in $map.
  464. $map[$targetKey] = $v;
  465. }
  466. }
  467. }
  468. return $map;
  469. }
  470. /**
  471. * Suspends the current thread for the specified number of milliseconds.
  472. *
  473. * @param int $millisecond
  474. */
  475. public static function sleep($millisecond)
  476. {
  477. usleep($millisecond * 1000);
  478. }
  479. /**
  480. * Transform input as array.
  481. *
  482. * @param mixed $input
  483. *
  484. * @return array
  485. */
  486. public static function toArray($input)
  487. {
  488. if (\is_array($input)) {
  489. foreach ($input as $k => &$v) {
  490. $v = self::toArray($v);
  491. }
  492. } elseif ($input instanceof Model) {
  493. $input = $input->toMap();
  494. foreach ($input as $k => &$v) {
  495. $v = self::toArray($v);
  496. }
  497. }
  498. return $input;
  499. }
  500. /**
  501. * Assert a value, if it is a readable, return it, otherwise throws.
  502. *
  503. * @param mixed $value
  504. *
  505. * @return Stream the readable value
  506. */
  507. public static function assertAsReadable($value)
  508. {
  509. if (\is_string($value)) {
  510. return new Stream(
  511. fopen('data://text/plain;base64,' .
  512. base64_encode($value), 'r')
  513. );
  514. }
  515. if ($value instanceof Stream) {
  516. return $value;
  517. }
  518. throw new \InvalidArgumentException('It is not a stream value.');
  519. }
  520. }