# Aliases and aliasGenerator

Payloads often use a different naming convention than the PHP properties (a JSON API sending `first_name` for a `$firstName` property). Strictify resolves input keys through aliases:

1. An explicit `#[Alias('...')]` on the property always wins.
2. Otherwise, the model's `aliasGenerator` transforms the property name (`'pascal'`, `'camel'`, `'snake'`, `'kebab'`).
3. Otherwise the property name is used as-is.

## The Alias attribute

Map one input key to one property:

```php
<?php
// doctest: run
use Attributes\Strictify\BaseModel;
use Attributes\Strictify\Fields\Alias;
use function Attributes\Strictify\validate;

class User extends BaseModel
{
    #[Alias('user_name')]
    public string $name;
}

$user = validate(['user_name' => 'John'], new User());
echo $user->name, "\n"; // John
```

### Malformed alias declarations

Two alias mistakes are model-definition errors, so they throw an `InvalidBaseModelException` instead of producing a field nothing can satisfy:

- `#[Alias('')]` — an empty alias would expect the empty-string key.
- Two properties resolving to the same input key: `#[Alias('id')]` on both `$a` and `$b`, or an alias generator collapsing two names onto one key (`snake` maps `$aB` and `$a_b` to `a_b`). The message names both properties and the key.

```php
<?php
class User extends BaseModel
{
    #[Alias('id')]
    public int $userId;

    #[Alias('id')]
    public int $accountId;
}

validate(['id' => 1], new User());
// InvalidBaseModelException: Properties User::$userId and User::$accountId both resolve to the input key "id".
```

## The aliasGenerator option

Transform every property name with one `#[ModelConfigs]` declaration:

```php
<?php
// doctest: run
use Attributes\Strictify\BaseModel;
use Attributes\Strictify\ModelConfigs;
use function Attributes\Strictify\validate;

#[ModelConfigs(aliasGenerator: 'snake')]
class User extends BaseModel
{
    public string $firstName;
    public int $user2Id;
}

$user = validate(['first_name' => 'John', 'user2_id' => 123], new User());
echo $user->firstName, ' ', $user->user2Id, "\n"; // John 123
```

Names are split on case boundaries and separators, so digits stay attached (`$user2Id` → `user2_id`). Acronyms are split letter by letter: `snake` maps `$HTTPResponse` to `h_t_t_p_response`, not `http_response` — declare `#[Alias('http_response')]` when the payload uses the acronym as a word.

Resolved names are also used in **error paths**, so clients see the key they sent:

```php
<?php
// doctest: run
use Attributes\Strictify\BaseModel;
use Attributes\Strictify\Exceptions\ValidationException;
use Attributes\Strictify\Fields\Alias;
use Attributes\Strictify\ModelConfigs;
use function Attributes\Strictify\validate;

#[ModelConfigs(aliasGenerator: 'snake')]
class User extends BaseModel
{
    public string $firstName;
    #[Alias('last')]
    public string $lastName;
}

try {
    validate(['first_name' => 'John'], new User());
} catch (ValidationException $e) {
    print_r($e->getErrors()); // Array ( [last] => Array ( [0] => Field is required ) )
}
```

## Serializing back: toDict(useAlias: true)

`toDict()` is keyed by the raw property names by default. Pass `useAlias: true` to get the resolved names, so the output round-trips through `validate()`:

```php
<?php
// doctest: run
use Attributes\Strictify\BaseModel;
use Attributes\Strictify\ModelConfigs;
use function Attributes\Strictify\validate;

#[ModelConfigs(aliasGenerator: 'snake')]
class User extends BaseModel
{
    public string $firstName;
    public string $lastName;
}

$user = validate(['first_name' => 'John', 'last_name' => 'Doe'], new User());

print_r($user->toDict());               // Array ( [firstName] => John [lastName] => Doe )
print_r($user->toDict(useAlias: true)); // Array ( [first_name] => John [last_name] => Doe )

validate($user->toDict(useAlias: true), new User()); // works
```

## Nested models

A nested model uses **only its own** `aliasGenerator` (inherited at most from its parent class) — both when validating and when serializing with `useAlias: true`. It never inherits the enclosing model's generator: without one, it expects the raw property names. See [Nested models](</guides/nested-models/>) and the [ModelConfigs reference](</model-configs/>).

## See also

- [Serializing models](</guides/serializing-models/>)
- [ModelConfigs reference](</model-configs/>)
- [validate() reference](</validate/>)
