From cde7a749c04030e31c8b2278a72f6d29d430f157 Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Wed, 1 Jul 2026 14:37:07 -0400 Subject: [PATCH] - Worked on iteration to cleaning the methods a bit --- src/I18n.php | 39 +++++++++------------------------------ src/Iterate.php | 33 ++++++++++++++------------------- 2 files changed, 23 insertions(+), 49 deletions(-) diff --git a/src/I18n.php b/src/I18n.php index e7dfed4..b21f299 100644 --- a/src/I18n.php +++ b/src/I18n.php @@ -16,28 +16,17 @@ class I18n implements MissingKeyInterface protected string $currentLanguage = ""; - protected ? Reader\ReaderInterface $dataReader; - - protected ? Reader\ReaderInterface $cacheReader = null; - - protected ? MissingKeyInterface $missingKey = null; - public function __construct( - ? Reader\ReaderInterface $dataReader = null, - ? Reader\ReaderInterface $cacheReader = null, - ? MissingKeyInterface $missingKey = null - ) - { - $this->dataReader = $dataReader; - $this->cacheReader = $cacheReader; - $this->missingKey = $missingKey; - } + protected ? Reader\ReaderInterface $dataReader, + protected ? Reader\ReaderInterface $cacheReader = null, + protected ? MissingKeyInterface $missingKey = null, + ) { } - 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); } @@ -68,19 +57,9 @@ class I18n implements MissingKeyInterface return $string; } - public function set(string $key, $value) : mixed + public function set(string $key, $value) : void { - $cache = explode(static::DELIMITER, $key); - - 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); + Iterate::arraySet($this->data, $key, $value, static::DELIMITER); } public function locale(? string $set = null) diff --git a/src/Iterate.php b/src/Iterate.php index 74540eb..f6f1fd8 100644 --- a/src/Iterate.php +++ b/src/Iterate.php @@ -13,14 +13,14 @@ class Iterate * @param string $path - path like 'person.name.first' * @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); // Go to next node if ( isset($pathArr[1])) { static::arraySet($array[ array_shift($pathArr) ], implode($delimiter, $pathArr), $value); } - // We are at the end of the path, set value else { $array[ $pathArr[0] ] = $value; } @@ -32,21 +32,17 @@ class Iterate if ($key_list) { return static::arrayKeysSet($key_list, $array[$current_key], $value, $append); } - else { - if ( $append && isset($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]); - } - else { - return $array[$current_key] = $array[$current_key] . $value; - } + elseif ( $append && isset($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]); } else { - return $array[$current_key] = $value; + return $array[$current_key] = $array[$current_key] . $value; } } - - return false; + else { + return $array[$current_key] = $value; + } } # Parses $obj1.obj2.obj3.propery @@ -60,8 +56,8 @@ class Iterate return [ $object, end($pathArr) ]; } - public static function arrayGet(iterable $array, string $path, string $delimiter = '.') { - $pathArr = explode($delimiter, $path); + public static function arrayGet(iterable $array, array|string $path, string $delimiter = '.') : mixed { + $pathArr = is_array($path) ? $path : explode($delimiter, $path); if (isset($array[$pathArr[0]])) { if ( isset($pathArr[1]) ) { @@ -81,15 +77,14 @@ class Iterate } } - return static::arrayGet($recurse, implode($delimiter, $pathArr), $delimiter); + return static::arrayGet($recurse, $pathArr); } else { return $array[$pathArr[0]]; } } - else { - return null; - } + + return null; } public static function splitKeys(array &$array, string $delimiter = ".", string $enclosed = '{}') {