Skip to content

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.2

Core Static Facade (Labmin\Materialize\Materialize) ​

MethodBehavior
Materialize::build(array $data, string $class, array $fieldMap = []): objectCasts and validates $data into $class. Throws ValidationException containing every violation found.
Materialize::tryBuild(array $data, string $class, array $fieldMap = []): BuildResultNon-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): objectHydrates only supplied keys (leaving omitted properties uninitialized) for partial updates. Also has tryPartialBuild().
Materialize::validate(object $object): voidValidates an existing hydrated object (including nested objects and class constraints).
Materialize::normalize(object $object): arrayConverts 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\) ​

AttributePurpose
#[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 DeclarationMissing keyExplicit null
public int $x;required violationnot_null violation
public ?int $x;required violationAllowed
public int $x = 10;Uses 10not_null violation
public ?int $x = null;Uses nullAllowed

Built-in Validation Constraints (...\Validation\Constraints\) ​

Constraints run after casting, skip null, and accept an optional custom message: parameter:

ConstraintTargetOptions
#[NotBlank]PropertyRejects empty/whitespace-only strings.
#[Length(min:, max:)]PropertyString length bounds.
#[Range(min:, max:)]PropertyNumeric bounds.
#[Email]PropertyValid email address.
#[Regex('/.../')]PropertyPattern match.
#[Choice(['a', 'b'])]PropertyAllowed value list.
#[Count(min:, max:)]PropertyArray length bounds.
#[Each([new NotBlank(), new Email()])]PropertyValidates each element of a typed array (DTO.emails[2]).
#[FieldsMatch('password', 'confirmation')]ClassStrictly compares two properties; reports violation on the second.

Labmin LIMS Module Developer Documentation