Skip to content

Commit aed7838

Browse files
committed
update some helper methods, add new util class
1 parent 58f9959 commit aed7838

File tree

10 files changed

+349
-28
lines changed

10 files changed

+349
-28
lines changed

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ jobs:
4040
run: |
4141
tag1=${GITHUB_REF#refs/*/}
4242
echo "release tag: ${tag1}"
43-
composer install --no-progress --no-suggest
43+
composer install --no-progress
4444
4545
# more see https://github.com/inhere/kite
4646
- name: Generate changelog file

src/Helper/Assert.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,32 @@ public static function naturalInt(int $value, string $errMsg = ''): void
139139
}
140140
}
141141

142+
/**
143+
* Positive integer. should > 0
144+
*
145+
* @param int $value
146+
* @param string $errMsg
147+
*/
148+
public static function intShouldGt0(int $value, string $errMsg = ''): void
149+
{
150+
if ($value < 1) {
151+
throw static::createEx($errMsg ?: 'Expected a integer value and should > 0');
152+
}
153+
}
154+
155+
/**
156+
* Positive integer. should >= 0
157+
*
158+
* @param int $value
159+
* @param string $errMsg
160+
*/
161+
public static function intShouldGte0(int $value, string $errMsg = ''): void
162+
{
163+
if ($value < 0) {
164+
throw static::createEx($errMsg ?: 'Expected a integer value and should >= 0');
165+
}
166+
}
167+
142168
/**
143169
* Positive integer. > 0
144170
*

src/OS.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
use InvalidArgumentException;
1313
use RuntimeException;
1414
use function defined;
15-
use function dirname;
1615
use function explode;
1716
use function file_get_contents;
1817
use function file_put_contents;
@@ -369,11 +368,11 @@ public static function getNullDevice(): string
369368
/**
370369
* Returns if the file descriptor is an interactive terminal or not.
371370
*
372-
* @param int $fileDescriptor
371+
* @param int|resource|mixed $fileDescriptor
373372
*
374373
* @return boolean
375374
*/
376-
public static function isInteractive(int $fileDescriptor): bool
375+
public static function isInteractive(mixed $fileDescriptor): bool
377376
{
378377
return function_exists('posix_isatty') && @posix_isatty($fileDescriptor);
379378
}

src/Std/BaseEnum.php

Lines changed: 243 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,243 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Toolkit\Stdlib\Std;
4+
5+
use BadMethodCallException;
6+
use JsonSerializable;
7+
use ReflectionClass;
8+
use UnexpectedValueException;
9+
use function array_key_exists;
10+
use function array_keys;
11+
use function array_search;
12+
use function get_class;
13+
use function in_array;
14+
15+
/**
16+
* class BaseEnum
17+
*
18+
* @author inhere
19+
* @template T
20+
* @link https://github.com/Elao/PhpEnums
21+
* @link https://github.com/myclabs/php-enum/blob/master/src/Enum.php
22+
*/
23+
abstract class BaseEnum implements JsonSerializable
24+
{
25+
26+
/**
27+
* Store existing constants in a static cache per object.
28+
*
29+
* @var array<class-string, array<string, mixed>>
30+
*/
31+
protected static array $caches = [];
32+
33+
/**
34+
* Cache of instances of the Enum class
35+
*
36+
* @var array<class-string, array<string, static>>
37+
*/
38+
protected static array $instances = [];
39+
40+
/**
41+
* @param string $name
42+
* @param array $args
43+
*
44+
* @return mixed
45+
*/
46+
public static function __callStatic(string $name, array $args): mixed
47+
{
48+
$class = static::class;
49+
50+
if (!isset(self::$instances[$class][$name])) {
51+
$array = static::toArray();
52+
if (!isset($array[$name]) && !array_key_exists($name, $array)) {
53+
throw new BadMethodCallException("No static method or enum constant '$name' in class $class");
54+
}
55+
56+
return self::$instances[$class][$name] = new static($array[$name]);
57+
}
58+
59+
return clone self::$instances[$class][$name];
60+
}
61+
62+
/**
63+
* @param mixed $variable
64+
*
65+
* @return bool
66+
*/
67+
final public function equals(mixed $variable = null): bool
68+
{
69+
return $variable instanceof self
70+
&& $this->getValue() === $variable->getValue()
71+
&& static::class === get_class($variable);
72+
}
73+
74+
/**
75+
* Returns all possible values as an array
76+
*
77+
* @psalm-suppress ImpureStaticProperty
78+
*
79+
* @return array<string, mixed> Constant name in key, constant value in value
80+
*/
81+
public static function toArray(): array
82+
{
83+
$class = static::class;
84+
85+
if (!isset(static::$caches[$class])) {
86+
/** @psalm-suppress ImpureMethodCall this reflection API usage has no side-effects here */
87+
/** @noinspection PhpUnhandledExceptionInspection */
88+
$reflection = new ReflectionClass($class);
89+
90+
/** @psalm-suppress ImpureMethodCall this reflection API usage has no side-effects here */
91+
static::$caches[$class] = $reflection->getConstants();
92+
}
93+
94+
return static::$caches[$class];
95+
}
96+
97+
/**
98+
* Returns the names (keys) of all constants in the Enum class
99+
*
100+
* @return list<string>
101+
*/
102+
public static function keys(): array
103+
{
104+
return array_keys(static::toArray());
105+
}
106+
107+
/**
108+
* Returns instances of the Enum class of all Enum constants
109+
*
110+
* @return array<string, static> Constant name in key, Enum instance in value
111+
*/
112+
public static function values(): array
113+
{
114+
$values = [];
115+
116+
/** @psalm-var T $value */
117+
foreach (static::toArray() as $key => $value) {
118+
$values[$key] = new static($value);
119+
}
120+
121+
return $values;
122+
}
123+
124+
/**
125+
* Return key for value
126+
*
127+
* @param mixed $value
128+
*
129+
* @return string|false
130+
*/
131+
public static function search(mixed $value): bool|string
132+
{
133+
return array_search($value, static::toArray(), true);
134+
}
135+
136+
/**
137+
* Check if is valid enum value
138+
*
139+
* @param T $value
140+
* @return bool
141+
*/
142+
public static function isValid(mixed $value): bool
143+
{
144+
return in_array($value, static::toArray(), true);
145+
}
146+
147+
/**
148+
* Asserts valid enum value
149+
*
150+
* @param T $value
151+
*/
152+
public static function assertValidValue(mixed $value): void
153+
{
154+
self::mustGetKeyByValue($value);
155+
}
156+
157+
/**
158+
* @param mixed $value
159+
*
160+
* @return string
161+
*/
162+
protected static function mustGetKeyByValue(mixed $value): string
163+
{
164+
if (false === ($key = static::search($value))) {
165+
throw new UnexpectedValueException("Value '$value' is not item of the enum " . static::class);
166+
}
167+
168+
return $key;
169+
}
170+
171+
/**
172+
* Enum key, the constant name
173+
*
174+
* @var string
175+
*/
176+
private string $key;
177+
178+
/**
179+
* Enum value
180+
*
181+
* @var T
182+
*/
183+
protected mixed $value;
184+
185+
/**
186+
* Creates a new value of some type
187+
*
188+
* @psalm-pure
189+
* @param mixed $value
190+
*
191+
* @psalm-param T $value
192+
* @throws UnexpectedValueException if incompatible type is given.
193+
*/
194+
public function __construct(mixed $value)
195+
{
196+
if ($value instanceof static) {
197+
/** @psalm-var T */
198+
$value = $value->getValue();
199+
}
200+
201+
/** @psalm-suppress ImplicitToStringCast assertValidValueReturningKey returns always a string but psalm has currently an issue here */
202+
$this->key = static::assertValidValueReturningKey($value);
203+
204+
/** @psalm-var T */
205+
$this->value = $value;
206+
}
207+
208+
/**
209+
* @psalm-pure
210+
* @psalm-suppress InvalidCast
211+
* @return string
212+
*/
213+
public function __toString()
214+
{
215+
return (string)$this->value;
216+
}
217+
218+
/**
219+
* Returns the enum key (i.e. the constant name).
220+
*
221+
* @return string
222+
*/
223+
public function getKey(): string
224+
{
225+
return $this->key;
226+
}
227+
228+
/**
229+
* @return mixed
230+
*/
231+
public function getValue(): mixed
232+
{
233+
return $this->value;
234+
}
235+
236+
/**
237+
* @return mixed
238+
*/
239+
public function jsonSerialize(): mixed
240+
{
241+
return $this->getValue();
242+
}
243+
}

src/Str/Traits/StringConvertTrait.php

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -41,25 +41,6 @@
4141
*/
4242
trait StringConvertTrait
4343
{
44-
/**
45-
* @param string $val
46-
*
47-
* @return bool|string
48-
*/
49-
public static function tryToBool(string $val): bool|string
50-
{
51-
// check it is a bool value.
52-
if (false !== stripos(StringHelper::TRUE_WORDS, "|$val|")) {
53-
return true;
54-
}
55-
56-
if (false !== stripos(StringHelper::FALSE_WORDS, "|$val|")) {
57-
return false;
58-
}
59-
60-
return $val;
61-
}
62-
6344
/**
6445
* @param string $str
6546
*
@@ -81,6 +62,25 @@ public static function toBool2(string $val): bool
8162
return false !== stripos(StringHelper::TRUE_WORDS, "|$val|");
8263
}
8364

65+
/**
66+
* @param string $val
67+
*
68+
* @return bool|string
69+
*/
70+
public static function tryToBool(string $val): bool|string
71+
{
72+
// check it is a bool value.
73+
if (false !== stripos(StringHelper::TRUE_WORDS, "|$val|")) {
74+
return true;
75+
}
76+
77+
if (false !== stripos(StringHelper::FALSE_WORDS, "|$val|")) {
78+
return false;
79+
}
80+
81+
return $val;
82+
}
83+
8484
/**
8585
* @param string $str
8686
*

src/Type.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
namespace Toolkit\Stdlib;
1111

12-
use Toolkit\Cli\Helper\FlagHelper;
1312
use function gettype;
1413
use function is_string;
1514

@@ -109,7 +108,7 @@ public static function getDefault(string $type): float|bool|int|array|string|nul
109108
* @param string $type
110109
* @param mixed $value
111110
*
112-
* @return array|bool|float|int|mixed|string
111+
* @return mixed
113112
*/
114113
public static function fmtValue(string $type, mixed $value): mixed
115114
{
@@ -121,7 +120,7 @@ public static function fmtValue(string $type, mixed $value): mixed
121120
case self::BOOL:
122121
case self::BOOLEAN:
123122
if (is_string($value)) {
124-
$value = FlagHelper::str2bool($value);
123+
$value = Str::toBool2($value);
125124
} else {
126125
$value = (bool)$value;
127126
}

0 commit comments

Comments
 (0)