Skip to content

Commit 6c87e81

Browse files
committed
feat: add more array and string uril methods
1 parent 41c0764 commit 6c87e81

File tree

7 files changed

+44
-15
lines changed

7 files changed

+44
-15
lines changed

src/Arr/ArrayHelper.php

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use ArrayAccess;
1313
use ArrayObject;
1414
use stdClass;
15+
use Toolkit\Stdlib\Helper\DataHelper;
1516
use Traversable;
1617
use function array_change_key_case;
1718
use function array_diff;
@@ -29,6 +30,7 @@
2930
use function explode;
3031
use function get_class;
3132
use function gettype;
33+
use function implode;
3234
use function in_array;
3335
use function is_array;
3436
use function is_numeric;
@@ -212,7 +214,7 @@ public static function valueExistsAll($check, array $sampleArr): bool
212214
// 以逗号分隔的会被拆开,组成数组
213215
if (is_string($check)) {
214216
$check = trim($check, ', ');
215-
$check = strpos($check, ',') !== false ? (array)explode(',', $check) : [$check];
217+
$check = str_contains($check, ',') ? explode(',', $check) : [$check];
216218
}
217219

218220
return !array_diff((array)$check, $sampleArr);
@@ -232,7 +234,7 @@ public static function valueExistsOne($check, array $sampleArr): bool
232234
// 以逗号分隔的会被拆开,组成数组
233235
if (is_string($check)) {
234236
$check = trim($check, ', ');
235-
$check = strpos($check, ',') !== false ? (array)explode(',', $check) : [$check];
237+
$check = str_contains($check, ',') ? explode(',', $check) : [$check];
236238
}
237239

238240
return (bool)array_intersect((array)$check, $sampleArr);
@@ -254,7 +256,7 @@ public static function existsAll($need, $arr, bool $type = false)
254256
foreach ((array)$need as $v) {
255257
self::existsAll($v, $arr, $type);
256258
}
257-
} elseif (strpos($need, ',') !== false) {
259+
} elseif (str_contains($need, ',')) {
258260
$need = explode(',', $need);
259261
self::existsAll($need, $arr, $type);
260262
} else {
@@ -289,7 +291,7 @@ public static function existsOne($need, $arr, bool $type = false): bool
289291
}
290292
}
291293
} else {
292-
if (strpos($need, ',') !== false) {
294+
if (str_contains($need, ',')) {
293295
$need = explode(',', $need);
294296

295297
return self::existsOne($need, $arr, $type);
@@ -514,7 +516,7 @@ public static function flatten(array $array, int $depth = INF): array
514516
*
515517
* @return void
516518
*/
517-
public static function forget(&$array, $keys): void
519+
public static function forget(array &$array, $keys): void
518520
{
519521
$original = &$array;
520522
$keys = (array)$keys;
@@ -787,7 +789,7 @@ public static function toStringNoKey(
787789
*
788790
* @return string
789791
*/
790-
public static function toFormatString($array, int $length = 400): string
792+
public static function toFormatString(array $array, int $length = 400): string
791793
{
792794
$string = var_export($array, true);
793795

@@ -834,7 +836,23 @@ public static function toLimitOut($array): array
834836
}
835837

836838
// $num++;
837-
838839
return $array;
839840
}
841+
842+
/**
843+
* @param array $data
844+
* @param string $kvSep
845+
* @param string $lineSep
846+
*
847+
* @return string
848+
*/
849+
public static function toKVString(array $data, string $kvSep = '=', string $lineSep = "\n"): string
850+
{
851+
$lines = [];
852+
foreach ($data as $key => $val) {
853+
$lines = $key . $kvSep . DataHelper::toString($val);
854+
}
855+
856+
return implode($lineSep, $lines);
857+
}
840858
}

src/Helper/DataHelper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class DataHelper
3838
*
3939
* @return bool
4040
*/
41-
public static function boolean($val, $nullAsFalse = false): bool
41+
public static function boolean($val, bool $nullAsFalse = false): bool
4242
{
4343
if ($val !== null && !is_scalar($val)) {
4444
return (bool)$val;
@@ -55,7 +55,7 @@ public static function boolean($val, $nullAsFalse = false): bool
5555
*
5656
* @return bool
5757
*/
58-
public static function toBool($val, $nullAsFalse = false): bool
58+
public static function toBool($val, bool $nullAsFalse = false): bool
5959
{
6060
return self::boolean($val, $nullAsFalse);
6161
}

src/Std.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,22 @@
22

33
namespace Toolkit\Stdlib;
44

5+
use Toolkit\Stdlib\Helper\DataHelper;
6+
57
/**
68
* Class Std
79
*
810
* @package Toolkit\Stdlib
911
*/
1012
class Std
1113
{
14+
/**
15+
* @param mixed $data
16+
*
17+
* @return string
18+
*/
19+
public static function toString($data): string
20+
{
21+
return DataHelper::toString($data);
22+
}
1223
}

src/Str/StringHelper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,8 +349,8 @@ public static function wordFormat(string $keyword): string
349349
public static function deleteStripSpace($fileName, $type = 0)
350350
{
351351
$data = trim(file_get_contents($fileName));
352-
$data = 0 === strpos($data, '<?php') ? substr($data, 5) : $data;
353-
$data = substr($data, -2) === '?>' ? substr($data, 0, -2) : $data;
352+
$data = str_starts_with($data, '<?php') ? substr($data, 5) : $data;
353+
$data = str_ends_with($data, '?>') ? substr($data, 0, -2) : $data;
354354

355355
//去掉所有注释 换行空白保留
356356
if ((int)$type === 1) {

src/Str/UrlHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public static function simpleCheck(string $url): bool
9595
*/
9696
public static function isFullUrl(string $url): bool
9797
{
98-
return 0 === strpos($url, 'http:') || 0 === strpos($url, 'https:') || 0 === strpos($url, '//');
98+
return str_starts_with($url, 'http:') || str_starts_with($url, 'https:') || str_starts_with($url, '//');
9999
}
100100

101101
/**

src/Util/AutoLoader.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ private function findFileWithExtension(string $class)
309309

310310
// PSR-4
311311
foreach ($this->psr4Map as $prefix => $dir) {
312-
if (0 === strpos($class, $prefix)) {
312+
if (str_starts_with($class, $prefix)) {
313313
$length = strlen($prefix);
314314

315315
if (file_exists($file = $dir . DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $length))) {
@@ -322,7 +322,7 @@ private function findFileWithExtension(string $class)
322322
$logicalPathPsr0 = str_replace('_', DIRECTORY_SEPARATOR, $class) . '.php';
323323

324324
foreach ($this->psr0Map as $prefix => $dir) {
325-
if (0 === strpos($class, $prefix)) {
325+
if (str_starts_with($class, $prefix)) {
326326
$file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0;
327327

328328
if (file_exists($file)) {

src/Util/PhpDotEnv.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ private function settingEnv(array $data): void
132132
$name = strtoupper($name);
133133

134134
// don't check existence with getenv() because of thread safety issues
135-
$notHttpName = 0 !== strpos($name, 'HTTP_');
135+
$notHttpName = !str_starts_with($name, 'HTTP_');
136136
if ((isset($_ENV[$name]) || (isset($_SERVER[$name]) && $notHttpName)) && !isset($loadedVars[$name])) {
137137
continue;
138138
}

0 commit comments

Comments
 (0)