- Worked on iteration to cleaning the methods a bit

This commit is contained in:
Dave M. 2026-07-01 14:37:07 -04:00
parent 71dda03c3e
commit cde7a749c0
2 changed files with 23 additions and 49 deletions

View File

@ -16,28 +16,17 @@ class I18n implements MissingKeyInterface
protected string $currentLanguage = ""; protected string $currentLanguage = "";
protected ? Reader\ReaderInterface $dataReader;
protected ? Reader\ReaderInterface $cacheReader = null;
protected ? MissingKeyInterface $missingKey = null;
public function __construct( public function __construct(
? Reader\ReaderInterface $dataReader = null, protected ? Reader\ReaderInterface $dataReader,
? Reader\ReaderInterface $cacheReader = null, protected ? Reader\ReaderInterface $cacheReader = null,
? MissingKeyInterface $missingKey = null protected ? MissingKeyInterface $missingKey = null,
) ) { }
{
$this->dataReader = $dataReader;
$this->cacheReader = $cacheReader;
$this->missingKey = $missingKey;
}
public function initialize(bool $useCache) : bool public function initialize(bool $readFromCache) : bool
{ {
$loaded = $useCache && $this->fromReader($this->cacheReader) ?: $this->fromReader($this->dataReader); $loaded = $readFromCache && $this->fromReader($this->cacheReader) ?: $this->fromReader($this->dataReader);
if ( ! $useCache ) { if ( ! $readFromCache ) {
$this->cacheReader->save($this->locale, $this->data); $this->cacheReader->save($this->locale, $this->data);
} }
@ -68,19 +57,9 @@ class I18n implements MissingKeyInterface
return $string; return $string;
} }
public function set(string $key, $value) : mixed public function set(string $key, $value) : void
{ {
$cache = explode(static::DELIMITER, $key); Iterate::arraySet($this->data, $key, $value, static::DELIMITER);
while ( ! empty($cache) ) {
$imp = implode(static::DELIMITER, $cache);
array_pop($cache);
unset( $this->cache[$imp] );
}
$this->cache[$key] = $value;
return Iterate::arraySet($this->data, $key, $value, static::DELIMITER);
} }
public function locale(? string $set = null) public function locale(? string $set = null)

View File

@ -13,14 +13,14 @@ class Iterate
* @param string $path - path like 'person.name.first' * @param string $path - path like 'person.name.first'
* @param string $value - value to set * @param string $value - value to set
*/ */
public static function arraySet(?array &$array, string $path, $value, string $delimiter = '.') { public static function arraySet(?array &$array, string $path, $value, string $delimiter = '.') : void
{
$pathArr = explode($delimiter, $path); $pathArr = explode($delimiter, $path);
// Go to next node // Go to next node
if ( isset($pathArr[1])) { if ( isset($pathArr[1])) {
static::arraySet($array[ array_shift($pathArr) ], implode($delimiter, $pathArr), $value); static::arraySet($array[ array_shift($pathArr) ], implode($delimiter, $pathArr), $value);
} }
// We are at the end of the path, set value
else { else {
$array[ $pathArr[0] ] = $value; $array[ $pathArr[0] ] = $value;
} }
@ -32,21 +32,17 @@ class Iterate
if ($key_list) { if ($key_list) {
return static::arrayKeysSet($key_list, $array[$current_key], $value, $append); return static::arrayKeysSet($key_list, $array[$current_key], $value, $append);
} }
else { elseif ( $append && isset($array[$current_key]) ) {
if ( $append && isset($array[$current_key]) ) { if (is_array($array[$current_key])) {
if (is_array($array[$current_key])) { return $array[$current_key] = array_merge_recursive($value, $array[$current_key]); #: array_replace_recursive($value, $array[$current_key]);
return $array[$current_key] = array_merge_recursive($value, $array[$current_key]); #: array_replace_recursive($value, $array[$current_key]);
}
else {
return $array[$current_key] = $array[$current_key] . $value;
}
} }
else { else {
return $array[$current_key] = $value; return $array[$current_key] = $array[$current_key] . $value;
} }
} }
else {
return false; return $array[$current_key] = $value;
}
} }
# Parses $obj1.obj2.obj3.propery # Parses $obj1.obj2.obj3.propery
@ -60,8 +56,8 @@ class Iterate
return [ $object, end($pathArr) ]; return [ $object, end($pathArr) ];
} }
public static function arrayGet(iterable $array, string $path, string $delimiter = '.') { public static function arrayGet(iterable $array, array|string $path, string $delimiter = '.') : mixed {
$pathArr = explode($delimiter, $path); $pathArr = is_array($path) ? $path : explode($delimiter, $path);
if (isset($array[$pathArr[0]])) { if (isset($array[$pathArr[0]])) {
if ( isset($pathArr[1]) ) { if ( isset($pathArr[1]) ) {
@ -81,15 +77,14 @@ class Iterate
} }
} }
return static::arrayGet($recurse, implode($delimiter, $pathArr), $delimiter); return static::arrayGet($recurse, $pathArr);
} }
else { else {
return $array[$pathArr[0]]; return $array[$pathArr[0]];
} }
} }
else {
return null; return null;
}
} }
public static function splitKeys(array &$array, string $delimiter = ".", string $enclosed = '{}') { public static function splitKeys(array &$array, string $delimiter = ".", string $enclosed = '{}') {