- Added array return type if key is incomplete
This commit is contained in:
parent
026126be76
commit
86603044c0
16
src/I18n.php
16
src/I18n.php
@ -44,16 +44,16 @@ class I18n implements MissingKeyInterface
|
||||
return $loaded;
|
||||
}
|
||||
|
||||
public function fromKey(string $key, array $variables) : ?string
|
||||
public function fromKey(string $key, array $variables) # : ?string|array
|
||||
{
|
||||
if ( null === ( $string = $this->get($key) ) ) {
|
||||
$string = $this->missingKey ? $this->missingKey->get($key) : null;
|
||||
$string = $this->missingKey ? $this->missingKey->get($key) : null;
|
||||
}
|
||||
|
||||
if (is_array($string)) {
|
||||
$string = json_encode($string);
|
||||
}
|
||||
|
||||
|
||||
#if (is_array($string)) {
|
||||
# $string = json_encode($string);
|
||||
#}
|
||||
|
||||
return $string && $variables ? $this->placeVariables($string, $variables) : $string;
|
||||
}
|
||||
|
||||
@ -130,7 +130,7 @@ class I18n implements MissingKeyInterface
|
||||
}
|
||||
|
||||
protected function placeVariables(string $string, array $parameters) {
|
||||
if ( preg_match_all('~{$(.*?)}~si', $string, $matches, PREG_SET_ORDER) ) {
|
||||
if ( preg_match_all('~\\{\\$(.*?)\\}~si', $string, $matches, PREG_SET_ORDER) ) {
|
||||
$search = [];
|
||||
|
||||
foreach($matches as $item) {
|
||||
|
@ -49,12 +49,27 @@ class Iterate
|
||||
return false;
|
||||
}
|
||||
|
||||
public static function arrayGet(array $array, string $path, string $delimiter = '.') {
|
||||
public static function arrayGet(iterable $array, string $path, string $delimiter = '.') {
|
||||
$pathArr = explode($delimiter, $path);
|
||||
|
||||
if (isset($array[$pathArr[0]])) {
|
||||
if ( isset($pathArr[1]) ) {
|
||||
return static::arrayGet($array[array_shift($pathArr)], implode($delimiter, $pathArr));
|
||||
$recurse = $array[array_shift($pathArr)];
|
||||
|
||||
if ( ! is_iterable($recurse) ) {
|
||||
if ( is_object($recurse) ) {
|
||||
if ( count($pathArr) > 1 ) {
|
||||
throw new \Exception("An error occured trying to fetch a value from an object using path $path");
|
||||
}
|
||||
else {
|
||||
# @TODO An object caller would be a WAY better idea then this line of code
|
||||
# Calling $obj->varname
|
||||
return $recurse->{$pathArr[0]} ?? "";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return static::arrayGet($recurse, implode($delimiter, $pathArr));
|
||||
}
|
||||
else {
|
||||
return $array[$pathArr[0]];
|
||||
|
@ -1,47 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Tell;
|
||||
|
||||
trait TellTrait {
|
||||
|
||||
public function lang(...$args)
|
||||
{
|
||||
static $vars_pattern = null;
|
||||
|
||||
$vars_pattern || ( $vars_pattern = $this->config('Eckinox.language.vars.pattern') );
|
||||
|
||||
switch ( count($args) ) {
|
||||
case 1:
|
||||
return ( $value = Language::get( $args[0] ) ) !== null ? $value : ( Eckinox::debug() ? "'{$args[0]}'" : "" );
|
||||
|
||||
case 0:
|
||||
return Language::instance();
|
||||
|
||||
case 2:
|
||||
if ( empty($args[1]) ) {
|
||||
return $this->lang($args[0]);
|
||||
}
|
||||
|
||||
/* Handling arrays with keys */
|
||||
if ( Arrayobj::array_is_associative($args[1]) ) {
|
||||
$content = $this->lang($args[0]);
|
||||
|
||||
if ( preg_match_all("~\\{\\$(.*?)\\}~si", $this->lang($args[0]), $matches, PREG_SET_ORDER) ) {
|
||||
$search = [];
|
||||
|
||||
foreach($matches as $item) {
|
||||
$search[ $item[0] ] = $a = iterate::array_get($args[1], $item[1]);
|
||||
}
|
||||
|
||||
$content = str_replace(array_keys($search), array_values($search), $content);
|
||||
}
|
||||
|
||||
return $content;
|
||||
}
|
||||
|
||||
default:
|
||||
/* Handling arrays with indexes */
|
||||
return call_user_func_array('sprintf', array_merge([ $this->lang( array_shift($args) ) ], $args));
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user