Skip to content

Commit

Permalink
Add simple php format
Browse files Browse the repository at this point in the history
  • Loading branch information
simonprev committed Aug 13, 2024
1 parent a09630f commit 2924962
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 8 deletions.
1 change: 1 addition & 0 deletions lib/graphql/types/document_format.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
14 changes: 6 additions & 8 deletions lib/langue/formatter/laravel_php/serializer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
"""
<?php
return #{render};
"""

@spec wrap_values(String.t()) :: String.t()
defp wrap_values(values) do
"""
<?php
return #{values};
"""
%Langue.Formatter.SerializerResult{render: render}
end
end
24 changes: 24 additions & 0 deletions lib/langue/formatter/simple_php/parser.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
defmodule Langue.Formatter.SimplePhp.Parser do
@moduledoc false
@behaviour Langue.Formatter.Parser

alias Langue.Utils.Placeholders

def parse(%{render: render}) do
entries =
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
9 changes: 9 additions & 0 deletions lib/langue/formatter/simple_php/php.ex
Original file line number Diff line number Diff line change
@@ -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
26 changes: 26 additions & 0 deletions lib/langue/formatter/simple_php/serializer.ex
Original file line number Diff line number Diff line change
@@ -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 =
"""
<?php
return #{render};
"""

%Langue.Formatter.SerializerResult{render: render}
end
end
1 change: 1 addition & 0 deletions lib/langue/langue.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ defmodule Langue do
Langue.Formatter.Json,
Langue.Formatter.Rails,
Langue.Formatter.SimpleJson,
Langue.Formatter.SimplePhp,
Langue.Formatter.Strings,
Langue.Formatter.LaravelPhp,
Langue.Formatter.GoI18nJson,
Expand Down
26 changes: 26 additions & 0 deletions test/langue/simple_php/expectation_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
defmodule LangueTest.Formatter.SimplePhp.Expectation do
@moduledoc false
alias Langue.Entry

defmodule NotNested do
@moduledoc false
use Langue.Expectation.Case

def render do
"""
<?php
return [
'a'=>'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
22 changes: 22 additions & 0 deletions test/langue/simple_php/formatter_test.exs
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 2924962

Please sign in to comment.