66
77use Elegantly \Translator \Drivers \PhpDriver ;
88use Illuminate \Support \Arr ;
9- use Illuminate \Support \Collection ;
109use Illuminate \Support \Enumerable ;
1110use Illuminate \Support \Str ;
1211
@@ -177,31 +176,29 @@ public function only($keys)
177176 */
178177 public static function toDot (array $ values ): static
179178 {
180- $ translations = collect ($ values )
181- ->mapWithKeys (fn ($ value , $ key ) => [static ::prepareTranslations ($ key ) => static ::prepareTranslations ($ value )])
182- ->dot ();
183-
184- return new static ($ translations );
179+ return new static (
180+ Arr::dot (static ::prepareTranslations ($ values ))
181+ );
185182 }
186183
187184 /**
188- * @param array<array-key, mixed>|Translations $translations
189- * @return Collection<string , mixed>
185+ * @param Translations| array<array-key, mixed> $translations
186+ * @return array<array-key , mixed> $values
190187 */
191- public static function toUndot (Translations |array $ translations ): Collection
188+ public static function toUndot (Translations |array $ translations ): array
192189 {
193- return collect ( $ translations)
194- -> undot ()
195- -> mapWithKeys ( fn ( $ value , $ key ) => [ static ::unprepareTranslations ($ key ) => static :: unprepareTranslations ( $ value )] );
190+ $ translations = $ translations instanceof Translations ? $ translations -> all () : $ translations ;
191+
192+ return static ::unprepareTranslations (Arr:: undot (( array ) $ translations ) );
196193 }
197194
198195 /**
199196 * Dot in translations keys might break the initial array structure
200197 * To prevent that, we encode the dots in unicode
201198 */
202- public static function prepareTranslations (mixed $ values ): mixed
199+ public static function prepareTranslations (mixed $ values, bool $ escape = false ): mixed
203200 {
204- if (is_string ($ values )) {
201+ if ($ escape && is_string ($ values )) {
205202 return Str::replace ('. ' , '. ' , $ values );
206203 }
207204
@@ -215,7 +212,7 @@ public static function prepareTranslations(mixed $values): mixed
215212
216213 return collect ($ values )
217214 ->mapWithKeys (fn ($ value , $ key ) => [
218- static ::prepareTranslations ($ key ) => static ::prepareTranslations ($ value ),
215+ static ::prepareTranslations ($ key, true ) => static ::prepareTranslations ($ value ),
219216 ])
220217 ->all ();
221218 }
@@ -224,9 +221,9 @@ public static function prepareTranslations(mixed $values): mixed
224221 * Dot in translations keys might break the initial array structure
225222 * To prevent that, we encode the dots in unicode
226223 */
227- public static function unprepareTranslations (mixed $ values ): mixed
224+ public static function unprepareTranslations (mixed $ values, bool $ unescape = false ): mixed
228225 {
229- if (is_string ($ values )) {
226+ if ($ unescape && is_string ($ values )) {
230227 return Str::replace ('. ' , '. ' , $ values );
231228 }
232229
@@ -240,7 +237,7 @@ public static function unprepareTranslations(mixed $values): mixed
240237
241238 return collect ($ values )
242239 ->mapWithKeys (fn ($ value , $ key ) => [
243- static ::unprepareTranslations ($ key ) => static ::unprepareTranslations ($ value ),
240+ static ::unprepareTranslations ($ key, true ) => static ::unprepareTranslations ($ value ),
244241 ])
245242 ->all ();
246243 }
0 commit comments