Utils.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  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 (null === $object) {
  159. $object = new \stdClass();
  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. return \is_bool($value);
  284. }
  285. /**
  286. * Assert a value, if it is a string, return it, otherwise throws.
  287. *
  288. * @param mixed $value
  289. *
  290. * @return bool the string value
  291. */
  292. public static function assertAsString($value)
  293. {
  294. return \is_string($value);
  295. }
  296. /**
  297. * Assert a value, if it is a bytes, return it, otherwise throws.
  298. *
  299. * @param mixed $value
  300. *
  301. * @return bool the bytes value
  302. */
  303. public static function assertAsBytes($value)
  304. {
  305. if (!\is_array($value)) {
  306. return false;
  307. }
  308. $i = 0;
  309. foreach ($value as $k => $ord) {
  310. if ($k !== $i) {
  311. return false;
  312. }
  313. if (!\is_int($ord)) {
  314. return false;
  315. }
  316. if ($ord < 0 || $ord > 255) {
  317. return false;
  318. }
  319. ++$i;
  320. }
  321. return true;
  322. }
  323. /**
  324. * Assert a value, if it is a number, return it, otherwise throws.
  325. *
  326. * @param mixed $value
  327. *
  328. * @return bool the number value
  329. */
  330. public static function assertAsNumber($value)
  331. {
  332. return is_numeric($value);
  333. }
  334. /**
  335. * Assert a value, if it is a map, return it, otherwise throws.
  336. *
  337. * @param $any
  338. *
  339. * @return array the map value
  340. */
  341. public static function assertAsMap($any)
  342. {
  343. if (\is_array($any)) {
  344. return $any;
  345. }
  346. throw new \InvalidArgumentException('It is not a map value.');
  347. }
  348. public static function assertAsArray($any){
  349. if (\is_array($any)) {
  350. return $any;
  351. }
  352. throw new \InvalidArgumentException('It is not a array value.');
  353. }
  354. /**
  355. * Get user agent, if it userAgent is not null, splice it with defaultUserAgent and return, otherwise return
  356. * defaultUserAgent.
  357. *
  358. * @param string $userAgent
  359. *
  360. * @return string the string value
  361. */
  362. public static function getUserAgent($userAgent = '')
  363. {
  364. if (empty(self::$defaultUserAgent)) {
  365. self::$defaultUserAgent = sprintf('AlibabaCloud (%s; %s) PHP/%s Core/3.1 TeaDSL/1', PHP_OS, \PHP_SAPI, PHP_VERSION);
  366. }
  367. if (!empty($userAgent)) {
  368. return self::$defaultUserAgent . ' ' . $userAgent;
  369. }
  370. return self::$defaultUserAgent;
  371. }
  372. /**
  373. * If the code between 200 and 300, return true, or return false.
  374. *
  375. * @param int $code
  376. *
  377. * @return bool
  378. */
  379. public static function is2xx($code)
  380. {
  381. return $code >= 200 && $code < 300;
  382. }
  383. /**
  384. * If the code between 300 and 400, return true, or return false.
  385. *
  386. * @param int $code
  387. *
  388. * @return bool
  389. */
  390. public static function is3xx($code)
  391. {
  392. return $code >= 300 && $code < 400;
  393. }
  394. /**
  395. * If the code between 400 and 500, return true, or return false.
  396. *
  397. * @param int $code
  398. *
  399. * @return bool
  400. */
  401. public static function is4xx($code)
  402. {
  403. return $code >= 400 && $code < 500;
  404. }
  405. /**
  406. * If the code between 500 and 600, return true, or return false.
  407. *
  408. * @param int $code
  409. *
  410. * @return bool
  411. */
  412. public static function is5xx($code)
  413. {
  414. return $code >= 500 && $code < 600;
  415. }
  416. /**
  417. * Validate model.
  418. *
  419. * @param Model $model
  420. */
  421. public static function validateModel($model)
  422. {
  423. if (null !== $model) {
  424. $model->validate();
  425. }
  426. }
  427. /**
  428. * Model transforms to map[string]any.
  429. *
  430. * @param Model $model
  431. *
  432. * @return array
  433. */
  434. public static function toMap($model)
  435. {
  436. if (null === $model) {
  437. return [];
  438. }
  439. $map = $model->toMap();
  440. $names = $model->getName();
  441. $vars = get_object_vars($model);
  442. foreach ($vars as $k => $v) {
  443. if (false !== strpos($k, 'Shrink') && !isset($names[$k])) {
  444. // A field that has the suffix `Shrink` and is not a Model class property.
  445. $targetKey = ucfirst(substr($k, 0, \strlen($k) - 6));
  446. if (isset($map[$targetKey])) {
  447. // $targetKey exists in $map.
  448. $map[$targetKey] = $v;
  449. }
  450. }
  451. }
  452. return $map;
  453. }
  454. /**
  455. * Suspends the current thread for the specified number of milliseconds.
  456. *
  457. * @param int $millisecond
  458. */
  459. public static function sleep($millisecond)
  460. {
  461. usleep($millisecond * 1000);
  462. }
  463. /**
  464. * Transform input as array.
  465. *
  466. * @param mixed $input
  467. *
  468. * @return array
  469. */
  470. public static function toArray($input)
  471. {
  472. if (\is_array($input)) {
  473. foreach ($input as $k => &$v) {
  474. $v = self::toArray($v);
  475. }
  476. } elseif ($input instanceof Model) {
  477. $input = $input->toMap();
  478. foreach ($input as $k => &$v) {
  479. $v = self::toArray($v);
  480. }
  481. }
  482. return $input;
  483. }
  484. /**
  485. * Assert a value, if it is a readable, return it, otherwise throws.
  486. *
  487. * @param mixed $value
  488. *
  489. * @return Stream the readable value
  490. */
  491. public static function assertAsReadable($value)
  492. {
  493. if (\is_string($value)) {
  494. return new Stream(
  495. fopen('data://text/plain;base64,' .
  496. base64_encode($value), 'r')
  497. );
  498. }
  499. if ($value instanceof Stream) {
  500. return $value;
  501. }
  502. throw new \InvalidArgumentException('It is not a stream value.');
  503. }
  504. }