mirror of
https://github.com/mirego/accent.git
synced 2024-09-11 18:58:14 +03:00
Add simple php format
This commit is contained in:
parent
a09630f9b5
commit
29249620bf
@ -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")
|
||||
|
@ -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
lib/langue/formatter/simple_php/parser.ex
Normal file
24
lib/langue/formatter/simple_php/parser.ex
Normal file
@ -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
lib/langue/formatter/simple_php/php.ex
Normal file
9
lib/langue/formatter/simple_php/php.ex
Normal file
@ -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
lib/langue/formatter/simple_php/serializer.ex
Normal file
26
lib/langue/formatter/simple_php/serializer.ex
Normal file
@ -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
|
@ -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,
|
||||
|
26
test/langue/simple_php/expectation_test.exs
Normal file
26
test/langue/simple_php/expectation_test.exs
Normal file
@ -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
test/langue/simple_php/formatter_test.exs
Normal file
22
test/langue/simple_php/formatter_test.exs
Normal file
@ -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
|
Loading…
Reference in New Issue
Block a user