- 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 ? 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)

View File

@ -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,8 +32,7 @@ class Iterate
if ($key_list) {
return static::arrayKeysSet($key_list, $array[$current_key], $value, $append);
}
else {
if ( $append && isset($array[$current_key]) ) {
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]);
}
@ -46,9 +45,6 @@ class Iterate
}
}
return false;
}
# Parses $obj1.obj2.obj3.propery
protected static function objectFromPath(object $object, array $pathArr) : array
{
@ -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,16 +77,15 @@ class Iterate
}
}
return static::arrayGet($recurse, implode($delimiter, $pathArr), $delimiter);
return static::arrayGet($recurse, $pathArr);
}
else {
return $array[$pathArr[0]];
}
}
else {
return null;
}
}
public static function splitKeys(array &$array, string $delimiter = ".", string $enclosed = '{}') {
if ( is_array($array) ) {