Skip to content

Commit e28dbae

Browse files
committed
up: add new method for convert val to bool
1 parent 0e85f43 commit e28dbae

File tree

6 files changed

+147
-12
lines changed

6 files changed

+147
-12
lines changed

src/Helper/DataHelper.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Toolkit\Stdlib\Helper;
4+
5+
use function filter_var;
6+
use function is_scalar;
7+
use const FILTER_NULL_ON_FAILURE;
8+
use const FILTER_VALIDATE_BOOLEAN;
9+
10+
/**
11+
* Class DataHelper
12+
*
13+
* @package Toolkit\Stdlib\Helper
14+
*/
15+
class DataHelper
16+
{
17+
/**
18+
* 布尔值验证,转换成字符串后是下列的一个,就认为他是个bool值
19+
* - "1"、"true"、"on" 和 "yes" (equal TRUE)
20+
* - "0"、"false"、"off"、"no" 和 ""(equal FALSE)
21+
* 注意: NULL 不是标量类型
22+
*
23+
* @param int|string $val
24+
* @param bool|mixed $nullAsFalse
25+
*
26+
* @return bool
27+
*/
28+
public static function boolean($val, $nullAsFalse = false): bool
29+
{
30+
if ($val !== null && !is_scalar($val)) {
31+
return (bool)$val;
32+
}
33+
34+
return filter_var($val, FILTER_VALIDATE_BOOLEAN, [
35+
'flags' => $nullAsFalse ? FILTER_NULL_ON_FAILURE : 0
36+
]);
37+
}
38+
39+
/**
40+
* @param mixed $val
41+
* @param false|mixed $nullAsFalse
42+
*
43+
* @return bool
44+
*/
45+
public static function toBool($val, $nullAsFalse = false): bool
46+
{
47+
return self::boolean($val, $nullAsFalse);
48+
}
49+
}

src/Str/StrObject.php

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,21 @@ public function setString(string $string): void
9292
*/
9393
public function trim(string $chars = " \t\n\r\0\x0B"): self
9494
{
95-
$this->string = trim($this->string, $chars);
95+
if ($this->string) {
96+
$this->string = trim($this->string, $chars);
97+
}
98+
9699
return $this;
97100
}
98101

102+
/**
103+
* @return bool
104+
*/
105+
public function isEmpty(): bool
106+
{
107+
return $this->string !== '';
108+
}
109+
99110
/**
100111
* @return int
101112
*/
@@ -146,6 +157,14 @@ public function hasSuffix(string $suffix): bool
146157

147158
// ------------------ convert ------------------
148159

160+
/**
161+
* @return int
162+
*/
163+
public function toInt(): int
164+
{
165+
return (int)$this->string;
166+
}
167+
149168
/**
150169
* @return int
151170
*/
@@ -157,19 +176,51 @@ public function getInt(): int
157176
/**
158177
* @return int[]
159178
*/
160-
public function getInts(string $sep = ','): array
179+
public function toInts(string $sep = ','): array
161180
{
162181
return Str::str2ints($this->string, $sep);
163182
}
164183

184+
/**
185+
* @return int[]
186+
*/
187+
public function getInts(string $sep = ','): array
188+
{
189+
return $this->toInts($sep);
190+
}
191+
192+
/**
193+
* @return bool
194+
*/
195+
public function toBool(): bool
196+
{
197+
return Str::toBool($this->string);
198+
}
199+
165200
/**
166201
* @return string[]
167202
*/
168-
public function getStrings(string $sep = ',', int $limit = 0): array
203+
public function toArray(string $sep = ',', int $limit = 0): array
204+
{
205+
return $this->toStrings($sep, $limit);
206+
}
207+
208+
/**
209+
* @return string[]
210+
*/
211+
public function toStrings(string $sep = ',', int $limit = 0): array
169212
{
170213
return Str::explode($this->string, $sep, $limit);
171214
}
172215

216+
/**
217+
* @return string[]
218+
*/
219+
public function getStrings(string $sep = ',', int $limit = 0): array
220+
{
221+
return $this->toStrings($sep, $limit);
222+
}
223+
173224
/**
174225
* @return string
175226
*/

src/Str/StringHelper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use Toolkit\Stdlib\Str\Traits\StringCaseHelperTrait;
1414
use Toolkit\Stdlib\Str\Traits\StringCheckHelperTrait;
1515
use Toolkit\Stdlib\Str\Traits\StringLengthHelperTrait;
16-
use Toolkit\Stdlib\Str\Traits\StringSplitHelperTrait;
16+
use Toolkit\Stdlib\Str\Traits\StringConvertTrait;
1717
use Toolkit\Stdlib\Str\Traits\StringTruncateHelperTrait;
1818
use Toolkit\Stdlib\Util\UUID;
1919
use function array_merge;
@@ -50,7 +50,7 @@ abstract class StringHelper
5050
use StringCaseHelperTrait;
5151
use StringCheckHelperTrait;
5252
use StringLengthHelperTrait;
53-
use StringSplitHelperTrait;
53+
use StringConvertTrait;
5454
use StringTruncateHelperTrait;
5555

5656
/**

src/Str/Traits/StringCaseHelperTrait.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ trait StringCaseHelperTrait
4444
/**
4545
* Alias of the `strtolower()`
4646
*
47-
* @param string $str
47+
* @param string|int $str
4848
*
4949
* @return string
5050
*/
@@ -56,7 +56,7 @@ public static function lower($str): string
5656
/**
5757
* Alias of the `strtolower()`
5858
*
59-
* @param string $str
59+
* @param string|int $str
6060
*
6161
* @return string
6262
*/
@@ -86,7 +86,7 @@ public static function strtolower(string $str): string
8686
/**
8787
* Alias of the `strtoupper()`
8888
*
89-
* @param string $str
89+
* @param string|int $str
9090
*
9191
* @return string
9292
*/
@@ -98,7 +98,7 @@ public static function upper($str): string
9898
/**
9999
* Alias of the `strtoupper()`
100100
*
101-
* @param string $str
101+
* @param string|int $str
102102
*
103103
* @return string
104104
*/
@@ -108,7 +108,7 @@ public static function toUpper($str): string
108108
}
109109

110110
/**
111-
* @param string $str
111+
* @param string|int $str
112112
*
113113
* @return string
114114
*/

src/Str/Traits/StringSplitHelperTrait.php renamed to src/Str/Traits/StringConvertTrait.php

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

1010
namespace Toolkit\Stdlib\Str\Traits;
1111

12+
use Toolkit\Stdlib\Helper\DataHelper;
1213
use function array_map;
1314
use function array_values;
1415
use function count;
@@ -29,8 +30,17 @@
2930
*
3031
* @package Toolkit\Stdlib\Str\Traits
3132
*/
32-
trait StringSplitHelperTrait
33+
trait StringConvertTrait
3334
{
35+
/**
36+
* @param string $str
37+
*
38+
* @return bool
39+
*/
40+
public static function toBool(string $str): bool
41+
{
42+
return DataHelper::boolean($str);
43+
}
3444

3545
////////////////////////////////////////////////////////////////////////
3646
/// split to array
@@ -185,7 +195,7 @@ public static function splitByWidth(string $string, int $width = 1): array
185195

186196
/**
187197
* @param string $str
188-
* @param int $length
198+
* @param int $length
189199
*
190200
* @return array|string[]
191201
* @link https://www.php.net/manual/zh/function.str-split.php

test/Helper/DataHelperTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Toolkit\StdlibTest\Str;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Toolkit\Stdlib\Helper\DataHelper;
7+
8+
/**
9+
* Class DataHelperTest
10+
*
11+
* @package Toolkit\StdlibTest\Str
12+
*/
13+
class DataHelperTest extends TestCase
14+
{
15+
public function testToBool(): void
16+
{
17+
self::assertTrue(DataHelper::toBool(1));
18+
self::assertTrue(DataHelper::toBool('1'));
19+
self::assertTrue(DataHelper::toBool('on'));
20+
self::assertTrue(DataHelper::toBool('true'));
21+
self::assertFalse(DataHelper::toBool('false'));
22+
self::assertFalse(DataHelper::toBool('off'));
23+
self::assertFalse(DataHelper::toBool('0'));
24+
}
25+
}

0 commit comments

Comments
 (0)