mirror of
https://github.com/wiosna-dev/common-library.git
synced 2026-03-12 01:31:45 +01:00
Arrays > minor refactoring
This commit is contained in:
@@ -4,6 +4,7 @@ Common and useful classes, methods, exceptions etc.
|
|||||||
# 0.0.21
|
# 0.0.21
|
||||||
|
|
||||||
1. Composer > require ext-pcre
|
1. Composer > require ext-pcre
|
||||||
|
2. Arrays > minor refactoring
|
||||||
|
|
||||||
# 0.0.20
|
# 0.0.20
|
||||||
|
|
||||||
|
|||||||
@@ -673,7 +673,7 @@ class Arrays
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
$effect = $array;
|
$effect = &$array;
|
||||||
ksort($effect, $sortFlags);
|
ksort($effect, $sortFlags);
|
||||||
|
|
||||||
foreach ($effect as &$value) {
|
foreach ($effect as &$value) {
|
||||||
@@ -822,60 +822,66 @@ class Arrays
|
|||||||
*/
|
*/
|
||||||
public static function getLastElementsPaths(array $array, $separator = '.', $parentPath = '', $stopIfMatchedBy = '')
|
public static function getLastElementsPaths(array $array, $separator = '.', $parentPath = '', $stopIfMatchedBy = '')
|
||||||
{
|
{
|
||||||
|
/*
|
||||||
|
* No elements?
|
||||||
|
* Nothing to do
|
||||||
|
*/
|
||||||
|
if (empty($array)) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!empty($stopIfMatchedBy)) {
|
||||||
|
$stopIfMatchedBy = self::makeArray($stopIfMatchedBy);
|
||||||
|
}
|
||||||
|
|
||||||
$paths = [];
|
$paths = [];
|
||||||
|
|
||||||
if (!empty($array)) {
|
foreach ($array as $key => $value) {
|
||||||
if (!empty($stopIfMatchedBy)) {
|
$path = $key;
|
||||||
$stopIfMatchedBy = self::makeArray($stopIfMatchedBy);
|
$stopRecursion = false;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* If the path of parent element is delivered,
|
||||||
|
* I have to use it and build longer path
|
||||||
|
*/
|
||||||
|
if (!empty($parentPath)) {
|
||||||
|
$pathTemplate = '%s%s%s';
|
||||||
|
$path = sprintf($pathTemplate, $parentPath, $separator, $key);
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($array as $key => $value) {
|
/*
|
||||||
$path = $key;
|
* Check if the key or current path matches one of patterns at which the process should be stopped,
|
||||||
$stopRecursion = false;
|
* the recursive not used. It means that I have to pass current value and stop processing of the
|
||||||
|
* array (don't go to the next step).
|
||||||
|
*/
|
||||||
|
if (!empty($stopIfMatchedBy)) {
|
||||||
|
foreach ($stopIfMatchedBy as $rawPattern) {
|
||||||
|
$pattern = sprintf('|%s|', $rawPattern);
|
||||||
|
|
||||||
/*
|
if (preg_match($pattern, $key) || preg_match($pattern, $path)) {
|
||||||
* If the path of parent element is delivered,
|
$stopRecursion = true;
|
||||||
* I have to use it and build longer path
|
break;
|
||||||
*/
|
|
||||||
if (!empty($parentPath)) {
|
|
||||||
$pathTemplate = '%s%s%s';
|
|
||||||
$path = sprintf($pathTemplate, $parentPath, $separator, $key);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Check if the key or current path matches one of patterns at which the process should be stopped,
|
|
||||||
* the recursive not used. It means that I have to pass current value and stop processing of the
|
|
||||||
* array (don't go to the next step).
|
|
||||||
*/
|
|
||||||
if (!empty($stopIfMatchedBy)) {
|
|
||||||
foreach ($stopIfMatchedBy as $rawPattern) {
|
|
||||||
$pattern = sprintf('|%s|', $rawPattern);
|
|
||||||
|
|
||||||
if (preg_match($pattern, $key) || preg_match($pattern, $path)) {
|
|
||||||
$stopRecursion = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* The value is passed to the returned array if:
|
* The value is passed to the returned array if:
|
||||||
* - it's not an array
|
* - it's not an array
|
||||||
* or
|
* or
|
||||||
* - the process is stopped, recursive is not used
|
* - the process is stopped, recursive is not used
|
||||||
*/
|
*/
|
||||||
if (!is_array($value) || (is_array($value) && empty($value)) || $stopRecursion) {
|
if (!is_array($value) || (is_array($value) && empty($value)) || $stopRecursion) {
|
||||||
$paths[$path] = $value;
|
$paths[$path] = $value;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Let's iterate through the next level, using recursive
|
* Let's iterate through the next level, using recursive
|
||||||
*/
|
*/
|
||||||
if (is_array($value)) {
|
if (is_array($value)) {
|
||||||
$recursivePaths = self::getLastElementsPaths($value, $separator, $path, $stopIfMatchedBy);
|
$recursivePaths = self::getLastElementsPaths($value, $separator, $path, $stopIfMatchedBy);
|
||||||
$paths += $recursivePaths;
|
$paths += $recursivePaths;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user