Appearance
labmin/materialize
Database-neutral object mapper (^0.2) that casts associative arrays into typed PHP DTOs, runs attribute-based validation, normalizes objects back to arrays, and can project DTOs to TypeScript.
bash
composer require labmin/materialize:^0.2Core Static Facade (Labmin\Materialize\Materialize)
| Method | Behavior |
|---|---|
Materialize::build(array $data, string $class, array $fieldMap = []): object | Casts and validates $data into $class. Throws ValidationException containing every violation found. |
Materialize::tryBuild(array $data, string $class, array $fieldMap = []): BuildResult | Non-throwing variant returning BuildResult ($result->ok, $result->isSuccessful(), $result->value, $result->errors). Ideal for manual/dynamic form validation. |
Materialize::partialBuild(array $data, string $class, array $fieldMap = [], array $required = [], ?array $only = null): object | Hydrates only supplied keys (leaving omitted properties uninitialized) for partial updates. Also has tryPartialBuild(). |
Materialize::validate(object $object): void | Validates an existing hydrated object (including nested objects and class constraints). |
Materialize::normalize(object $object): array | Converts a mapped object back to a canonical associative array. |
For strict unknown-key rejection or custom casters/validators, build a configured ObjectMapper::defaults()->rejectingUnknownKeys()->withValidator(...).
Mapping Attributes (Labmin\Materialize\ObjectMapping\Attributes\)
| Attribute | Purpose |
|---|---|
#[DataObject] | Marks a class for mapping (also implied by MaterializeSQL's #[Table]). Every public, typed, non-static property is mapped automatically. |
#[Ignore] | Excludes a public property from hydration, validation, and normalization. |
#[ArrayOf(ArrayElementType::INTEGER | MyDTO::class)] | Required on every array property to specify its runtime element type (INTEGER, FLOAT, STRING, BOOLEAN, ARRAY, MIXED, or a class name). |
#[DateFormat(input: 'Y-m-d', output: DATE_ATOM)] | Enforces exact input parsing format and/or normalized output format for DateTimeImmutable properties. |
#[Discriminator(key: 'kind', map: ['a' => ClassA::class])] | Polymorphic mapping for abstract/interface nested objects or typed object arrays. |
Required & Nullability Rules
Requiredness comes directly from the PHP property declaration:
| PHP Declaration | Missing key | Explicit null |
|---|---|---|
public int $x; | required violation | not_null violation |
public ?int $x; | required violation | Allowed |
public int $x = 10; | Uses 10 | not_null violation |
public ?int $x = null; | Uses null | Allowed |
Built-in Validation Constraints (...\Validation\Constraints\)
Constraints run after casting, skip null, and accept an optional custom message: parameter:
| Constraint | Target | Options |
|---|---|---|
#[NotBlank] | Property | Rejects empty/whitespace-only strings. |
#[Length(min:, max:)] | Property | String length bounds. |
#[Range(min:, max:)] | Property | Numeric bounds. |
#[Email] | Property | Valid email address. |
#[Regex('/.../')] | Property | Pattern match. |
#[Choice(['a', 'b'])] | Property | Allowed value list. |
#[Count(min:, max:)] | Property | Array length bounds. |
#[Each([new NotBlank(), new Email()])] | Property | Validates each element of a typed array (DTO.emails[2]). |
#[FieldsMatch('password', 'confirmation')] | Class | Strictly compares two properties; reports violation on the second. |