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 99803f7

Browse files
committed
up: update some helper methods and readme
1 parent 088ce28 commit 99803f7

File tree

10 files changed

+229
-19
lines changed

10 files changed

+229
-19
lines changed

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[![License](https://img.shields.io/github/license/php-toolkit/stdlib)](LICENSE)
44
[![Php Version](https://img.shields.io/badge/php-%3E8.0-brightgreen.svg?maxAge=2592000)](https://packagist.org/packages/toolkit/stdlib)
55
[![Latest Stable Version](http://img.shields.io/packagist/v/toolkit/stdlib.svg)](https://packagist.org/packages/toolkit/stdlib)
6-
[![Github Actions Status](https://github.com/php-toolkit/stdlib/workflows/Unit-Tests/badge.svg)](https://github.com/php-toolkit/stdlib/actions)
6+
[![Unit Tests](https://github.com/php-toolkit/stdlib/actions/workflows/php.yml/badge.svg)](https://github.com/php-toolkit/stdlib/actions)
77
[![Docs on pages](https://img.shields.io/badge/DocsOn-Pages-brightgreen.svg?maxAge=2592000)](https://php-toolkit.github.io/stdlib/)
88

99
🧰 Stdlib - Useful basic tools library for PHP development.

‎README.zh-CN.md‎

Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
# StdLib
2+
3+
[![License](https://img.shields.io/github/license/php-toolkit/stdlib)](LICENSE)
4+
[![Php Version](https://img.shields.io/badge/php-%3E8.0-brightgreen.svg?maxAge=2592000)](https://packagist.org/packages/toolkit/stdlib)
5+
[![Latest Stable Version](http://img.shields.io/packagist/v/toolkit/stdlib.svg)](https://packagist.org/packages/toolkit/stdlib)
6+
[![Unit Tests](https://github.com/php-toolkit/stdlib/actions/workflows/php.yml/badge.svg)](https://github.com/php-toolkit/stdlib/actions)
7+
[![Docs on pages](https://img.shields.io/badge/DocsOn-Pages-brightgreen.svg?maxAge=2592000)](https://php-toolkit.github.io/stdlib/)
8+
9+
🧰 Stdlib - Useful basic tools library for PHP development.
10+
11+
**Contains**:
12+
13+
- array, string, number, object helper
14+
- common php, OS env information
15+
16+
**More Utils**
17+
18+
- `PhpDotEnv` Dotenv(`.env`) file load
19+
- `AutoLoader` Simple autoloader
20+
- `ObjectBox` simple object container
21+
- `Optional` like java `java.util.Optional`
22+
- and more ...
23+
24+
## Install
25+
26+
```bash
27+
composer require toolkit/stdlib
28+
```
29+
30+
## Strings
31+
32+
### StrBuffer
33+
34+
```php
35+
use Toolkit\Stdlib\Str\StrBuffer;
36+
37+
$buf = StrBuffer::new("c");
38+
$buf->prepend('ab')
39+
$buf->append('de')
40+
41+
$str = (string)$buf; // "abcde"
42+
$str = $buf->toString(); // "abcde"
43+
// get and clean.
44+
$str = $buf->fetch(); // "abcde"
45+
$str = $buf->join(','); // "ab,c,de"
46+
```
47+
48+
## Objects
49+
50+
### Object box
51+
52+
`ObjectBox` - Simple object container.
53+
54+
```php
55+
use Toolkit\Stdlib\Obj\ObjectBox;
56+
57+
$box = ObjectBox::global();
58+
59+
// set
60+
$box->set('router', function () {
61+
return new MyRouter();
62+
});
63+
64+
$box->set('renderer', [
65+
'class' => MyRenderer::class,
66+
'tplDir' => 'path/to/dir',
67+
]);
68+
69+
// with options for create
70+
$box->set('somObj', [
71+
'class' => MyObject::class,
72+
'__opt' => [
73+
// will always create new object.
74+
'objType' => ObjectBox::TYPE_PROTOTYPE,
75+
],
76+
]);
77+
78+
// get
79+
/** @var MyRouter $router */
80+
$router = $box->get('router');
81+
/** @var MyRenderer $renderer */
82+
$renderer = $box->get('renderer');
83+
```
84+
85+
## Util classes
86+
87+
### AutoLoader
88+
89+
`AutoLoader` - an simple psr4 loader, can use for tests.
90+
91+
```php
92+
AutoLoader::addFiles([
93+
// alone files
94+
]);
95+
96+
$loader = AutoLoader::getLoader();
97+
$loader->addPsr4Map([
98+
'namespace' => 'path'
99+
]);
100+
101+
$loader->addClassMap([
102+
'name' => 'class file'
103+
]);
104+
```
105+
106+
### Optional
107+
108+
It aims to eliminate excessive if judgments.
109+
110+
Not use Optional:
111+
112+
```php
113+
use Toolkit\Stdlib\Util\Optional;
114+
115+
$userModel = UserModel::findOne(23);
116+
117+
if ($userModel) {
118+
$username = $userModel->name;
119+
} else {
120+
$username = 'unknown';
121+
}
122+
```
123+
124+
Use Optional:
125+
126+
```php
127+
use Toolkit\Stdlib\Util\Optional;
128+
129+
$username = Optional::ofNullable($userModel)
130+
->map(function ($userModel) {
131+
return $userModel->name;
132+
})->orElse('unknown');
133+
```
134+
135+
Use arrow syntax:
136+
137+
```php
138+
use Toolkit\Stdlib\Util\Optional;
139+
140+
$username = Optional::ofNullable($userModel)
141+
->map(fn($userModel) => $userModel->name)
142+
->orElse('unknown');
143+
```
144+
145+
### Php DotEnv
146+
147+
`PhpDotEnv` - a simple dont env file loader.
148+
149+
The env config file `.env` (must is 'ini' format):
150+
151+
```ini
152+
APP_ENV=dev
153+
DEBUG=true
154+
; ... ...
155+
```
156+
157+
Usage:
158+
159+
```php
160+
PhpDotEnv::load(__DIR__, '.env');
161+
162+
env('DEBUG', false);
163+
env('APP_ENV', 'prod');
164+
```
165+
166+
### Stream
167+
168+
```php
169+
use Toolkit\Stdlib\Util\Stream\DataStream;
170+
use Toolkit\Stdlib\Util\Stream\ListStream;
171+
172+
$userList = ListStream::of($userModels)
173+
->filter(fn($userModel) => $userModel->age > 20) // only need age > 20
174+
->map(function ($userModel) {
175+
// only need field: age, name
176+
return [
177+
'age' => $userModel->age,
178+
'name' => $userModel->name,
179+
];
180+
})
181+
->toArray();
182+
183+
vdump($userList);
184+
```
185+
186+
### PipeFilters
187+
188+
```php
189+
$pf = PipeFilters::newWithDefaultFilters();
190+
191+
$val = $pf->applyString('inhere', 'upper'); // 'INHERE'
192+
$val = $pf->applyString('inhere', 'upper|substr:0,3'); // 'INH'
193+
```
194+
195+
## License
196+
197+
[MIT](LICENSE)

‎src/Helper/Assert.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ public static function isFile(string $path, string $errMsg = ''): void
320320
*
321321
* @return void
322322
*/
323-
public static function isResource($res, string $errMsg = ''): void
323+
public static function isResource(mixed$res, string $errMsg = ''): void
324324
{
325325
if (!is_resource($res)) {
326326
throw static::createEx($errMsg ?: 'Excepted an resource');

‎src/Helper/IntHelper.php‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111

1212
use function is_array;
1313
use function is_int;
14+
use function max;
15+
use function min;
16+
use function pack;
1417

1518
/**
1619
* Class IntHelper
@@ -27,7 +30,7 @@ class IntHelper
2730
*/
2831
public static function getMax(int $val1, int $val2): int
2932
{
30-
return $val1 > $val2 ? $val1 : $val2;
33+
return max($val1, $val2);
3134
}
3235

3336
/**
@@ -38,7 +41,7 @@ public static function getMax(int $val1, int $val2): int
3841
*/
3942
public static function getMin(int $val1, int $val2): int
4043
{
41-
return $val1 < $val2 ? $val1 : $val2;
44+
return min($val1, $val2);
4245
}
4346

4447
// ----- http://cn2.php.net/manual/zh/function.pack.php#119402

‎src/OS.php‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,6 @@ public static function name(): string
169169
if (defined('PHP_OS_FAMILY')) {
170170
return PHP_OS_FAMILY;
171171
}
172-
173172
return PHP_OS;
174173
}
175174

@@ -363,7 +362,6 @@ public static function getNullDevice(): string
363362
if (self::isUnix()) {
364363
return '/dev/null';
365364
}
366-
367365
return 'NUL';
368366
}
369367

@@ -420,6 +418,6 @@ public static function writeFile(string $filepath, string $contents, int $flags
420418
*/
421419
public static function mkdir(string $path, int $mode = 0775, bool $recursive = true): bool
422420
{
423-
return (is_dir($path) || !(!@mkdir($path, $mode, $recursive) && !is_dir($path))) && is_writable($path);
421+
return (is_dir($path) || !(!mkdir($path, $mode, $recursive) && !is_dir($path))) && is_writable($path);
424422
}
425423
}

‎src/Obj/Traits/ObjectPoolTrait.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ trait ObjectPoolTrait
3232
/**
3333
* @param string $class
3434
*
35-
* @return mixed
35+
* @return object of $class
3636
*/
37-
public static function get(string $class): mixed
37+
public static function get(string $class): object
3838
{
3939
$stack = self::getStack($class);
4040

‎src/Util/Contract/PipelineInterface.php‎

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

1010
namespace Toolkit\Stdlib\Util\Contract;
1111

12+
use Closure;
13+
1214
/**
1315
* Interface PipelineInterface
1416
*
@@ -19,11 +21,11 @@ interface PipelineInterface
1921
/**
2022
* Adds stage to the pipeline
2123
*
22-
* @param callable $stage
24+
* @param Closure $stage
2325
*
2426
* @return $this
2527
*/
26-
public function add(callable $stage): PipelineInterface;
28+
public function add(Closure $stage): PipelineInterface;
2729

2830
/**
2931
* Runs pipeline with initial value

‎src/Util/Pipeline.php‎

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

1010
namespace Toolkit\Stdlib\Util;
1111

12+
use Closure;
1213
use SplObjectStorage;
1314
use Toolkit\Stdlib\Util\Contract\PipelineInterface;
1415
use function is_callable;
@@ -34,7 +35,7 @@ public function __construct()
3435
/**
3536
* {@inheritdoc}
3637
*/
37-
public function add(callable $stage): PipelineInterface
38+
public function add(Closure $stage): PipelineInterface
3839
{
3940
if ($stage instanceof $this) {
4041
$stage->add(fn ($payload) => $this->invokeStage($payload));
@@ -50,7 +51,6 @@ public function add(callable $stage): PipelineInterface
5051
public function run(mixed $payload): mixed
5152
{
5253
$this->stages->rewind();
53-
5454
return $this->invokeStage($payload);
5555
}
5656

‎src/Util/Token.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424

2525
/**
2626
* Usage:
27+
*
2728
* $user = $db->query(['name' => $_POST['name'] ]);
29+
*
2830
* 1.
2931
* gen:
3032
* $password = Token::gen('123456');

‎test/_navbar.md‎

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
1-
* PhpPkg
2-
* [EasyTpl](https://phppkg.github.io/easytpl/ "template engine")
3-
* [Validate](https://inhere.github.io/php-validate/ "data validate engine")
4-
* Toolkit
5-
* [PFlag](https://php-toolkit.github.io/pflag/ "console option and argument parse")
6-
* [Stdlib](https://php-toolkit.github.io/stdlib/ "Useful basic tools library for PHP development.")
1+
* [English](/README.md)
2+
* [中文说明](/README.zh-CN.md)
3+
* **[PHPPkg](https://github.com/phppkg)**
4+
* [Config](https://phppkg.github.io/config/ "🗂 Config load, management, merge, get, set and more.")
5+
* [EasyTpl](https://phppkg.github.io/easytpl/ "⚡️ Simple and fastly template engine for PHP")
6+
* [Http-client](https://phppkg.github.io/http-client/ "An easy-to-use HTTP client library for PHP")
7+
* [PhpGit](https://phppkg.github.io/phpgit/ "A Git wrapper library for PHP")
8+
* [Ini](https://phppkg.github.io/ini/ "💪 An enhanced INI format parser written in PHP")
9+
* [Jenkins-client](https://phppkg.github.io/jenkins-client/ "Designed to interact with Jenkins CI using its API")
10+
* [Console](https://inhere.github.io/php-console/ "🖥 PHP CLI library, provide console options, arguments parse")
11+
* [Validate](https://inhere.github.io/php-validate/ "php data validate engine")
12+
* **[Toolkit](https://github.com/php-toolkit)**
13+
* [PFlag](https://php-toolkit.github.io/pflag/ "console option and argument parse")
14+
* [Stdlib](https://php-toolkit.github.io/stdlib/ "Useful basic tools library for PHP development.")

0 commit comments

Comments
(0)

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