Some lang improvements

This commit is contained in:
Andy Miller
2018-10-22 18:11:26 -06:00
parent 8590f4fbf5
commit a9196c3268
5 changed files with 76 additions and 1 deletions

View File

@@ -1,8 +1,11 @@
# v1.6.0-beta.4
## mm/dd/2018
1. [](#new)
* Added `Utils::arrayFlattenDotNotation()` and `Utils::arrayUnflattenDotNotation()` helper methods
1. [](#improved)
* Added apcu autoloader optimization
* Additional helper methods in `Language`, `Languages`, and `LanguageCodes` classes
1. [](#bugfix)
* Use login provider User avatar if set

View File

@@ -52,4 +52,15 @@ class Languages extends Data
{
$this->items = Utils::arrayMergeRecursiveUnique($this->items, $data);
}
public function flattenByLang($lang)
{
$language = $this->items[$lang];
return Utils::arrayFlattenDotNotation($language);
}
public function unflatten($array)
{
return Utils::arrayUnflattenDotNotation($array);
}
}

View File

@@ -504,4 +504,16 @@ class Language
return $this->http_accept_language;
}
/**
* Accessible wrapper to LanguageCodes
*
* @param $code
* @param string $type
* @return bool
*/
public function getLanguageCode($code, $type = 'name')
{
return LanguageCodes::get($code, $type);
}
}

View File

@@ -196,7 +196,7 @@ class LanguageCodes
return $results;
}
protected static function get($code, $type)
public static function get($code, $type)
{
if (isset(static::$codes[$code][$type])) {
return static::$codes[$code][$type];

View File

@@ -708,6 +708,55 @@ abstract class Utils
return $flatten;
}
/**
* Flatten a multi-dimensional associative array into dot notation
*
* @param array $array
* @param string $prepend
* @return array
*/
public static function arrayFlattenDotNotation($array, $prepend = '')
{
$results = array();
foreach ($array as $key => $value)
{
if (is_array($value))
{
$results = array_merge($results, static::arrayFlattenDotNotation($value, $prepend.$key.'.'));
}
else
{
$results[$prepend.$key] = $value;
}
}
return $results;
}
/**
* Opposite of flatten, convert flat dot notation array to multi dimensional array
*
* @param $array
* @param string $separator
* @return array
*/
public static function arrayUnflattenDotNotation($array, $separator = '.') {
$newArray = array();
foreach($array as $key => $value) {
$dots = explode($separator, $key);
if(count($dots) > 1) {
$last = &$newArray[ $dots[0] ];
foreach($dots as $k => $dot) {
if($k == 0) continue;
$last = &$last[$dot];
}
$last = $value;
} else {
$newArray[$key] = $value;
}
}
return $newArray;
}
/**
* Checks if the passed path contains the language code prefix
*