Skip to content

Commit

Permalink
Merge pull request #58 from MohmmedAshraf/fix-json-export
Browse files Browse the repository at this point in the history
Fix Export Issue for Keys with Dots
  • Loading branch information
MohmmedAshraf authored Jan 24, 2024
2 parents 78ab399 + 9326079 commit 0765394
Showing 1 changed file with 43 additions and 2 deletions.
45 changes: 43 additions & 2 deletions src/helpers.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php

use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Support\Arr;
use Illuminate\Support\HtmlString;
use Outhebox\TranslationsUI\Models\Contributor;

Expand Down Expand Up @@ -59,14 +58,56 @@ function buildPhrasesTree($phrases, $locale): array
{
$tree = [];

/** @var \Outhebox\TranslationsUI\Models\Phrase $phrase */
foreach ($phrases as $phrase) {
Arr::set($tree[$locale][$phrase->file->file_name], $phrase->key, $phrase->value);
setArrayValue(
array: $tree[$locale][$phrase->file->file_name],
key: $phrase->key,
value: ! blank($phrase->value) ? $phrase->value : $phrase->source->value
);
}

return $tree;
}
}

if (! function_exists('setArrayValue')) {
function setArrayValue(&$array, $key, $value)
{
if (is_null($key)) {
return $array = $value;
}

$keys = preg_split('/\.(?=[^.]*[^.])/', $key);

foreach ($keys as $i => $key) {
if (blank($value)) {
dd($key, $value);
}

if (count($keys) === 1) {
break;
}

unset($keys[$i]);

if (! isset($array[$key]) || ! is_array($array[$key])) {
$array[$key] = [];
}

$array = &$array[$key];
}

$lastKey = array_shift($keys);

if (! blank($lastKey)) {
$array[$lastKey] = $value;
}

return $array;
}
}

if (! function_exists('currentUser')) {
function currentUser(): null|Authenticatable|Contributor
{
Expand Down

0 comments on commit 0765394

Please sign in to comment.