Skip to content

Commit eabe6c6

Browse files
committed
update and add some new help methods
1 parent 2c71964 commit eabe6c6

File tree

3 files changed

+77
-21
lines changed

3 files changed

+77
-21
lines changed

src/Arr/ArrayHelper.php

Lines changed: 51 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ public static function keyExists($key, array $arr): bool
159159
*/
160160
public static function valueToLower(array $arr): array
161161
{
162-
return self::changeValueCase($arr, 0);
162+
return self::changeValueCase($arr, false);
163163
}
164164

165165
/**
@@ -175,12 +175,12 @@ public static function valueToUpper(array $arr): array
175175
/**
176176
* 将数组中的值全部转为大写或小写
177177
*
178-
* @param array $arr
179-
* @param int $toUpper 1 值大写 0 值小写
178+
* @param array|iterable $arr
179+
* @param bool $toUpper
180180
*
181181
* @return array
182182
*/
183-
public static function changeValueCase($arr, $toUpper = 1): array
183+
public static function changeValueCase($arr, bool $toUpper = true): array
184184
{
185185
$function = $toUpper ? 'strtoupper' : 'strtolower';
186186
$newArr = []; //格式化后的数组
@@ -248,7 +248,7 @@ public static function valueExistsOne($check, array $sampleArr): bool
248248
*
249249
* @return bool | string 不存在的会返回 检查到的 字段,判断时 请使用 ArrHelper::existsAll($need,$arr)===true 来验证是否全存在
250250
*/
251-
public static function existsAll($need, $arr, $type = false)
251+
public static function existsAll($need, $arr, bool $type = false)
252252
{
253253
if (is_array($need)) {
254254
foreach ((array)$need as $v) {
@@ -274,12 +274,12 @@ public static function existsAll($need, $arr, $type = false)
274274
* 有一个存在就返回 true 都不存在 return false
275275
*
276276
* @param string|array $need
277-
* @param array $arr 只能检查一维数组
277+
* @param array|iterable $arr 只能检查一维数组
278278
* @param bool $type 是否同时验证类型
279279
*
280280
* @return bool
281281
*/
282-
public static function existsOne($need, $arr, $type = false): bool
282+
public static function existsOne($need, $arr, bool $type = false): bool
283283
{
284284
if (is_array($need)) {
285285
foreach ((array)$need as $v) {
@@ -309,28 +309,59 @@ public static function existsOne($need, $arr, $type = false): bool
309309
/**
310310
* get key Max Width
311311
*
312-
* @param array $data
313-
* [
312+
* ```php
313+
* $data = [
314314
* 'key1' => 'value1',
315315
* 'key2-test' => 'value2',
316-
* ]
317-
* @param bool $expectInt
316+
* ]
317+
* ```
318+
*
319+
* @param array $data
320+
* @param bool $excludeInt
318321
*
319322
* @return int
320323
*/
321-
public static function getKeyMaxWidth(array $data, $expectInt = true): int
324+
public static function getKeyMaxWidth(array $data, bool $excludeInt = true): int
322325
{
323-
$keyMaxWidth = 0;
324-
326+
$maxWidth = 0;
325327
foreach ($data as $key => $value) {
326328
// key is not a integer
327-
if (!$expectInt || !is_numeric($key)) {
328-
$width = mb_strlen($key, 'UTF-8');
329-
$keyMaxWidth = $width > $keyMaxWidth ? $width : $keyMaxWidth;
329+
if (!$excludeInt || !is_numeric($key)) {
330+
$width = mb_strlen($key, 'UTF-8');
331+
$maxWidth = $width > $maxWidth ? $width : $maxWidth;
332+
}
333+
}
334+
335+
return $maxWidth;
336+
}
337+
338+
/**
339+
* get max width
340+
*
341+
* ```php
342+
* $keys = [
343+
* 'key1',
344+
* 'key2-test',
345+
* ]
346+
* ```
347+
*
348+
* @param array $keys
349+
* @param bool $excludeInt
350+
*
351+
* @return int
352+
*/
353+
public static function getMaxWidth(array $keys, bool $excludeInt = true): int
354+
{
355+
$maxWidth = 0;
356+
foreach ($keys as $key) {
357+
// key is not a integer
358+
if (!$excludeInt || !is_numeric($key)) {
359+
$keyWidth = mb_strlen($key, 'UTF-8');
360+
$maxWidth = $keyWidth > $maxWidth ? $keyWidth : $maxWidth;
330361
}
331362
}
332363

333-
return $keyMaxWidth;
364+
return $maxWidth;
334365
}
335366

336367
////////////////////////////////////////////////////////////
@@ -606,8 +637,8 @@ public static function remove(array &$arr, $key, $default = null)
606637
/**
607638
* Get a value from the array, and remove it.
608639
*
609-
* @param array $array
610-
* @param string $key
640+
* @param array|ArrayAccess $array
641+
* @param string|int $key
611642
* @param mixed $default
612643
*
613644
* @return mixed

src/Helper/Format.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,18 @@ class Format
4141
[172800, 'days', 86400],
4242
];
4343

44+
/**
45+
* format timestamp to how long ago
46+
*
47+
* @param int $secs
48+
*
49+
* @return string
50+
*/
51+
public static function howLongAgo(int $secs): string
52+
{
53+
return self::beforeTime($secs);
54+
}
55+
4456
/**
4557
* format Time
4658
*

src/Helper/IntHelper.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,23 @@
1515
/**
1616
* Class IntHelper
1717
*
18-
* @link http://cn2.php.net/manual/zh/function.pack.php#119402
18+
* @link http://cn2.php.net/manual/zh/function.pack.php#119402
1919
*/
2020
class IntHelper
2121
{
22+
/**
23+
* @param int $val1
24+
* @param int $val2
25+
*
26+
* @return int
27+
*/
28+
public static function getMax(int $val1, int $val2): int
29+
{
30+
return $val1 > $val2 ? $val1 : $val2;
31+
}
32+
33+
// ----- http://cn2.php.net/manual/zh/function.pack.php#119402
34+
2235
/**
2336
* @param $i
2437
*

0 commit comments

Comments
 (0)