diff --git a/lib/graphql/types/document_format.ex b/lib/graphql/types/document_format.ex index c2bee99cb..ed1fbd54d 100644 --- a/lib/graphql/types/document_format.ex +++ b/lib/graphql/types/document_format.ex @@ -14,6 +14,7 @@ defmodule Accent.GraphQL.Types.DocumentFormat do value(:java_properties_xml, as: "java_properties_xml") value(:csv, as: "csv") value(:laravel_php, as: "laravel_php") + value(:simple_php, as: "simple_php") value(:go_i18n_json, as: "go_i18n_json") value(:xliff_1_2, as: "xliff_1_2") value(:resx_2_0, as: "resx_2_0") diff --git a/lib/langue/formatter/laravel_php/serializer.ex b/lib/langue/formatter/laravel_php/serializer.ex index 88b9d90b6..63dc322b3 100644 --- a/lib/langue/formatter/laravel_php/serializer.ex +++ b/lib/langue/formatter/laravel_php/serializer.ex @@ -13,14 +13,12 @@ defmodule Langue.Formatter.LaravelPhp.Serializer do |> elem(1) |> PhpAssocMap.from_tuple({:spaces, 2}) - %Langue.Formatter.SerializerResult{render: wrap_values(render)} - end + render = + """ + PhpAssocMap.to_tuple() + |> Enum.with_index(1) + |> Enum.map(fn {{key, value}, index} -> + %Langue.Entry{ + key: key, + value: value, + index: index, + value_type: Langue.ValueType.parse(value) + } + end) + |> Placeholders.parse(Langue.Formatter.SimplePhp.placeholder_regex()) + + %Langue.Formatter.ParserResult{entries: entries} + end +end diff --git a/lib/langue/formatter/simple_php/php.ex b/lib/langue/formatter/simple_php/php.ex new file mode 100644 index 000000000..c439b0b70 --- /dev/null +++ b/lib/langue/formatter/simple_php/php.ex @@ -0,0 +1,9 @@ +defmodule Langue.Formatter.SimplePhp do + @moduledoc false + use Langue.Formatter, + id: "simple_php", + display_name: "Simple PHP", + extension: "php", + parser: Langue.Formatter.SimplePhp.Parser, + serializer: Langue.Formatter.SimplePhp.Serializer +end diff --git a/lib/langue/formatter/simple_php/serializer.ex b/lib/langue/formatter/simple_php/serializer.ex new file mode 100644 index 000000000..4386b8e3e --- /dev/null +++ b/lib/langue/formatter/simple_php/serializer.ex @@ -0,0 +1,26 @@ +defmodule Langue.Formatter.SimplePhp.Serializer do + @moduledoc false + @behaviour Langue.Formatter.Serializer + + alias Langue.Utils.NestedSerializerHelper + + def serialize(%{entries: entries}) do + render = + entries + |> Enum.map(fn entry -> + { + entry.key, + NestedSerializerHelper.entry_value_to_string(entry.value, entry.value_type) + } + end) + |> PhpAssocMap.from_tuple({:spaces, 2}) + + render = + """ + 'Test', + 'b.c'=>'Not nested' + ]; + """ + end + + def entries do + [ + %Entry{index: 1, key: "a", value: "Test", value_type: "string"}, + %Entry{index: 2, key: "b.c", value: "Not nested", value_type: "string"} + ] + end + end +end diff --git a/test/langue/simple_php/formatter_test.exs b/test/langue/simple_php/formatter_test.exs new file mode 100644 index 000000000..6af0d9252 --- /dev/null +++ b/test/langue/simple_php/formatter_test.exs @@ -0,0 +1,22 @@ +defmodule LangueTest.Formatter.SimplePhp do + @moduledoc false + use ExUnit.Case, async: true + + alias Langue.Formatter.SimplePhp + + Code.require_file("expectation_test.exs", __DIR__) + + @tests [ + NotNested + ] + + for test <- @tests, module = Module.concat(LangueTest.Formatter.SimplePhp.Expectation, test) do + test "Laravel Php #{test}" do + {expected_parse, result_parse} = Accent.FormatterTestHelper.test_parse(unquote(module), SimplePhp) + {expected_serialize, result_serialize} = Accent.FormatterTestHelper.test_serialize(unquote(module), SimplePhp) + + assert expected_parse == result_parse + assert expected_serialize == result_serialize + end + end +end