Skip to content

toDict()

BaseModel::toDict() returns the model's public properties as an array, so a hydrated model can be serialized back to plain data.

<?php
namespace Attributes\Strictify;

abstract class BaseModel
{
    public function toDict(bool $includeSensitive = false, bool $useAlias = false): array {}
}
<?php
use function Attributes\Strictify\validate;

$user = validate($rawData, new User());
$array = $user->toDict();

Field names

By default the output is keyed by the raw PHP property names. Pass useAlias: true to key it by the resolved field names validate() expects instead: aliases and the aliasGenerator from #[ModelConfigs] are applied, and each nested model uses only its own generator (inherited at most from its parent class, never from the enclosing model). With useAlias: true the output therefore round-trips through validate():

<?php
use Attributes\\Strictify\\Fields\\Alias;

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

$user = validate(['user_name' => 'John', 'birthday' => '1994-01-01T09:00:00+00:00'], new User());

$user->toDict();
// ['name' => 'John', 'birthday' => '1994-01-01T09:00:00+00:00']

$user->toDict(useAlias: true);
// ['user_name' => 'John', 'birthday' => '1994-01-01T09:00:00+00:00']

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

Value serialization

  • Scalars and arrays (including Sequence/Dict shapes) are returned as-is, recursing into nested elements.
  • DateTimeInterface values are formatted as ISO 8601 strings (Y-m-d\TH:i:sP).
  • Nested BaseModel properties are serialized recursively.
  • Properties marked with #[Sensitive] are omitted from the output. Pass includeSensitive: true to toDict() to serialize their real values (the flag propagates into nested models and arrays). The key is the property name by default and the resolved alias with useAlias: true.
  • Uninitialized typed properties are omitted from the output.
<?php
class Address extends BaseModel
{
    public string $street;
}

class User extends BaseModel
{
    public string $name;
    public Address $address;
}

$user = validate(['name' => 'John', 'address' => ['street' => 'Main St']], new User());

$user->toDict();
// ['name' => 'John', 'address' => ['street' => 'Main St']]

DateTime constraints

When a property has a #[DateTime(format: ..., timezone: ...)] constraint, toDict() uses that format and timezone instead of the ISO 8601 default:

<?php
class Event extends BaseModel
{
    #[DateTime(format: 'Y-m-d', timezone: 'Europe/Madrid')]
    public ?DateTime $startsAt;
}

$event = validate(['startsAt' => '1994-01-01'], new Event());
$event->toDict();
// ['startsAt' => '1994-01-01'] (Madrid is UTC+1 in January, formatted as Y-m-d)

See also