Skip to content

Latest commit

 

History

History
114 lines (85 loc) · 1.58 KB

alias.md

File metadata and controls

114 lines (85 loc) · 1.58 KB

Alias

The alias allows you rename a property.

Example

In this example, we rename the username property to login.

namespace Acme;

use Ivory\Serializer\Mapping\Annotation as Serializer;

class User
{
    /**
     * @Serializer\Alias("login")
     *
     * @var string
     */
    public $username;
}

Usage

use Acme\User;

$user = new User();
$user->username = 'GeLo';

$serialize = $serializer->serialize($user, $format);
// echo $serialize;

$deserialize = $serializer->deserialize($serialize, User::class, $format);
// $deserialize == $user

Results

JSON

{
    "login": "GeLo"
}

XML

<?xml version="1.0" encoding="UTF-8"?>
<result>
    <login>GeLo</login>
</result>

YAML

login: GeLo

CSV

login
GeLo

Definitions

If you prefer use an other definition format, the following examples are identical.

XML

<?xml version="1.0" encoding="UTF-8" ?>

<serializer
    xmlns="http://egeloen.fr/schema/ivory-serializer"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://egeloen.fr/schema/ivory-serializer
                        http://egeloen.fr/schema/ivory-serializer/mapping-1.0.xsd"
>
    <class name="Acme\User">
        <property name="username" alias="login" />
    </class>
</serializer>

YAML

Acme\User:
    properties:
        username:
            alias: login

JSON

{
    "Acme\\User": {
        "properties": {
            "username": {
                "alias": "login"
            }
        }
    }
}