Open Source/plain-to-class/Usage/Collection
Collection
If you have an array of objects of a certain class, then you must specify the ConvertArray attribute to it, passing it to which class you need to cast the elements.
It is also possible to specify the 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 UserDTO{ public int $id; public string $email; public string $balance;}
class PurchaseDTO{ #[ConvertArray(ProductDTO::class)] public array $products;
/** @var UserDTO $user */ public UserDTO $user;}
$data = [ 'products' => [ ['id' => 1, 'name' => 'phone'], ['id' => 2, 'name' => 'bread'], ], 'user' => ['id' => 1, 'email' => 'test@test.com', 'balance' => 10012.23],];$purchaseDTO = ClassTransformer::transform(PurchaseDTO::class, $data);var_dump($purchaseDTO);object(PurchaseDTO)[345] public array 'products' => array (size=2) 0 => object(ProductDTO)[1558] public int 'id' => int 1 public string 'name' => string 'phone' (length=5) 1 => object(ProductDTO)[1563] public int 'id' => int 2 public string 'name' => string 'bread' (length=5) public UserDTO 'user' => object(UserDTO)[1559] public int 'id' => int 1 public string 'email' => string 'test@test.com' (length=13) public float 'balance' => float 10012.23