Skip to content

Commit 1ea3da4

Browse files
committed
up: update some base util class
1 parent e3869ac commit 1ea3da4

File tree

9 files changed

+412
-105
lines changed

9 files changed

+412
-105
lines changed

README.md

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,19 @@
55
[![Latest Stable Version](http://img.shields.io/packagist/v/toolkit/stdlib.svg)](https://packagist.org/packages/toolkit/stdlib)
66
[![Github Actions Status](https://github.com/php-toolkit/stdlib/workflows/Unit-Tests/badge.svg)](https://github.com/php-toolkit/stdlib/actions)
77

8-
Some useful basic tools for php.
9-
10-
Contains:
11-
12-
- array helper
13-
- object helper
14-
- string helper
15-
- common php helper
16-
- OS env information
17-
- dotenv load `.env`
18-
- simple autoloader
19-
- simple object container
8+
Stdlib - useful basic tools for php develop.
9+
10+
**Contains**:
11+
12+
- array, string, number, object helper
13+
- common php, OS env information
14+
15+
**More Utils**
16+
17+
- Dotenv load `.env`
18+
- Simple autoloader
19+
- `ObjectBox` simple object container
20+
- `Optional` like java `java.util.Optional`
2021
- and more ...
2122

2223
## Install
@@ -27,10 +28,16 @@ composer require toolkit/stdlib
2728

2829
## Usage
2930

30-
### Object Box
31+
### String helper
32+
33+
### Object box
34+
35+
`ObjectBox` - Simple object container.
3136

3237
```php
33-
$box = \Toolkit\Stdlib\Obj\ObjectBox::global();
38+
use Toolkit\Stdlib\Obj\ObjectBox;
39+
40+
$box = ObjectBox::global();
3441

3542
// set
3643
$box->set('router', function () {
@@ -42,13 +49,38 @@ $box->set('renderer', [
4249
'tplDir' => 'path/to/dir',
4350
]);
4451

52+
// with options for create
53+
$box->set('somObj', [
54+
'class' => MyObject::class,
55+
'__opt' => [
56+
// will always create new object.
57+
'objType' => ObjectBox::TYPE_PROTOTYPE,
58+
],
59+
]);
60+
4561
// get
4662
/** @var MyRouter $router */
4763
$router = $box->get('router');
4864
/** @var MyRenderer $renderer */
4965
$renderer = $box->get('renderer');
5066
```
5167

68+
## Util classes
69+
70+
### Optional
71+
72+
```php
73+
74+
```
75+
76+
### DataStream
77+
78+
```php
79+
use Toolkit\Stdlib\Util\Stream\DataStream;
80+
81+
$stream = DataStream::of($data);
82+
```
83+
5284
## License
5385

5486
[MIT](LICENSE)

src/Arr/ArrBuffer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ final class ArrBuffer
1919
/**
2020
* @var string[]
2121
*/
22-
private $body = [];
22+
private array $body = [];
2323

2424
/**
2525
* @var string
2626
*/
27-
private $delimiter = ''; // '/' ':'
27+
private string $delimiter = ''; // '/' ':'
2828

2929
/**
3030
* @param string $first

src/Helper/Assert.php

Lines changed: 256 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,256 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Toolkit\Stdlib\Helper;
4+
5+
use InvalidArgumentException;
6+
use RuntimeException;
7+
use function in_array;
8+
9+
/**
10+
* class Assert
11+
*
12+
* @author inhere
13+
*/
14+
class Assert
15+
{
16+
/**
17+
* @param mixed $value
18+
* @param string $errMsg
19+
*
20+
* @return mixed
21+
*/
22+
public static function notEmpty(mixed $value, string $errMsg = ''): mixed
23+
{
24+
if (empty($value)) {
25+
throw static::createEx($errMsg ?: 'Expected a non-empty value');
26+
}
27+
28+
return $value;
29+
}
30+
31+
/**
32+
* @param mixed $value
33+
* @param string $errMsg
34+
*
35+
* @return mixed
36+
*/
37+
public static function notNull(mixed $value, string $errMsg = ''): mixed
38+
{
39+
if (null === $value) {
40+
throw static::createEx($errMsg ?: 'Expected a non-null value');
41+
}
42+
43+
return $value;
44+
}
45+
46+
/**
47+
* @param string $value
48+
* @param string $errMsg
49+
*
50+
* @return string
51+
*/
52+
public static function notBlank(string $value, string $errMsg = ''): string
53+
{
54+
if ('' === $value) {
55+
throw static::createEx($errMsg ?: 'Expected a non-blank string value');
56+
}
57+
58+
return $value;
59+
}
60+
61+
/**
62+
* Two values should be equal, an exception will be thrown when they are not equal.
63+
*
64+
* @param mixed $value1
65+
* @param mixed $value2
66+
* @param string $errMsg
67+
*
68+
* @return bool
69+
*/
70+
public static function equals(mixed $value1, mixed $value2, string $errMsg = ''): bool
71+
{
72+
if ($value1 !== $value2) {
73+
throw static::createEx($errMsg ?: "The $value1 should equals to $value2");
74+
}
75+
76+
return true;
77+
}
78+
79+
/**
80+
* Two values cannot be equal, an exception will be thrown when they are equal.
81+
*
82+
* @param mixed $value1
83+
* @param mixed $value2
84+
* @param string $errMsg
85+
*
86+
* @return bool
87+
*/
88+
public static function notEquals(mixed $value1, mixed $value2, string $errMsg = ''): bool
89+
{
90+
if ($value1 === $value2) {
91+
throw static::createEx($errMsg ?: "The $value1 should not equals to $value2");
92+
}
93+
94+
return true;
95+
}
96+
97+
/**
98+
* @param bool $value
99+
* @param string $errMsg
100+
*
101+
* @return bool
102+
*/
103+
public static function isTrue(bool $value, string $errMsg = ''): bool
104+
{
105+
if (false === $value) {
106+
throw static::createEx($errMsg ?: 'Expected a true value');
107+
}
108+
109+
return $value;
110+
}
111+
112+
/**
113+
* @param bool $value
114+
* @param string $errMsg
115+
*
116+
* @return bool
117+
*/
118+
public static function isFalse(bool $value, string $errMsg = ''): bool
119+
{
120+
if (true === $value) {
121+
throw static::createEx($errMsg ?: 'Expected a false value');
122+
}
123+
124+
return $value;
125+
}
126+
127+
/**
128+
* @param mixed $needle
129+
* @param array $haystack
130+
* @param string $errMsg
131+
*
132+
* @return mixed
133+
*/
134+
public static function inArray(mixed $needle, array $haystack, string $errMsg = ''): mixed
135+
{
136+
if (!in_array($needle, $haystack, true)) {
137+
throw static::createEx($errMsg ?: 'Expected a value in array');
138+
}
139+
140+
return $needle;
141+
}
142+
143+
/**
144+
* Value should != 0
145+
*
146+
* @param int $value
147+
* @param string $errMsg
148+
*
149+
* @return int
150+
*/
151+
public static function notZero(int $value, string $errMsg = ''): int
152+
{
153+
if ($value === 0) {
154+
throw static::createEx($errMsg ?: 'Expected a non-zero integer value');
155+
}
156+
157+
return $value;
158+
}
159+
160+
/**
161+
* Natural number. >= 0
162+
*
163+
* @param int $value
164+
* @param string $errMsg
165+
*
166+
* @return int
167+
*/
168+
public static function naturalInt(int $value, string $errMsg = ''): int
169+
{
170+
if ($value < 0) {
171+
throw static::createEx($errMsg ?: 'Expected a natural number value(>=0)');
172+
}
173+
174+
return $value;
175+
}
176+
177+
/**
178+
* Positive integer. > 0
179+
*
180+
* @param int $value
181+
* @param string $errMsg
182+
*
183+
* @return int
184+
*/
185+
public static function positiveInt(int $value, string $errMsg = ''): int
186+
{
187+
if ($value < 1) {
188+
throw static::createEx($errMsg ?: 'Expected a positive integer value(>0)');
189+
}
190+
191+
return $value;
192+
}
193+
194+
/**
195+
* @param array $data
196+
* @param string $key
197+
* @param string $errMsg
198+
*
199+
* @return mixed
200+
*/
201+
public static function arrayHasKey(array $data, string $key, string $errMsg = ''): mixed
202+
{
203+
if (!isset($data[$key])) {
204+
throw static::createEx($errMsg ?: "Array data must contains key '$key'");
205+
}
206+
207+
return $data[$key];
208+
}
209+
210+
/**
211+
* @param array $data
212+
* @param array $keys
213+
* @param string $errMsg
214+
*
215+
* @return array
216+
*/
217+
public static function arrayHasKeys(array $data, array $keys, string $errMsg = ''): array
218+
{
219+
$values = [];
220+
foreach ($keys as $key) {
221+
if (!isset($data[$key])) {
222+
throw static::createEx($errMsg ?: "Array data must contains key '$key'");
223+
}
224+
225+
$values[$key] = $data[$key];
226+
}
227+
228+
return $values;
229+
}
230+
231+
/**
232+
* @param array $data
233+
* @param string $key
234+
* @param string $errMsg
235+
*
236+
* @return mixed
237+
*/
238+
public static function arrayHasNoEmptyKey(array $data, string $key, string $errMsg = ''): mixed
239+
{
240+
if (!isset($data[$key]) || empty($data[$key])) {
241+
throw static::createEx($errMsg ?: "Data must contains key '$key' and value non-empty");
242+
}
243+
244+
return $data[$key];
245+
}
246+
247+
/**
248+
* @param string $errMsg
249+
*
250+
* @return RuntimeException|InvalidArgumentException
251+
*/
252+
public static function createEx(string $errMsg): RuntimeException
253+
{
254+
return new InvalidArgumentException($errMsg);
255+
}
256+
}

src/Obj/AbstractMap.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ abstract class AbstractMap implements JsonSerializable
2424
*
2525
* @return static
2626
*/
27-
public static function new(array $data = [])
27+
public static function new(array $data = []): static
2828
{
2929
return new static($data);
3030
}

0 commit comments

Comments
 (0)