functions.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * This file is part of Swoole.
  4. *
  5. * @link https://www.swoole.com
  6. * @contact team@swoole.com
  7. * @license https://github.com/swoole/library/blob/master/LICENSE
  8. */
  9. declare(strict_types=1);
  10. if (PHP_VERSION_ID < 70200) {
  11. throw new RuntimeException('require PHP version 7.2 or later');
  12. }
  13. if (SWOOLE_USE_SHORTNAME) {
  14. function _string(string $string = ''): Swoole\StringObject
  15. {
  16. return new Swoole\StringObject($string);
  17. }
  18. function _mbstring(string $string = ''): Swoole\MultibyteStringObject
  19. {
  20. return new Swoole\MultibyteStringObject($string);
  21. }
  22. function _array(array $array = []): Swoole\ArrayObject
  23. {
  24. return new Swoole\ArrayObject($array);
  25. }
  26. }
  27. function swoole_string(string $string = ''): Swoole\StringObject
  28. {
  29. return new Swoole\StringObject($string);
  30. }
  31. function swoole_mbstring(string $string = ''): Swoole\MultibyteStringObject
  32. {
  33. return new Swoole\MultibyteStringObject($string);
  34. }
  35. function swoole_array(array $array = []): Swoole\ArrayObject
  36. {
  37. return new Swoole\ArrayObject($array);
  38. }
  39. function swoole_table(int $size, string $fields): Swoole\Table
  40. {
  41. $_fields = swoole_string($fields)->trim()->split(',');
  42. $table = new Swoole\Table($size, 0.25);
  43. foreach ($_fields as $f) {
  44. $_f = swoole_string($f)->trim()->split(':');
  45. $name = $_f->get(0)->trim()->toString();
  46. $type = $_f->get(1)->trim();
  47. switch ($type) {
  48. case 'i':
  49. case 'int':
  50. $table->column($name, Swoole\Table::TYPE_INT);
  51. break;
  52. case 'f':
  53. case 'float':
  54. $table->column($name, Swoole\Table::TYPE_FLOAT);
  55. break;
  56. case 's':
  57. case 'string':
  58. if ($_f->count() < 3) {
  59. throw new RuntimeException('need to give string length');
  60. }
  61. $length = intval($_f->get(2)->trim()->toString());
  62. if ($length <= 0) {
  63. throw new RuntimeException("invalid string length[{$length}]");
  64. }
  65. $table->column($name, Swoole\Table::TYPE_STRING, $length);
  66. break;
  67. default:
  68. throw new RuntimeException("unknown field type[{$type}]");
  69. break;
  70. }
  71. }
  72. if (!$table->create()) {
  73. throw new RuntimeException('failed to create table');
  74. }
  75. return $table;
  76. }
  77. function swoole_array_list(...$arrray): Swoole\ArrayObject
  78. {
  79. return new Swoole\ArrayObject($arrray);
  80. }
  81. function swoole_array_default_value(array $array, $key, $default_value = null)
  82. {
  83. return array_key_exists($key, $array) ? $array[$key] : $default_value;
  84. }
  85. if (!function_exists('array_key_last')) {
  86. function array_key_last(array $array)
  87. {
  88. if (!empty($array)) {
  89. return key(array_slice($array, -1, 1, true));
  90. }
  91. return null;
  92. }
  93. }
  94. if (!function_exists('array_key_first')) {
  95. function array_key_first(array $array)
  96. {
  97. foreach ($array as $key => $unused) {
  98. return $key;
  99. }
  100. return null;
  101. }
  102. }