Collection

If you have an array of objects of a certain class, then you must specify the ConvertArray attribute for it, passing it to which class you need to bring the elements.

You can also specify a class in PHP DOC, but then you need to write the full path to this class array<\DTO\ProductDTO>. This is done in order to know exactly which instance you need to create — since Reflection does not provide out-of-the-box functions for getting the use * file. Besides use *, you can specify an alias, and it will be more difficult to trace it.

Example:

class ProductDTO
{
public int $id;
public string $name;
}
class PurchaseDTO
{
#[ConvertArray(ProductDTO::class)]
public array $products;
}
$data = [
'products' => [
['id' => 1, 'name' => 'phone'],
['id' => 2, 'name' => 'bread'],
],
];
$purchaseDTO = Hydrator::init()->create(PurchaseDTO::class, $data);