Skip to content

Commit

Permalink
refactor arrays method
Browse files Browse the repository at this point in the history
  • Loading branch information
cs-eliseev committed Apr 1, 2019
1 parent eb2ccf5 commit f34259f
Showing 1 changed file with 32 additions and 61 deletions.
93 changes: 32 additions & 61 deletions src/Arrays.php
Expand Up @@ -22,9 +22,7 @@ class Arrays
*/
public static function get(array $data, $key, $default = null)
{
if (!array_key_exists($key, $data)) return $default;

return $data[$key];
return array_key_exists($key, $data) ? $data[$key] : $default;
}

/**
Expand Down Expand Up @@ -120,12 +118,10 @@ public static function map(array $data, $keyGroup, $keyValue = null): array

foreach ($data as $item) {
if (array_key_exists($keyGroup, $item)) {
$result[$item[$keyGroup]] = is_null($keyValue) ? $item : (array_key_exists($keyValue, $item) ? $item[$keyValue] : null);
$result[$item[$keyGroup]] = self::getValueCheckParam($item, $keyValue);
}
}

unset($item);

return $result;
}

Expand All @@ -148,12 +144,10 @@ public static function group(array $data, $keyGroup, $keyValue = null): array

if (empty($result[$key])) $result[$key] = [];

$result[$key][] = is_null($keyValue) ? $item : (array_key_exists($keyValue, $item) ? $item[$keyValue] : null);
$result[$key][] = self::getValueCheckParam($item, $keyValue);
}
}

unset($item, $key);

return $result;
}
/**
Expand All @@ -171,12 +165,10 @@ public static function index(array $data, $keyGroup, $keyValue = null): array

foreach ($data as $item) {
if (array_key_exists($keyGroup, $item)) {
$result[$item[$keyGroup]][] = is_null($keyValue) ? $item : (array_key_exists($keyValue, $item) ? $item[$keyValue] : null);
$result[$item[$keyGroup]][] = self::getValueCheckParam($item, $keyValue);
}
}

unset($item);

return $result;
}

Expand Down Expand Up @@ -218,20 +210,13 @@ public static function appendNotEmptyData(array $first, array $second): array
*
* @return array
*/
public static function replaceEmptyNotEmptyData(array $first, array $second): array
public static function replaceEmptyNotEmptyData(array $first = [], array $second = []): array
{
if (empty($first)) {
return [];
} elseif (empty($second)) {
return $first;
}

foreach ($first as $key => &$value) {
if (empty($value) && array_key_exists($key, $second)) $value = $second[$key];
if (!empty($first) && !empty($second)) {
foreach ($first as $key => &$value) {
if (empty($value) && array_key_exists($key, $second)) $value = $second[$key];
}
}

unset($value, $key, $second);

return $first;
}

Expand All @@ -243,20 +228,13 @@ public static function replaceEmptyNotEmptyData(array $first, array $second): ar
*
* @return array
*/
public static function replaceNotEmptyData(array $first, array $second): array
public static function replaceNotEmptyData(array $first = [], array $second = []): array
{
if (empty($first)) {
return [];
} elseif (empty($second)) {
return $first;
}

foreach ($first as $key => &$value) {
if (!empty($second[$key])) $value = $second[$key];
if (!empty($first) && !empty($second)) {
foreach ($first as $key => &$value) {
if (!empty($second[$key])) $value = $second[$key];
}
}

unset($value, $key, $second);

return $first;
}

Expand All @@ -268,20 +246,13 @@ public static function replaceNotEmptyData(array $first, array $second): array
*
* @return array
*/
public static function mergeNotEmptyData(array $first, array $second): array
public static function mergeNotEmptyData(array $first = [], array $second = []): array
{
if (empty($first) && empty($second)) {
return [];
} elseif (empty($second)) {
return $first;
}

foreach ($second as $key => $value) {
if (!empty($value)) $first[$key] = $value;
if (!empty($first) && !empty($second)) {
foreach ($second as $key => $value) {
if (!empty($value)) $first[$key] = $value;
}
}

unset($value, $key);

return $first;
}

Expand All @@ -302,9 +273,6 @@ public static function emptyToNull(array $data, bool $recursive = false): array
$value = self::emptyToNull($value, $recursive);
}
}

unset($value, $key);

return $data;
}

Expand All @@ -325,9 +293,6 @@ public static function removeEmpty(array $data, bool $recursive = false): array
$value = self::removeEmpty($value, $recursive);
}
}

unset($value, $key);

return $data;
}

Expand All @@ -348,9 +313,6 @@ public static function removeNull(array $data, bool $recursive = false): array
$value = self::removeNull($value, $recursive);
}
}

unset($value, $key);

return $data;
}

Expand All @@ -371,9 +333,6 @@ public static function trim(array $data, bool $recursive = true): array
$value = self::trim($value);
}
}

unset($value, $key);

return $data;
}

Expand All @@ -390,7 +349,6 @@ protected static function appendNotEmpty(array $first, array $second): array
foreach ($second as $key => $value) {
if (!empty($value) && !array_key_exists($key, $first)) $first[$key] = $value;
}

return $first;
}

Expand All @@ -417,4 +375,17 @@ protected static function replaceTagInfo(array $info): array

return $result;
}

/**
* Get value check param
*
* @param array $data
* @param $keyValue
*
* @return mixed
*/
protected static function getValueCheckParam(array $data, $keyValue)
{
return is_null($keyValue) ? $data : (array_key_exists($keyValue, $data) ? $data[$keyValue] : null);
}
}

0 comments on commit f34259f

Please sign in to comment.