diff --git a/e107_handlers/e_parse_class.php b/e107_handlers/e_parse_class.php index 865f2a107..7ae11c650 100644 --- a/e107_handlers/e_parse_class.php +++ b/e107_handlers/e_parse_class.php @@ -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 * diff --git a/e107_plugins/social/admin_config.php b/e107_plugins/social/admin_config.php index 18b36ddc7..77bb54517 100644 --- a/e107_plugins/social/admin_config.php +++ b/e107_plugins/social/admin_config.php @@ -656,7 +656,7 @@ class social_ui extends e_admin_ui ".LAN_TYPE."$provider_type "; - $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 .= ""; - $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) diff --git a/e107_tests/tests/unit/e_parseTest.php b/e107_tests/tests/unit/e_parseTest.php index dee0dde1b..88c802a82 100644 --- a/e107_tests/tests/unit/e_parseTest.php +++ b/e107_tests/tests/unit/e_parseTest.php @@ -583,6 +583,44 @@ while($row = $sql->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() {