Compare commits
5
Commits
c7c955ecad
..
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cde7a749c0 | ||
|
|
71dda03c3e | ||
|
|
114d4ebe1e | ||
|
|
f92baa03c8 | ||
|
|
bc6344ad17 |
No files matched your search
+1
-1
@@ -6,7 +6,7 @@
|
|||||||
"authors": [
|
"authors": [
|
||||||
{
|
{
|
||||||
"name": "Dave Mc Nicoll",
|
"name": "Dave Mc Nicoll",
|
||||||
"email": "mcndave@gmail.com"
|
"email": "info@mcnd.ca"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"require": {},
|
"require": {},
|
||||||
|
|||||||
+19
-42
@@ -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,
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -50,48 +39,37 @@ class I18n implements MissingKeyInterface
|
|||||||
$string = $this->missingKey ? $this->missingKey->get($key) : null;
|
$string = $this->missingKey ? $this->missingKey->get($key) : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
#if (is_array($string)) {
|
|
||||||
# $string = json_encode($string);
|
|
||||||
#}
|
|
||||||
|
|
||||||
return $string && $variables ? $this->placeVariables($string, $variables) : $string;
|
return $string && $variables ? $this->placeVariables($string, $variables) : $string;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function get(string $key)
|
public function get(string $key) : mixed
|
||||||
{
|
{
|
||||||
# Testing full locale key first
|
# Testing full locale key first
|
||||||
if ( null === ( $string = Iterate::arrayGet($this->data, "{$this->locale}.$key", static::DELIMITER)) ) {
|
if ( null === ( $string = Iterate::arrayGet($this->data, "{$this->locale}.$key", static::DELIMITER)) ) {
|
||||||
# Fallback on language only
|
# Fallback on language only
|
||||||
$string = Iterate::arrayGet($this->data, "{$this->language}.$key", static::DELIMITER);
|
$string = Iterate::arrayGet($this->data, "{$this->language}.$key", static::DELIMITER);
|
||||||
|
|
||||||
|
if ($string === null && $this->missingKey) {
|
||||||
|
return $this->missingKey->get($key);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $string;
|
return $string;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function set(string $key, $value)
|
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;
|
public function locale(? string $set = null)
|
||||||
|
|
||||||
return Iterate::arraySet($this->data, $key, $value, static::DELIMITER);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function locale(?string $set = null)
|
|
||||||
{
|
{
|
||||||
$locale = $set === null ? ( $this->locale ?? null ) : $this->locale = strtolower(explode(".", $this->locale)[0]);
|
|
||||||
|
|
||||||
if ( $set !== null ) {
|
if ( $set !== null ) {
|
||||||
$this->language = explode('_', $locale)[0];
|
$this->locale = strtolower(explode(".", $set)[0]);
|
||||||
|
$this->language = explode('_', $this->locale)[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
return $locale;
|
return $this->locale;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function mergeData($data) : void
|
protected function mergeData($data) : void
|
||||||
@@ -103,9 +81,8 @@ class I18n implements MissingKeyInterface
|
|||||||
{
|
{
|
||||||
foreach($reader->pathList as $path) {
|
foreach($reader->pathList as $path) {
|
||||||
foreach(Iterate::files($path) as $item) {
|
foreach(Iterate::files($path) as $item) {
|
||||||
if ( $reader->accept($item->getExtension() )) {
|
if ( $reader->accept($item->getExtension()) ) {
|
||||||
if ( null !== ( $data = $reader->load( $item ) )) {
|
if ( null !== ( $data = $reader->load( $item ) )) {
|
||||||
|
|
||||||
if ( $reader->filenameAsKey ) {
|
if ( $reader->filenameAsKey ) {
|
||||||
# Adding folder to full key
|
# Adding folder to full key
|
||||||
$filekey = ltrim(substr($item->getPathname(), strlen($path)), DIRECTORY_SEPARATOR);
|
$filekey = ltrim(substr($item->getPathname(), strlen($path)), DIRECTORY_SEPARATOR);
|
||||||
|
|||||||
+24
-17
@@ -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,8 +32,7 @@ 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]);
|
||||||
}
|
}
|
||||||
@@ -46,11 +45,19 @@ class Iterate
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
# Parses $obj1.obj2.obj3.propery
|
||||||
|
protected static function objectFromPath(object $object, array $pathArr) : array
|
||||||
|
{
|
||||||
|
while(count($pathArr) > 1) {
|
||||||
|
$propertyName = array_shift($pathArr);
|
||||||
|
$object = $object->$propertyName;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function arrayGet(iterable $array, string $path, string $delimiter = '.') {
|
return [ $object, end($pathArr) ];
|
||||||
$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($array[$pathArr[0]])) {
|
||||||
if ( isset($pathArr[1]) ) {
|
if ( isset($pathArr[1]) ) {
|
||||||
@@ -58,27 +65,27 @@ class Iterate
|
|||||||
|
|
||||||
if ( ! is_iterable($recurse) ) {
|
if ( ! is_iterable($recurse) ) {
|
||||||
if ( is_object($recurse) ) {
|
if ( is_object($recurse) ) {
|
||||||
if ( count($pathArr) > 1 ) {
|
list($object, $method) = static::objectFromPath($recurse, $pathArr);
|
||||||
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
|
# @TODO An object caller would be a WAY better idea then this line of code
|
||||||
# Calling $obj->varname
|
# Calling $obj->varname
|
||||||
return $recurse->{$pathArr[0]} ?? "";
|
|
||||||
|
return $object->$method ?? "";
|
||||||
}
|
}
|
||||||
|
elseif (is_scalar($recurse)) {
|
||||||
|
throw new \Exception("An error occured trying to fetch a value from a scalar value using path '$path'");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return static::arrayGet($recurse, implode($delimiter, $pathArr));
|
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 = '{}') {
|
||||||
if ( is_array($array) ) {
|
if ( is_array($array) ) {
|
||||||
@@ -102,12 +109,12 @@ class Iterate
|
|||||||
$keylist = explode($delimiter, $key);
|
$keylist = explode($delimiter, $key);
|
||||||
$finalKey = array_shift($keylist);
|
$finalKey = array_shift($keylist);
|
||||||
static::arrayKeysSet($keylist, $tmp, $value);
|
static::arrayKeysSet($keylist, $tmp, $value);
|
||||||
$swap[$finalKey] = static::splitKeys($tmp, $delimiter);
|
$swap[$finalKey] = static::splitKeys($tmp, $delimiter, $enclosed);
|
||||||
|
|
||||||
unset($array[isset($oldKey) ? $oldKey : $key]);
|
unset($array[isset($oldKey) ? $oldKey : $key]);
|
||||||
}
|
}
|
||||||
else if ( is_array($value) ) {
|
else if ( is_array($value) ) {
|
||||||
$value = static::splitKeys($value, $delimiter);
|
$value = static::splitKeys($value, $delimiter, $enclosed);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -41,9 +41,9 @@ class PhpReader implements ReaderInterface {
|
|||||||
$path = $this->pathList[0];
|
$path = $this->pathList[0];
|
||||||
|
|
||||||
if ( ! file_exists($path) ) {
|
if ( ! file_exists($path) ) {
|
||||||
mkdir($path, '755', true);
|
mkdir($path, 0755, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
return (bool) file_put_contents($path . DIRECTORY_SEPARATOR . "$filepath." . $this->accept[0], "<?php return " . var_export($content, true) . ";");
|
return (bool) file_put_contents($path . DIRECTORY_SEPARATOR . "$filepath.{$this->accept[0]}", "<?php return " . var_export($content, true) . ";");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user