Skip to content

Commit 9785c99

Browse files
committed
update some for array helper
1 parent 682d1d8 commit 9785c99

File tree

5 files changed

+406
-320
lines changed

5 files changed

+406
-320
lines changed

Diff for: .phpunit.result.cache

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
C:37:"PHPUnit\Runner\DefaultTestResultCache":111:{a:2:{s:7:"defects";a:0:{}s:5:"times";a:1:{s:51:"Toolkit\StdlibTest\Str\StringHelperTest::testStrLen";d:0.003;}}}

Diff for: src/Arr/ArrayHelper.php

+11-319
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
use ArrayAccess;
1313
use ArrayObject;
1414
use stdClass;
15-
use Toolkit\Stdlib\Php;
1615
use Traversable;
1716
use function array_change_key_case;
1817
use function array_diff;
@@ -32,7 +31,6 @@
3231
use function gettype;
3332
use function in_array;
3433
use function is_array;
35-
use function is_int;
3634
use function is_numeric;
3735
use function is_object;
3836
use function is_resource;
@@ -52,6 +50,9 @@
5250
*/
5351
class ArrayHelper
5452
{
53+
use Traits\ArrayMergeTrait;
54+
use Traits\ArrayValueGetSetTrait;
55+
5556
/**
5657
* Determine whether the given value is array accessible.
5758
*
@@ -118,126 +119,6 @@ public static function toObject($array, $class = stdClass::class)
118119
return $object;
119120
}
120121

121-
/**
122-
* Get Multi - 获取多个, 可以设置默认值
123-
*
124-
* @param array $data array data
125-
* @param array $needKeys
126-
* $needKeys = [
127-
* 'name',
128-
* 'password',
129-
* 'status' => '1'
130-
* ]
131-
* @param bool|false $unsetKey
132-
*
133-
* @return array
134-
*/
135-
public static function gets(array &$data, array $needKeys = [], $unsetKey = false): array
136-
{
137-
$needed = [];
138-
139-
foreach ($needKeys as $key => $default) {
140-
if (is_int($key)) {
141-
$key = $default;
142-
$default = null;
143-
}
144-
145-
if (isset($data[$key])) {
146-
$value = $data[$key];
147-
148-
if (is_int($default)) {
149-
$value = (int)$value;
150-
} elseif (is_string($default)) {
151-
$value = trim($value);
152-
} elseif (is_array($default)) {
153-
$value = (array)$value;
154-
}
155-
156-
$needed[$key] = $value;
157-
158-
if ($unsetKey) {
159-
unset($data[$key]);
160-
}
161-
} else {
162-
$needed[$key] = $default;
163-
}
164-
}
165-
166-
return $needed;
167-
}
168-
169-
/**
170-
* 递归合并两个多维数组,后面的值将会递归覆盖原来的值
171-
*
172-
* @param array|null $src
173-
* @param array $new
174-
*
175-
* @return array
176-
*/
177-
public static function merge($src, array $new): array
178-
{
179-
if (!$src || !is_array($src)) {
180-
return $new;
181-
}
182-
183-
if (!$new) {
184-
return $src;
185-
}
186-
187-
foreach ($new as $key => $value) {
188-
if (is_int($key)) {
189-
if (isset($src[$key])) {
190-
$src[] = $value;
191-
} else {
192-
$src[$key] = $value;
193-
}
194-
} elseif (array_key_exists($key, $src) && is_array($value)) {
195-
$src[$key] = self::merge($src[$key], $new[$key]);
196-
} else {
197-
$src[$key] = $value;
198-
}
199-
}
200-
201-
return $src;
202-
}
203-
204-
/**
205-
* 递归合并多个多维数组,
206-
*
207-
* @from yii2
208-
* Merges two or more arrays into one recursively.
209-
*
210-
* @param array $args
211-
*
212-
* @return array the merged array (the original arrays are not changed.)
213-
*/
214-
public static function merge2(...$args): array
215-
{
216-
/** @var array[] $args */
217-
$res = array_shift($args);
218-
219-
while (!empty($args)) {
220-
/** @var array $next */
221-
$next = array_shift($args);
222-
223-
foreach ($next as $k => $v) {
224-
if (is_int($k)) {
225-
if (isset($res[$k])) {
226-
$res[] = $v;
227-
} else {
228-
$res[$k] = $v;
229-
}
230-
} elseif (is_array($v) && isset($res[$k]) && is_array($res[$k])) {
231-
$res[$k] = self::merge2($res[$k], $v);
232-
} else {
233-
$res[$k] = $v;
234-
}
235-
}
236-
}
237-
238-
return $res;
239-
}
240-
241122
/**
242123
* 清理数组值的空白
243124
*
@@ -452,108 +333,6 @@ public static function getKeyMaxWidth(array $data, $expectInt = true): int
452333
return $keyMaxWidth;
453334
}
454335

455-
/**
456-
* Get data from array or object by path.
457-
* Example: `DataCollector::getByPath($array, 'foo.bar.yoo')` equals to $array['foo']['bar']['yoo'].
458-
*
459-
* @param array|ArrayAccess $data An array or object to get value.
460-
* @param mixed $path The key path.
461-
* @param mixed $default
462-
* @param string $separator Separator of paths.
463-
*
464-
* @return mixed Found value, null if not exists.
465-
*/
466-
public static function getByPath($data, string $path, $default = null, string $separator = '.')
467-
{
468-
if (isset($data[$path])) {
469-
return $data[$path];
470-
}
471-
472-
// Error: will clear '0'. eg 'some-key.0'
473-
// if (!$nodes = array_filter(explode($separator, $path))) {
474-
if (!$nodes = explode($separator, $path)) {
475-
return $default;
476-
}
477-
478-
$dataTmp = $data;
479-
480-
foreach ($nodes as $arg) {
481-
if (is_object($dataTmp) && isset($dataTmp->$arg)) {
482-
$dataTmp = $dataTmp->$arg;
483-
} elseif ((is_array($dataTmp) || $dataTmp instanceof ArrayAccess) && isset($dataTmp[$arg])) {
484-
$dataTmp = $dataTmp[$arg];
485-
} else {
486-
return $default;
487-
}
488-
}
489-
490-
return $dataTmp;
491-
}
492-
493-
/**
494-
* findValueByNodes
495-
*
496-
* @param array $data
497-
* @param array $nodes
498-
* @param mixed $default
499-
*
500-
* @return mixed
501-
*/
502-
public static function getValueByNodes(array $data, array $nodes, $default = null)
503-
{
504-
$temp = $data;
505-
506-
foreach ($nodes as $name) {
507-
if (isset($temp[$name])) {
508-
$temp = $temp[$name];
509-
} else {
510-
$temp = $default;
511-
break;
512-
}
513-
}
514-
515-
return $temp;
516-
}
517-
518-
/**
519-
* setByPath
520-
*
521-
* @param array|ArrayAccess &$data
522-
* @param string $path
523-
* @param mixed $value
524-
* @param string $separator
525-
*/
526-
public static function setByPath(&$data, string $path, $value, string $separator = '.'): void
527-
{
528-
if (false === strpos($path, $separator)) {
529-
$data[$path] = $value;
530-
return;
531-
}
532-
533-
if (!$nodes = array_filter(explode($separator, $path))) {
534-
return;
535-
}
536-
537-
$dataTmp = &$data;
538-
539-
foreach ($nodes as $node) {
540-
if (is_array($dataTmp)) {
541-
if (empty($dataTmp[$node])) {
542-
$dataTmp[$node] = [];
543-
}
544-
545-
$dataTmp = &$dataTmp[$node];
546-
} else {
547-
// If a node is value but path is not go to the end, we replace this value as a new store.
548-
// Then next node can insert new value to this store.
549-
$dataTmp = [];
550-
}
551-
}
552-
553-
// Now, path go to the end, means we get latest node, set value to this node.
554-
$dataTmp = $value;
555-
}
556-
557336
////////////////////////////////////////////////////////////
558337
/// from laravel
559338
////////////////////////////////////////////////////////////
@@ -669,93 +448,6 @@ public static function exists(array $array, $key): bool
669448
return array_key_exists($key, $array);
670449
}
671450

672-
/**
673-
* Add an element to an array using "dot" notation if it doesn't exist.
674-
*
675-
* @param array $array
676-
* @param string $key
677-
* @param mixed $value
678-
*
679-
* @return array
680-
*/
681-
public static function add(array $array, $key, $value): array
682-
{
683-
if (static::has($array, $key)) {
684-
static::set($array, $key, $value);
685-
}
686-
687-
return $array;
688-
}
689-
690-
/**
691-
* Get an item from an array using "dot" notation.
692-
*
693-
* @param ArrayAccess|array $array
694-
* @param string $key
695-
* @param mixed $default
696-
*
697-
* @return mixed
698-
*/
699-
public static function get($array, $key, $default = null)
700-
{
701-
if (!static::accessible($array)) {
702-
return Php::value($default);
703-
}
704-
705-
if (null === $key) {
706-
return $array;
707-
}
708-
709-
if (static::exists($array, $key)) {
710-
return $array[$key];
711-
}
712-
713-
foreach (explode('.', $key) as $segment) {
714-
if (static::accessible($array) && static::exists($array, $segment)) {
715-
$array = $array[$segment];
716-
} else {
717-
return Php::value($default);
718-
}
719-
}
720-
721-
return $array;
722-
}
723-
724-
/**
725-
* Set an array item to a given value using "dot" notation.
726-
* If no key is given to the method, the entire array will be replaced.
727-
*
728-
* @param array $array
729-
* @param string $key
730-
* @param mixed $value
731-
*
732-
* @return array
733-
*/
734-
public static function set(array &$array, $key, $value): array
735-
{
736-
if (null === $key) {
737-
return ($array = $value);
738-
}
739-
740-
$keys = explode('.', $key);
741-
742-
while (count($keys) > 1) {
743-
$key = array_shift($keys);
744-
// If the key doesn't exist at this depth, we will just create an empty array
745-
// to hold the next value, allowing us to create the arrays to hold final
746-
// values at the correct depth. Then we'll keep digging into the array.
747-
if (!isset($array[$key]) || !is_array($array[$key])) {
748-
$array[$key] = [];
749-
}
750-
751-
$array = &$array[$key];
752-
}
753-
754-
$array[array_shift($keys)] = $value;
755-
756-
return $array;
757-
}
758-
759451
/**
760452
* Flatten a multi-dimensional array into a single level.
761453
*
@@ -1026,14 +718,14 @@ public static function toString(
1026718

1027719
if (is_array($value)) {
1028720
$string .= $keyStr . 'Array(' . self::toString(
1029-
$value,
1030-
$length,
1031-
$cycles,
1032-
$showKey,
1033-
$addMark,
1034-
$separator,
1035-
$string
1036-
) . ')' . $separator;
721+
$value,
722+
$length,
723+
$cycles,
724+
$showKey,
725+
$addMark,
726+
$separator,
727+
$string
728+
) . ')' . $separator;
1037729
} elseif (is_object($value)) {
1038730
$string .= $keyStr . 'Object(' . get_class($value) . ')' . $separator;
1039731
} elseif (is_resource($value)) {

0 commit comments

Comments
 (0)