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

[Feature] introduce an of() method #15

Open
phh opened this issue Mar 30, 2024 · 3 comments
Open

[Feature] introduce an of() method #15

phh opened this issue Mar 30, 2024 · 3 comments

Comments

@phh
Copy link

phh commented Mar 30, 2024

Seeing this example:

$users = type($users)->as(Collection::class);

Really wants me to append an ->of(User::class) to make sure a Collection<User> is returned.

This could be also be used for arrays:
type($users)->asArray()->of(User::class)

@pascalbaljet
Copy link
Collaborator

I've been experimenting with a dedicated IterableType class. We could resolve it similar to not(), but that might be a bit verbose:

type($variable)->iterable()->asArray()->of(User::class);

Alternatively we could introduce a function that returns an IterableType instance:

iterable($variable)->asArray()->of(User::class);
iterable($variable)->as(Collection::class)->of(User::class);

@phh
Copy link
Author

phh commented Apr 2, 2024

How would you suggest dealing with the key?

@pascalbaljet
Copy link
Collaborator

Maybe something like this:

iterable($variable)->as(Collection::class)->of(User::class, 'int');

// or...

iterable($variable)->as(Collection::class)->key('int')->value(User::class);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants