1
0
mirror of https://github.com/e107inc/e107.git synced 2025-08-05 14:17:49 +02:00

Add: e_parse::toFlatArray() and e_parse::fromFlatArray()

Utility functions to convert multi-dimensional arrays to slash-delimited
single-dimensional arrays and vice versa
This commit is contained in:
Nick Liu
2021-04-17 02:56:12 -05:00
parent f6f63b680f
commit ce7f3b5d56
3 changed files with 99 additions and 29 deletions

View File

@@ -1806,6 +1806,65 @@ class e_parse
}
/**
* Flatten a multi-dimensional associative array with slashes.
*
* Based on Illuminate\Support\Arr::dot()
* @copyright Copyright (c) Taylor Otwell
* @license https://github.com/illuminate/support/blob/master/LICENSE.md MIT License
* @param $array
* @param string $prepend
* @return array
*/
public static function toFlatArray($array, $prepend = '')
{
$results = [];
foreach ($array as $key => $value)
{
if (is_array($value) && !empty($value))
{
$results = array_merge($results, static::toFlatArray($value, $prepend . $key . '/'));
}
else
{
$results[$prepend . $key] = $value;
}
}
return $results;
}
/**
* Convert a flattened slash-delimited multi-dimensional array back into an actual multi-dimensional array
*
* Inverse of {@link e_parse::toFlatArray()}
*
* @param $array
* @param string $unprepend
* @return array
*/
public static function fromFlatArray($array, $unprepend = '')
{
$output = [];
foreach ($array as $key => $value)
{
if (!empty($unprepend) && substr($key, 0, strlen($unprepend)) == $unprepend)
$key = substr($key, strlen($unprepend));
$parts = explode('/', $key);
$nested = &$output;
while (count($parts) > 1)
{
$nested = &$nested[array_shift($parts)];
if (!is_array($nested)) $nested = [];
}
$nested[array_shift($parts)] = $value;
}
return $output;
}
/**
* Convert text blocks which are to be embedded within JS
*

View File

@@ -656,7 +656,7 @@ class social_ui extends e_admin_ui
<tr><td>".LAN_TYPE."</td><td>$provider_type</td></tr>
";
$fieldInfo = self::array_slash($slcm->getFieldsOf($provider_name));
$fieldInfo = e107::getParser()->toFlatArray($slcm->getFieldsOf($provider_name));
@@ -706,7 +706,7 @@ class social_ui extends e_admin_ui
";
$text .= "<td><table class='table table-bordered table-condensed' style='margin:0'>";
$fieldInfo = self::array_slash($slcm->getFieldsOf($provider_name));
$fieldInfo = e107::getParser()->toFlatArray($slcm->getFieldsOf($provider_name));
foreach ($fieldInfo as $fieldSlash => $description)
{
$field = str_replace("/", "][", $fieldSlash);
@@ -748,33 +748,6 @@ class social_ui extends e_admin_ui
return $text;
}
/**
* Based on Illuminate\Support\Arr::dot()
* @copyright Copyright (c) Taylor Otwell
* @license https://github.com/illuminate/support/blob/master/LICENSE.md MIT License
* @param $array
* @param string $prepend
* @return array
*/
private static function array_slash($array, $prepend = '')
{
$results = [];
foreach ($array as $key => $value)
{
if (is_array($value) && !empty($value))
{
$results = array_merge($results, static::array_slash($value, $prepend . $key . '/'));
}
else
{
$results[$prepend . $key] = $value;
}
}
return $results;
}
private static function getPlaceholderFor($providerName, $fieldSlash)
{
switch ($fieldSlash)

View File

@@ -583,6 +583,44 @@ while(&#036;row = &#036;sql-&gt;fetch())
}
*/
public function testToFlatArray()
{
$input = [
'a' => [
'b' => [
'c' => 'value',
],
],
];
$expected = [
'prepend/xyza/b/c' => 'value'
];
$tp = $this->tp;
$actual = $tp->toFlatArray($input, 'prepend/xyz');
$this->assertSame($expected, $actual);
}
public function testFromFlatArray()
{
$input = [
'prepend/xyza/b/c' => 'value'
];
$expected = [
'a' => [
'b' => [
'c' => 'value',
],
],
];
$tp = $this->tp;
$actual = $tp->fromFlatArray($input, 'prepend/xyz');
$this->assertSame($expected, $actual);
}
public function testToForm()
{