Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b83e1b8

Browse files
committed
👔 up: update some helper and stream and object logic
- add ci test on php8.3
1 parent d3ca71c commit b83e1b8

File tree

6 files changed

+162
-35
lines changed

6 files changed

+162
-35
lines changed

‎.github/workflows/php.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jobs:
1010
strategy:
1111
fail-fast: true
1212
matrix:
13-
php: [8.1, 8.2] #
13+
php: [8.1, 8.2, 8.3]
1414
# os: [ubuntu-latest, macOS-latest] # windows-latest,
1515

1616
steps:

‎src/Helper/NumHelper.php

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

1010
namespace Toolkit\Stdlib\Helper;
1111

12+
use Throwable;
1213
use function abs;
1314
use function ceil;
1415
use function floor;
@@ -73,4 +74,20 @@ public static function roundInt(float|int $value): int
7374
{
7475
return (int)round((float)$value);
7576
}
77+
78+
/**
79+
* @param int $min
80+
* @param int $max
81+
* @param int|null $fallback
82+
*
83+
* @return int
84+
*/
85+
public static function random(int $min, int $max, int $fallback = null): int
86+
{
87+
try {
88+
return random_int($min, $max);
89+
} catch (Throwable) {
90+
return $fallback ?? $min;
91+
}
92+
}
7693
}

‎src/Obj/Singleton.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Toolkit\Stdlib\Obj;
4+
5+
use InvalidArgumentException;
6+
7+
/**
8+
* @author inhere
9+
*/
10+
final class Singleton
11+
{
12+
/**
13+
* @var array<string, object>
14+
*/
15+
private static array $objMap = [];
16+
17+
/**
18+
* @param string $id
19+
* @param object $obj
20+
*/
21+
public static function set(string $id, object $obj): void
22+
{
23+
self::$objMap[$id] = $obj;
24+
}
25+
26+
/**
27+
* get object by id
28+
*
29+
* @param string $id
30+
*
31+
* @return object|null
32+
*/
33+
public static function get(string $id): ?object
34+
{
35+
return self::$objMap[$id] ?? null;
36+
}
37+
38+
/**
39+
* must get the object, or throw an exception
40+
*
41+
* @param string $id
42+
*
43+
* @return object
44+
*/
45+
public static function must(string $id): object
46+
{
47+
return self::$objMap[$id] ?? throw new InvalidArgumentException("object not found: $id");
48+
}
49+
50+
/**
51+
* @param string $id
52+
*
53+
* @return bool
54+
*/
55+
public static function has(string $id): bool
56+
{
57+
return isset(self::$objMap[$id]);
58+
}
59+
60+
/**
61+
* @param string $id
62+
*/
63+
public static function del(string $id): void
64+
{
65+
unset(self::$objMap[$id]);
66+
}
67+
68+
/**
69+
* @return array
70+
*/
71+
public static function all(): array
72+
{
73+
return self::$objMap;
74+
}
75+
76+
/**
77+
* @return void
78+
*/
79+
public static function clear(): void
80+
{
81+
self::$objMap = [];
82+
}
83+
}

‎src/Std/Collection.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class Collection implements IteratorAggregate, ArrayAccess, Countable, JsonSeria
3939
*
4040
* @return static
4141
*/
42-
public static function new(array $data = []): self
42+
public static function new(array $data = []): static
4343
{
4444
return new static($data);
4545
}
@@ -71,7 +71,7 @@ public function __destruct()
7171
*
7272
* @return $this
7373
*/
74-
public function set(string $key, mixed $value): self
74+
public function set(string $key, mixed $value): static
7575
{
7676
$this->data[$key] = $value;
7777
return $this;
@@ -83,7 +83,7 @@ public function set(string $key, mixed $value): self
8383
*
8484
* @return $this
8585
*/
86-
public function add(string $name, $value): self
86+
public function add(string $name, $value): static
8787
{
8888
if (!$this->has($name)) {
8989
$this->set($name, $value);
@@ -170,7 +170,6 @@ public function getString(string $key, string $default = ''): string
170170
public function getArray(string $key, array $default = []): array
171171
{
172172
$data = $this->get($key);
173-
174173
return $data ? (array)$data : $default;
175174
}
176175

@@ -183,7 +182,6 @@ public function getArray(string $key, array $default = []): array
183182
public function getDataObject(string $key, array $default = []): DataObject
184183
{
185184
$data = $this->get($key);
186-
187185
return DataObject::new($data ? (array)$data : $default);
188186
}
189187

@@ -241,7 +239,7 @@ public function gets(array $keys): array
241239
*
242240
* @return static
243241
*/
244-
public function sets(array $data): self
242+
public function sets(array $data): static
245243
{
246244
foreach ($data as $key => $value) {
247245
$this->set($key, $value);
@@ -255,7 +253,7 @@ public function sets(array $data): self
255253
*
256254
* @return static
257255
*/
258-
public function reject(callable $filter): self
256+
public function reject(callable $filter): static
259257
{
260258
$data = [];
261259

@@ -275,7 +273,7 @@ public function reject(callable $filter): self
275273
*
276274
* @return static
277275
*/
278-
public function map(callable $callback): self
276+
public function map(callable $callback): static
279277
{
280278
$data = [];
281279
foreach ($this->getIterator() as $key => $value) {

‎src/Str/StringHelper.php

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -308,20 +308,13 @@ public static function renderVars(string $tplCode, array $vars, string $format =
308308
return $tplCode;
309309
}
310310

311-
$fmtVars = Arr::flattenMap($vars, Arr\ArrConst::FLAT_DOT_JOIN_INDEX);
312311
$pattern = sprintf('/%s([\w\s.-]+)%s/', preg_quote($left, '/'), preg_quote($right, '/'));
313-
314-
// convert array value to string.
315-
foreach ($vars as $name => $val) {
316-
if (is_array($val)) {
317-
$fmtVars[$name] = Arr::toStringV2($val);
318-
}
319-
}
320-
321-
return preg_replace_callback($pattern, static function (array $match) use ($fmtVars) {
322-
$var = trim($match[1]);
323-
if ($var && isset($fmtVars[$var])) {
324-
return $fmtVars[$var];
312+
return preg_replace_callback($pattern, static function (array $match) use ($vars) {
313+
if ($var = trim($match[1])) {
314+
$val = Arr::getByPath($vars, $var);
315+
if ($val !== null) {
316+
return is_array($val) ? Arr::toStringV2($val) : (string)$val;
317+
}
325318
}
326319

327320
return $match[0];

‎src/Util/Stream/DataStream.php

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ public function map(callable $mapper): static
211211
* @template U
212212
*
213213
* @param callable(T):U $mapper
214-
* @param bool $boolExpr
214+
* @param bool $boolExpr
215215
*
216216
* @return static
217217
*/
@@ -228,7 +228,7 @@ public function mapIf(callable $mapper, bool $boolExpr): static
228228
* @template U
229229
*
230230
* @param callable(T):U $mapper
231-
* @param DataStream $stream
231+
* @param DataStream $stream
232232
*
233233
* @return static
234234
*/
@@ -245,7 +245,7 @@ public function mapTo(callable $mapper, self $stream): static
245245
* Mapping values to MapStream
246246
*
247247
* @param callable(array|mixed): array{string,mixed} $mapper
248-
* @param MapStream|null $new
248+
* @param MapStream|null $new
249249
*
250250
* @return MapStream
251251
*/
@@ -264,7 +264,7 @@ public function mapToMap(callable $mapper, MapStream $new = null): MapStream
264264
* Mapping values to IntStream
265265
*
266266
* @param callable(T):int $mapper
267-
* @param IntStream|null $new
267+
* @param IntStream|null $new
268268
*
269269
* @return IntStream
270270
*/
@@ -282,7 +282,7 @@ public function mapToInt(callable $mapper, IntStream $new = null): IntStream
282282
* Mapping values to StringStream
283283
*
284284
* @param callable(T):string $mapper
285-
* @param StringStream|null $new
285+
* @param StringStream|null $new
286286
*
287287
* @return StringStream
288288
*/
@@ -528,7 +528,7 @@ public function forEach(callable $handler): void
528528

529529
/**
530530
* @param callable(array|mixed, int|string): array $func
531-
* @param array $arr
531+
* @param array $arr
532532
*
533533
* @return array
534534
*/
@@ -544,7 +544,7 @@ public function eachToArray(callable $func, array $arr = []): array
544544
* Each item to map
545545
*
546546
* @param callable(array|mixed): array{string, mixed} $func
547-
* @param array $map
547+
* @param array $map
548548
*
549549
* @return array<string, mixed> return the map key and value
550550
*/
@@ -559,15 +559,51 @@ public function eachToMap(callable $func, array $map = []): array
559559
}
560560

561561
/**
562-
* @param callable $handler
563-
* @param ...$args
562+
* Collect data items to list
564563
*
565-
* @return mixed
564+
* @param callable(mixed): mixed $func
565+
* @param array $list
566+
*
567+
* @return array return an array: [item, ...]
568+
*/
569+
public function collectToList(callable $func, array $list = []): array
570+
{
571+
foreach ($this as $item) {
572+
$list[] = $func($item);
573+
}
574+
return $list;
575+
}
576+
577+
/**
578+
* Collect data items to map
579+
*
580+
* @param callable $func
581+
* @param array $map
582+
*
583+
* @return array<string, mixed> return a map: [key => item]
584+
*/
585+
public function collectToMap(callable $func, array $map = []): array
586+
{
587+
return $this->eachToMap($func, $map);
588+
}
589+
590+
/**
591+
* Group data by key
592+
*
593+
* @param callable $getKeyFn
594+
*
595+
* @return array
566596
*/
567-
public function collect(callable $handler, ...$args): mixed
597+
public function groupBy(callable $getKeyFn): array
568598
{
569-
// TODO
570-
return null;
599+
$map = [];
600+
foreach ($this as $item) {
601+
$key = $getKeyFn($item);
602+
// collect to map
603+
$map[$key][] = $item;
604+
}
605+
606+
return $map;
571607
}
572608

573609
/**

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /