This repository was archived by the owner on Dec 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunctions.php
120 lines (102 loc) · 3.09 KB
/
functions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
/**
* This file is part of Open Swoole.
*
* @link https://www.swoole.co.uk
* @contact [email protected]
* @license https://github.com/openswoole/library/blob/master/LICENSE
*/
declare(strict_types=1);
if (PHP_VERSION_ID < 70200) {
throw new RuntimeException('require PHP version 7.2 or later');
}
if (SWOOLE_USE_SHORTNAME) {
function _string(string $string = ''): Swoole\StringObject
{
return new Swoole\StringObject($string);
}
function _mbstring(string $string = ''): Swoole\MultibyteStringObject
{
return new Swoole\MultibyteStringObject($string);
}
function _array(array $array = []): Swoole\ArrayObject
{
return new Swoole\ArrayObject($array);
}
}
function swoole_string(string $string = ''): Swoole\StringObject
{
return new Swoole\StringObject($string);
}
function swoole_mbstring(string $string = ''): Swoole\MultibyteStringObject
{
return new Swoole\MultibyteStringObject($string);
}
function swoole_array(array $array = []): Swoole\ArrayObject
{
return new Swoole\ArrayObject($array);
}
function swoole_table(int $size, string $fields): Swoole\Table
{
$_fields = swoole_string($fields)->trim()->split(',');
$table = new Swoole\Table($size, 0.25);
foreach ($_fields as $f) {
$_f = swoole_string($f)->trim()->split(':');
$name = $_f->get(0)->trim()->toString();
$type = $_f->get(1)->trim();
switch ($type) {
case 'i':
case 'int':
$table->column($name, Swoole\Table::TYPE_INT);
break;
case 'f':
case 'float':
$table->column($name, Swoole\Table::TYPE_FLOAT);
break;
case 's':
case 'string':
if ($_f->count() < 3) {
throw new RuntimeException('need to give string length');
}
$length = intval($_f->get(2)->trim()->toString());
if ($length <= 0) {
throw new RuntimeException("invalid string length[{$length}]");
}
$table->column($name, Swoole\Table::TYPE_STRING, $length);
break;
default:
throw new RuntimeException("unknown field type[{$type}]");
break;
}
}
if (!$table->create()) {
throw new RuntimeException('failed to create table');
}
return $table;
}
function swoole_array_list(...$arrray): Swoole\ArrayObject
{
return new Swoole\ArrayObject($arrray);
}
function swoole_array_default_value(array $array, $key, $default_value = null)
{
return array_key_exists($key, $array) ? $array[$key] : $default_value;
}
if (!function_exists('array_key_last')) {
function array_key_last(array $array)
{
if (!empty($array)) {
return key(array_slice($array, -1, 1, true));
}
return null;
}
}
if (!function_exists('array_key_first')) {
function array_key_first(array $array)
{
foreach ($array as $key => $unused) {
return $key;
}
return null;
}
}