dereuromark / cakephp-dto

CakePHP DTO plugin - quickly generate useful data transfer objects for your app (mutable/immutable)
MIT License
24 stars 6 forks source link

Dtos as Collections? #7

Open dereuromark opened 5 years ago

dereuromark commented 5 years ago

Do we want dtos as collections directly?

instead of

<dto name="Cars">
    <field name="cars" type="Car[]" collection="true" singular="car" />
</dto>

it could directly be

<dto name="Cars" collection="true" [collectionType="..."] type="Car[]"/>

and must not have any fields/children then.

Instead of

$carsArray = $carsDto->getCars()->toArray();

one could do

$carsArray = $carsDto->toArray();

It would also be a bit more intuitive for options array vs Options dto etc, directly being collections some of them have a very similar meaning to collection objects (ArrayObject collections etc) as they already exist in the apps.

But it would make it probably a bit confusing and more complex on the dto internal side. Maybe a different key is necessary then?

<collection name="Cars" [collectionType="..."] type="Car[]"/>

This might also go into a similar direction as https://github.com/burzum/cakephp-config-objects And it should definitely work with "associative" true/false.

asaliev commented 5 years ago

$carsArray = $carsDto->toArray();

I don't think we can rule out cases when Cars might have more than one field: a collection of Car and some other field, like type (van, sedan, etc) which describes the collection in some semantic way.

But it would make it probably a bit confusing and more complex on the dto internal side. Maybe a different key is necessary then?

I do agree though that it makes sense to introduce a new collection tag when we just want a single collection object of Car without any other fields. IMO this would generally improve QoL.

dereuromark commented 5 years ago

So the generated code would be a subtype of Dto with specific methods. The class would be NameCollection (with Collection suffix instead of Dto), correct?

dereuromark commented 5 years ago

Also, here then singular is also possible? For add...() sugar methods etc?