phex v1.0.0-rc.0 Phex View Source
A PHP serialized decoder and encoder.
Installation
The package can be installed by adding phex
to your list of dependencies in mix.exs
:
def deps do
[
{:phex, "~> 1.0.0-rc.0"}
]
end
Basic Usage
iex> Phex.encode!(%{"age" => 42, "name" => "John Doe"})
"a:2:{s:3:\"age\";i:42;s:4:\"name\";s:8:\"John Doe\";}"
iex> Phex.decode!("a:2:{s:3:\"age\";i:42;s:4:\"name\";s:8:\"John Doe\";}")
%{"age" => 42, "name" => "John Doe"}
PHP arrays and key-collisions
The way in which PHP handles array keys is a bit peculiar, the details can be found here.
Phex mimics this an will cast values in the manner described here.
iex> %{"1" => "a", 1.1 => "b"}
...> |> Phex.encode!()
...> |> Phex.decode!()
** (Phex.EncodeError) Duplicate key: 1
iex> %{"1" => "1"}
...> |> Phex.encode!()
...> |> Phex.decode!()
%{1 => "1"}
iex> %{1.1 => "1"}
...> |> Phex.encode!()
...> |> Phex.decode!()
%{1 => "1"}
iex> %{nil => "1"}
...> |> Phex.encode!()
...> |> Phex.decode!()
%{"" => "1"}
iex> %{true => "1"}
...> |> Phex.encode!()
...> |> Phex.decode!()
%{1 => "1"}
Link to this section Summary
Functions
Parses a PHP-Serialized value from a binary.
Parses a PHP-Serialized value from a binary.
Creates a PHP-Serialized binary from a term.
Creates a PHP-Serialized binary from a term.
Link to this section Types
Link to this type
array() View Source
Link to this type
decode_opts()
View Source
decode_opts()
View Source
decode_opts() :: :naive | :strict
decode_opts() :: :naive | :strict
Link to this type
key() View Source
Link to this section Functions
Link to this function
decode(binary, opt \\ :naive) View Source
Parses a PHP-Serialized value from a binary.
Examples
iex> decode("N;")
{:ok, nil, ""}
iex> decode("b:0;")
{:ok, false, ""}
iex> decode("i:-212;")
{:ok, -212, ""}
iex> decode("d:6.62607004;")
{:ok, 6.62607004, ""}
iex> decode("s:5:\"World\";")
{:ok, "World", ""}
iex> decode("a:2:{s:6:\"Bunker\";s:4:\"Blue\";s:7:\"Buscemi\";s:4:\"Pink\";}")
{:ok, %{"Bunker" => "Blue", "Buscemi" => "Pink"}, ""}
iex> decode("a:2:{s:6:\"Bunker\";s:4:\"Blue\";s:7:\"Buscemi\";s:4:\"Pink\";}", arrays: :strict)
{:ok, [{"Bunker", "Blue"}, {"Buscemi", "Pink"}], ""}
Link to this function
decode!(binary, opt \\ :naive) View Source
Parses a PHP-Serialized value from a binary.
Like decode/2
but will unwrap the error tuple and raise
in case of errors.
Examples
iex> Phex.decode!("a:0:{}")
%{}
iex> Phex.decode!("foo")
** (Phex.DecodeError) unable to decode
Link to this function
encode(term) View Source
Creates a PHP-Serialized binary from a term.
Examples
iex> encode(nil)
{:ok, "N;"}
iex> encode(true)
{:ok, "b:1;"}
iex> encode(9000)
{:ok, "i:9000;"}
iex> encode(3.14159265359)
{:ok, "d:3.14159265359;"}
iex> encode("Hello")
{:ok, "s:5:\"Hello\";"}
iex> encode(%{"Blue" => 2, "Pink" => 4})
{:ok, "a:2:{s:4:\"Blue\";i:2;s:4:\"Pink\";i:4;}"}
Link to this function
encode!(term) View Source
Creates a PHP-Serialized binary from a term.
Like encode/2
but raises a in case of errors.
Examples
iex> encode!(%{"Blue" => 2, "Pink" => 4})
"a:2:{s:4:\"Blue\";i:2;s:4:\"Pink\";i:4;}"
iex> Phex.encode!(%{"" => 2, nil => 4})
** (Phex.EncodeError) Duplicate key: ""