generated from inherelab/php-pkg-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFlagType.php
190 lines (159 loc) · 4.26 KB
/
FlagType.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php declare(strict_types=1);
/**
* This file is part of toolkit/pflag.
*
* @link https://github.com/php-toolkit
* @author https://github.com/inhere
* @license MIT
*/
namespace Toolkit\PFlag;
use Toolkit\PFlag\Exception\FlagException;
use Toolkit\Stdlib\Str;
use function is_scalar;
use function is_string;
use function strtoupper;
use function trim;
/**
* Class FlagConst
*
* @package Toolkit\PFlag
*/
class FlagType
{
public const INT = 'int';
public const BOOL = 'bool';
public const FLOAT = 'float';
public const STRING = 'string';
// ------ complex types ------
public const ARRAY = 'array';
public const OBJECT = 'object';
public const CALLABLE = 'callable';
// ------ extend types ------
public const INTS = 'ints';
public const STRINGS = 'strings';
public const MIXED = 'mixed';
public const CUSTOM = 'custom';
public const UNKNOWN = 'unknown';
public const ARRAY_TYPES = [
self::ARRAY => 2,
self::INTS => 3,
self::STRINGS => 3,
];
public const TYPES_MAP = [
self::INT => 1,
self::BOOL => 1,
self::FLOAT => 1,
self::STRING => 1,
// ------ complex types ------
self::ARRAY => 2,
self::OBJECT => 2,
self::CALLABLE => 2,
// ------ extend types ------
self::INTS => 3,
self::STRINGS => 3,
self::MIXED => 3,
self::CUSTOM => 3,
self::UNKNOWN => 3,
];
public const TYPE_HELP_NAME = [
// self::INTS => 'int...',
// self::STRINGS => 'string...',
self::ARRAY => '',
self::BOOL => '',
];
/**
* @param string $type
*
* @return bool
*/
public static function isValid(string $type): bool
{
return isset(self::TYPES_MAP[$type]);
}
/**
* @param string $type
*
* @return bool
*/
public static function isArray(string $type): bool
{
return isset(self::ARRAY_TYPES[$type]);
}
/**
* @param string $type
* @param bool $toUpper
*
* @return string
*/
public static function getHelpName(string $type, bool $toUpper = true): string
{
$name = self::TYPE_HELP_NAME[$type] ?? $type;
return $toUpper ? strtoupper($name) : $name;
}
/**
* Get type default value.
*
* @param string $type
*
* @return array|false|float|int|string|null
*/
public static function getDefault(string $type): float|bool|int|array|string|null
{
return match ($type) {
self::INT => 0,
self::BOOL => false,
self::FLOAT => 0.0,
self::STRING => '',
self::INTS, self::ARRAY, self::STRINGS => [],
default => null,
};
}
/**
* @param string $type
* @param mixed $value
*
* @return mixed
*/
public static function fmtBasicTypeValue(string $type, mixed $value): mixed
{
if (!is_scalar($value)) {
return $value;
}
// convert to bool
if ($type === self::BOOL) {
$value = is_string($value) ? Str::tryToBool($value) : (bool)$value;
if (is_string($value)) {
throw new FlagException("convert value '$value' to bool failed");
}
return $value;
}
// format value by type
return match ($type) {
self::INT, self::INTS => (int)$value,
// self::BOOL => is_string($value) ? Str::toBool2($value) : (bool)$value,
self::FLOAT => (float)$value,
self::STRING, self::STRINGS => (string)$value,
default => $value,
};
}
/**
* Convert string to array
*
* - eg: '23, 45' => [23, 45]
* - eg: 'a, b' => ['a', 'b']
* - eg: '[a, b]' => ['a', 'b']
*
* @param string $type
* @param string $str
*
* @return array|string
*/
public static function str2ArrValue(string $type, string $str): array|string
{
return match ($type) {
self::INTS => Str::toInts(trim($str, '[] ')),
self::ARRAY, self::STRINGS => Str::toArray(trim($str, '[] ')),
default => $str,
};
}
}