Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dealing with arrays of Value objects #4

Open
arrilot opened this issue Dec 7, 2020 · 3 comments
Open

Dealing with arrays of Value objects #4

arrilot opened this issue Dec 7, 2020 · 3 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@arrilot
Copy link

arrilot commented Dec 7, 2020

Hello, what if we have a jsonb column addresses with
[ {...}. {...}, {...} ]
Is there any way to cast it to
[ Address(...), Address(...), Address(...), ]
?

@danielbehrendt
Copy link

Same here, I'd also like to do that.

@jessarcher
Copy link
Owner

Hi @arrilot and @danielbehrendt, I can definitely see how that would be useful. It's currently only possible when the array of DTOs is nested within another DTO using the functionality provided by Spatie at https://github.com/spatie/data-transfer-object/#automatic-casting-of-nested-array-dtos.

To have the array as the top-level, we'd need to create an object to represent the array, and thankfully Spatie seems to have us covered there too, with their DataTransferObjectCollection class https://github.com/spatie/data-transfer-object/#working-with-collections, although I haven't needed that one myself yet so I'm not 100% sure how it works.

To get that working within the casts provided by this package, I think we'd just need to create a CastableDataTransferObjectCollection that implements Laravel's Castable interface, similar to how I've created the CastableDataTransferObject class. I don't have time to look into creating that right now, but I'd welcome a PR.

@jessarcher jessarcher added enhancement New feature or request help wanted Extra attention is needed labels Dec 7, 2020
@morrislaptop
Copy link

morrislaptop commented Jan 25, 2021

@arrilot @danielbehrendt I've ported this package for POPOs using Symfony's serializer, which can support casting objects, dates and arrays.

https://github.com/morrislaptop/laravel-castable-object

I'd love to hear if this suits your needs! Basically it works like this

namespace App\Models;

use App\Values\Address;
use Illuminate\Database\Eloquent\Model;
use Morrislaptop\Caster\Caster;

/**
 * @property Address[] $addresses
 */
class User extends Model
{
    protected $casts = [
        'addresses' => Caster::class . ':' . Address::class . '[]',
    ];
}

I haven't tried it yet, but it could also potentially work for DTO's as well by adding a DataTransferObjectNormalizer into Symfony's Serializer. https://github.com/spatie/data-transfer-object/issues/93#issuecomment-738023471

@jessarcher would love your feedback on my package as well!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

4 participants