Skip to content
New issue

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

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

Already on GitHub? # to your account

fix: constructor with default parameter array does not work with context #185

Conversation

MrMeshok
Copy link
Contributor

@MrMeshok MrMeshok commented Sep 12, 2024

After this PR #177 behavior was fixed when not using constructor arguments context, but with context it's still uses ??.

Before:

if (\AutoMapper\MapperContext::hasConstructorArgument($context, 'ConstructorWithDefaultValues', 'someOtters')) {
    $values = [];
    foreach ($value['someOtters'] ?? [] as $key => $value) {
        $values[$key] = $value;
    }
    $constructarg = $values ?? \AutoMapper\MapperContext::getConstructorArgument($context, 'ConstructorWithDefaultValues', 'someOtters');
} else {
    $values = [];
    foreach ($value['someOtters'] ?? [] as $key => $value) {
        $values[$key] = $value;
    }
    $constructarg = count($values) > 0 ? $values : array();
}

After:

if (\AutoMapper\MapperContext::hasConstructorArgument($context, 'ConstructorWithDefaultValues', 'someOtters')) {
    $values = [];
    foreach ($value['someOtters'] ?? [] as $key => $value) {
        $values[$key] = $value;
    }
    $constructarg = count($values) > 0 ? $values : \AutoMapper\MapperContext::getConstructorArgument($context, 'ConstructorWithDefaultValues', 'someOtters');
} else {
    $values = [];
    foreach ($value['someOtters'] ?? [] as $key => $value) {
        $values[$key] = $value;
    }
    $constructarg = count($values) > 0 ? $values : array();
}

@MrMeshok MrMeshok force-pushed the fix/default-array-value-in-construct-with-context branch from d9b5de9 to 0a7abf0 Compare September 13, 2024 07:19
Copy link
Member

@Korbeil Korbeil left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, just a quick fix in the test 🙏

tests/AutoMapperTest.php Show resolved Hide resolved
@MrMeshok MrMeshok force-pushed the fix/default-array-value-in-construct-with-context branch from 0a7abf0 to 52dc238 Compare September 13, 2024 11:58
@Korbeil Korbeil merged commit 76e25b5 into jolicode:main Sep 13, 2024
5 checks passed
@Korbeil
Copy link
Member

Korbeil commented Sep 13, 2024

Thanks for your contribution @MrMeshok !

@MrMeshok MrMeshok deleted the fix/default-array-value-in-construct-with-context branch September 13, 2024 12:11
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants