Skip to content

Commit 0778203

Browse files
committed
up: update some class desc and redme
1 parent 016a936 commit 0778203

File tree

7 files changed

+139
-49
lines changed

7 files changed

+139
-49
lines changed

README.md

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,25 @@ $renderer = $box->get('renderer');
8383

8484
## Util classes
8585

86+
### AutoLoader
87+
88+
`AutoLoader` - an simple psr4 loader, can use for tests.
89+
90+
```php
91+
AutoLoader::addFiles([
92+
// alone files
93+
]);
94+
95+
$loader = AutoLoader::getLoader();
96+
$loader->addPsr4Map([
97+
'namespace' => 'path'
98+
]);
99+
100+
$loader->addClassMap([
101+
'name' => 'class file'
102+
]);
103+
```
104+
86105
### Optional
87106

88107
Not use Optional:
@@ -109,12 +128,48 @@ $username = Optional::ofNullable($userModel)->map(function ($userModel) {
109128
})->orElse('unknown');
110129
```
111130

112-
### DataStream
131+
### PhpDotEnv
132+
133+
`PhpDotEnv` - a simple dont env file loader.
134+
135+
The env config file `.env` (must is 'ini' format):
136+
137+
```ini
138+
APP_ENV=dev
139+
DEBUG=true
140+
; ... ...
141+
```
142+
143+
Usage:
113144

114145
```php
115-
use Toolkit\Stdlib\Util\Stream\DataStream;
146+
PhpDotEnv::load(__DIR__, '.env');
116147

117-
$stream = DataStream::of($data);
148+
env('DEBUG', false);
149+
env('APP_ENV', 'prod');
150+
```
151+
152+
### Stream
153+
154+
```php
155+
use Toolkit\Stdlib\Util\Stream\DataStream;
156+
use Toolkit\Stdlib\Util\Stream\ListStream;
157+
158+
$userList = ListStream::of($userModels)
159+
->filter(function ($userModel) {
160+
// only need age > 20
161+
return $userModel->age > 20;
162+
})
163+
->map(function ($userModel) {
164+
// only need field: age, name
165+
return [
166+
'age' => $userModel->age,
167+
'name' => $userModel->name,
168+
];
169+
})
170+
->toArray();
171+
172+
vdump($userList);
118173
```
119174

120175
## License

src/Util/AutoLoader.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,12 @@
1616
use function spl_autoload_unregister;
1717
use function str_replace;
1818
use function strlen;
19-
use function strpos;
2019
use function substr;
2120
use const DIRECTORY_SEPARATOR;
2221

2322
/**
2423
* Class AutoLoader - an simple class loader
2524
*
26-
* @package Toolkit\Stdlib\Util
27-
*
2825
* ```php
2926
* AutoLoader::addFiles([
3027
* // file
@@ -37,44 +34,48 @@
3734
* 'name' => 'file'
3835
* ]);
3936
* ```
37+
*
38+
* @package Toolkit\Stdlib\Util
4039
*/
4140
class AutoLoader
4241
{
4342
/**
44-
* @var self
43+
* @var self|null
4544
*/
46-
private static $loader;
45+
private static ?AutoLoader $loader = null;
4746

4847
/**
4948
* @var array
5049
*/
51-
private static $files = [];
50+
private static array $files = [];
5251

5352
/**
54-
* @var array
5553
* array (
5654
* 'prefix' => 'dir path'
5755
* )
56+
*
57+
* @var array
5858
*/
59-
private $psr0Map = [];
59+
private array $psr0Map = [];
6060

6161
/**
62-
* @var array
6362
* array (
6463
* 'prefix' => 'dir path'
6564
* )
65+
*
66+
* @var array
6667
*/
67-
private $psr4Map = [];
68+
private array $psr4Map = [];
6869

6970
/**
70-
* @var array
71+
* @var array<string, string>
7172
*/
72-
private $classMap = [];
73+
private array $classMap = [];
7374

7475
/**
7576
* @var array
7677
*/
77-
private $missingClasses = [];
78+
private array $missingClasses = [];
7879

7980
/**
8081
* @param array $files
@@ -92,7 +93,6 @@ public static function getLoader(array $files = []): self
9293
}
9394

9495
self::$loader = $loader = new self();
95-
9696
$loader->register(true);
9797

9898
foreach (self::$files as $fileIdentifier => $file) {
@@ -275,11 +275,11 @@ public function loadClass(string $class): ?bool
275275
*
276276
* @return string|false The path if found, false otherwise
277277
*/
278-
public function findFile(string $class)
278+
public function findFile(string $class): bool|string
279279
{
280280
// work around for PHP 5.3.0 - 5.3.2 https://bugs.php.net/50731
281281
if ('\\' === $class[0]) {
282-
$class = (string)substr($class, 1);
282+
$class = substr($class, 1);
283283
}
284284

285285
// class map lookup

src/Util/PhpDotEnv.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@
2828
/**
2929
* Class PhpDotEnv - local env read
3030
*
31-
* @package Toolkit\Stdlib\Util
32-
*
33-
* in local config file `.env` (must is 'ini' format):
31+
* The env config file `.env` (must is 'ini' format):
3432
*
3533
* ```ini
3634
* APP_ENV=dev
@@ -45,6 +43,8 @@
4543
* env('DEBUG', false);
4644
* env('APP_ENV', 'pdt');
4745
* ```
46+
*
47+
* @package Toolkit\Stdlib\Util
4848
*/
4949
class PhpDotEnv
5050
{

src/Util/Stream/DataStream.php

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -238,30 +238,55 @@ public function mapTo(callable $mapper, self $stream): static
238238
}
239239

240240
/**
241-
* @param callable(array|mixed): array{string,mixed} $func
241+
* Mapping values to MapStream
242+
*
243+
* @param callable(array|mixed): array{string,mixed} $mapper
242244
* @param MapStream|null $new
243245
*
244246
* @return MapStream
245247
*/
246-
public function mapToMap(callable $func, MapStream $new = null): MapStream
248+
public function mapToMap(callable $mapper, MapStream $new = null): MapStream
247249
{
248250
$new = $new ?: new MapStream();
249251
foreach ($this as $item) {
250-
[$key, $val] = $func($item);
252+
[$key, $val] = $mapper($item);
251253
$new->offsetSet($key, $val);
252254
}
253255

254256
return $new;
255257
}
256258

257259
/**
260+
* Mapping values to IntStream
261+
*
262+
* @param callable(T):int $mapper
263+
* @param IntStream|null $new
264+
*
265+
* @return IntStream
266+
*/
267+
public function mapToInt(callable $mapper, IntStream $new = null): IntStream
268+
{
269+
$new = $new ?: new IntStream;
270+
foreach ($this as $val) {
271+
$new->append($mapper($val));
272+
}
273+
274+
return $new;
275+
}
276+
277+
/**
278+
* Mapping values to StringStream
279+
*
280+
* @param callable(T):string $mapper
281+
* @param StringStream|null $new
282+
*
258283
* @return StringStream
259284
*/
260-
public function mapToString(): StringStream
285+
public function mapToString(callable $mapper, StringStream $new = null): StringStream
261286
{
262-
$new = new StringStream;
287+
$new = $new ?: new StringStream;
263288
foreach ($this as $val) {
264-
$new->append($val);
289+
$new->append($mapper($val));
265290
}
266291

267292
return $new;
@@ -534,6 +559,12 @@ public function eachToMap(callable $func, array $map = []): array
534559
return $map;
535560
}
536561

562+
/**
563+
* @param callable $handler
564+
* @param ...$args
565+
*
566+
* @return mixed
567+
*/
537568
public function collect(callable $handler, ...$args): mixed
538569
{
539570
// TODO

src/Util/Stream/IntStream.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,4 @@ public function append(mixed $value): static
1919
parent::append((int)$value);
2020
return $this;
2121
}
22-
2322
}

src/Util/Stream/ListStream.php

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,19 @@
22

33
namespace Toolkit\Stdlib\Util\Stream;
44

5-
use function implode;
6-
75
/**
86
* class ListStream
7+
*
8+
* @author inhere
99
*/
1010
class ListStream extends DataStream
1111
{
1212
/**
13-
* @param string $sep
14-
*
15-
* @return string
16-
*/
17-
public function join(string $sep = ','): string
18-
{
19-
return $this->implode($sep);
20-
}
21-
22-
/**
23-
* @param string $sep
13+
* @param mixed $value
2414
*
25-
* @return string
15+
* @return $this
2616
*/
27-
public function implode(string $sep = ','): string
28-
{
29-
return implode($sep, $this->getArrayCopy());
30-
}
31-
32-
public function append($value): static
17+
public function append(mixed $value): static
3318
{
3419
parent::append($value);
3520
return $this;

src/Util/Stream/StringStream.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,24 @@ public function append(mixed $value): static
1919
parent::append((string)$value);
2020
return $this;
2121
}
22+
23+
/**
24+
* @param string $sep
25+
*
26+
* @return string
27+
*/
28+
public function join(string $sep = ','): string
29+
{
30+
return $this->implode($sep);
31+
}
32+
33+
/**
34+
* @param string $sep
35+
*
36+
* @return string
37+
*/
38+
public function implode(string $sep = ','): string
39+
{
40+
return implode($sep, $this->getArrayCopy());
41+
}
2242
}

0 commit comments

Comments
 (0)