-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
115 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |