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

feat/add-deep-mapping-for-mapOf #2

Merged
merged 3 commits into from
Oct 21, 2024

Conversation

zero-to-prod
Copy link
Owner

Description

Deep Mapping

You can set the level for mapping deep arrays.

class User
{
    use \Zerotoprod\DataModel\DataModel;
    use \Zerotoprod\DataModelHelper\DataModelHelper;
    
    /** @var Alias[] $Aliases */
    #[Describe([
        'cast' => [self::class, 'mapOf'],   // Use the mapOf helper method
        'type' => Alias::class,             // Target type for each item
        'level' => 2,                       // The dimension of the array. Defaults to 1.
    ])]
    public array $Aliases;
}

class Alias
{
    use \Zerotoprod\DataModel\DataModel;
    
    public string $name;
}

$User = User::from([
    'Aliases' => [
        [
            ['name' => 'John Doe'],
            ['name' => 'John Smith'],
        ]
    ]
]);

echo $User->Aliases[0][0]->name; // Outputs: John Doe
echo $User->Aliases[0][1]->name; // Outputs: John Smith

@zero-to-prod zero-to-prod self-assigned this Oct 21, 2024
@zero-to-prod zero-to-prod added the enhancement New feature or request label Oct 21, 2024
@zero-to-prod zero-to-prod merged commit 63ecbae into main Oct 21, 2024
3 checks passed
@zero-to-prod zero-to-prod deleted the feat/add-deep-mapping-for-mapOf branch October 21, 2024 14:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant