Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 709f6c50b5 | |||
| 5cfc3f5fa2 | |||
| ce90960649 | |||
| e7e6bbda29 | |||
| 6ec94020ef | |||
| dc1b802c77 |
@@ -1,6 +1,11 @@
|
|||||||
defmodule IbanEx.Commons do
|
defmodule IbanEx.Commons do
|
||||||
@moduledoc false
|
@moduledoc false
|
||||||
|
|
||||||
|
@spec blank(nil | binary()) :: nil | binary()
|
||||||
|
def blank(nil), do: nil
|
||||||
|
def blank(""), do: nil
|
||||||
|
def blank(string) when is_binary(string), do: string
|
||||||
|
|
||||||
@spec normalize(binary()) :: binary()
|
@spec normalize(binary()) :: binary()
|
||||||
def normalize(string) do
|
def normalize(string) do
|
||||||
string
|
string
|
||||||
|
|||||||
@@ -9,6 +9,10 @@ defmodule IbanEx.Country.Template do
|
|||||||
|
|
||||||
@callback size() :: size()
|
@callback size() :: size()
|
||||||
@callback rule() :: rule()
|
@callback rule() :: rule()
|
||||||
|
@callback rules() :: []
|
||||||
|
@callback rules_map() :: %{}
|
||||||
|
@callback bban_fields() :: [atom()]
|
||||||
|
@callback bban_size() :: non_neg_integer()
|
||||||
@callback to_string(Iban.t(), joiner()) :: String.t()
|
@callback to_string(Iban.t(), joiner()) :: String.t()
|
||||||
@callback to_string(Iban.t()) :: String.t()
|
@callback to_string(Iban.t()) :: String.t()
|
||||||
|
|
||||||
@@ -39,10 +43,56 @@ def to_string(
|
|||||||
@spec size() :: integer()
|
@spec size() :: integer()
|
||||||
def size(), do: @size
|
def size(), do: @size
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Return Regex for parsing complete BBAN (part of IBAN string)
|
||||||
|
"""
|
||||||
@impl IbanEx.Country.Template
|
@impl IbanEx.Country.Template
|
||||||
@spec rule() :: Regex.t()
|
@spec rule() :: Regex.t()
|
||||||
def rule(), do: @rule
|
def rule(), do: @rule
|
||||||
|
|
||||||
|
@impl IbanEx.Country.Template
|
||||||
|
@spec bban_size() :: integer()
|
||||||
|
def bban_size() do
|
||||||
|
{_rules, bban_size} = calculate_rules()
|
||||||
|
bban_size
|
||||||
|
end
|
||||||
|
|
||||||
|
@impl IbanEx.Country.Template
|
||||||
|
@spec bban_fields() :: []
|
||||||
|
def bban_fields(), do: rules_map() |> Map.keys()
|
||||||
|
|
||||||
|
@impl IbanEx.Country.Template
|
||||||
|
@spec rules_map() :: %{}
|
||||||
|
def rules_map(), do: rules() |> Map.new()
|
||||||
|
|
||||||
|
@impl IbanEx.Country.Template
|
||||||
|
@spec rules() :: []
|
||||||
|
def rules() do
|
||||||
|
{rules, _bban_size} = calculate_rules()
|
||||||
|
rules
|
||||||
|
end
|
||||||
|
|
||||||
|
defp calculate_rules() do
|
||||||
|
scanner = ~r/\(\?\<([\w_]+)\>(([^{]+)\{(\d+)\})\)/i
|
||||||
|
|
||||||
|
source =
|
||||||
|
@rule
|
||||||
|
|> Regex.source()
|
||||||
|
|
||||||
|
{list, bban_length} =
|
||||||
|
Regex.scan(scanner, source)
|
||||||
|
|> Enum.reduce({[], 0}, fn [_part, k, r, _syms, l], {list, position} = acc ->
|
||||||
|
key = String.to_atom(k)
|
||||||
|
{:ok, regex} = Regex.compile(r, "i")
|
||||||
|
length = String.to_integer(l)
|
||||||
|
left = position
|
||||||
|
right = left + length - 1
|
||||||
|
{[{key, %{regex: regex, range: left..right}} | list], right + 1}
|
||||||
|
end)
|
||||||
|
|
||||||
|
{Enum.reverse(list), bban_length}
|
||||||
|
end
|
||||||
|
|
||||||
defoverridable to_string: 1, to_string: 2, size: 0, rule: 0
|
defoverridable to_string: 1, to_string: 2, size: 0, rule: 0
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -20,8 +20,9 @@ defmodule IbanEx.Parser do
|
|||||||
@spec parse({:ok, binary()}) :: iban_or_error()
|
@spec parse({:ok, binary()}) :: iban_or_error()
|
||||||
def parse({:ok, iban_string}), do: parse(iban_string)
|
def parse({:ok, iban_string}), do: parse(iban_string)
|
||||||
|
|
||||||
@spec parse(binary()) :: iban_or_error()
|
def parse(iban_string, options \\ [incomplete: false])
|
||||||
def parse(iban_string) do
|
|
||||||
|
def parse(iban_string, incomplete: false) do
|
||||||
case Validator.validate(iban_string) do
|
case Validator.validate(iban_string) do
|
||||||
{:ok, valid_iban} ->
|
{:ok, valid_iban} ->
|
||||||
iban_map = %{
|
iban_map = %{
|
||||||
@@ -41,12 +42,69 @@ def parse(iban_string) do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def parse(iban_string, incomplete: true) do
|
||||||
|
iban_map = %{
|
||||||
|
country_code: country_code(iban_string),
|
||||||
|
check_digits: check_digits(iban_string)
|
||||||
|
}
|
||||||
|
|
||||||
|
bban = bban(iban_string)
|
||||||
|
|
||||||
|
case Country.is_country_code_supported?(iban_map.country_code) do
|
||||||
|
true ->
|
||||||
|
result =
|
||||||
|
parse_bban(bban, iban_map.country_code, incomplete: true)
|
||||||
|
|> Map.merge(iban_map)
|
||||||
|
|
||||||
|
{:ok, struct(Iban, result)}
|
||||||
|
|
||||||
|
false ->
|
||||||
|
{:error, :unsupported_country_code}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
@spec parse_bban(binary(), <<_::16>>) :: map()
|
@spec parse_bban(binary(), <<_::16>>) :: map()
|
||||||
def parse_bban(bban_string, country_code) do
|
def parse_bban(bban_string, country_code, options \\ [incomplete: false])
|
||||||
regex = Country.country_module(country_code).rule()
|
|
||||||
for {key, val} <- Regex.named_captures(regex, bban_string),
|
def parse_bban(bban_string, country_code, incomplete: true) do
|
||||||
|
case Country.is_country_code_supported?(country_code) do
|
||||||
|
true ->
|
||||||
|
parse_bban_by_rules(bban_string, Country.country_module(country_code))
|
||||||
|
|
||||||
|
false ->
|
||||||
|
%{}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def parse_bban(bban_string, country_code, incomplete: false) do
|
||||||
|
case Country.is_country_code_supported?(country_code) do
|
||||||
|
true ->
|
||||||
|
Country.country_module(country_code).rule()
|
||||||
|
|> parse_bban_by_regex(bban_string)
|
||||||
|
|
||||||
|
false ->
|
||||||
|
%{}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp parse_bban_by_rules(bban_string, country_module) do
|
||||||
|
for {field, rule} <- country_module.rules,
|
||||||
into: %{},
|
into: %{},
|
||||||
do: {String.to_atom(key), val}
|
do: {field, normalize_and_slice(bban_string, rule.range)}
|
||||||
|
end
|
||||||
|
|
||||||
|
defp parse_bban_by_regex(_regex, nil), do: %{}
|
||||||
|
|
||||||
|
defp parse_bban_by_regex(regex, bban_string) do
|
||||||
|
case Regex.named_captures(regex, bban_string) do
|
||||||
|
map when is_map(map) ->
|
||||||
|
for {key, val} <- map,
|
||||||
|
into: %{},
|
||||||
|
do: {String.to_atom(key), val}
|
||||||
|
|
||||||
|
nil ->
|
||||||
|
%{}
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@spec country_code(iban_string()) :: country_code_string()
|
@spec country_code(iban_string()) :: country_code_string()
|
||||||
|
|||||||
@@ -123,7 +123,7 @@ def iban_violates_country_rule?(iban) do
|
|||||||
rule <- country_module.rule() do
|
rule <- country_module.rule() do
|
||||||
!Regex.match?(rule, bban)
|
!Regex.match?(rule, bban)
|
||||||
else
|
else
|
||||||
{:error, _error} -> true
|
_ -> true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user