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

Constructor with default paramater array does not work #177

Merged
merged 1 commit into from
Aug 7, 2024

Conversation

Korbeil
Copy link
Member

@Korbeil Korbeil commented Jul 29, 2024

When using an argument with a default array value in a constructor, AutoMapper had issues using the default value if you pass no values in the ->map() input.

Before:

            if (\AutoMapper\MapperContext::hasConstructorArgument($context, 'AppBundle\\Entity\\Crawl\\Metadata\\Stats\\Stats', 'fourreTout')) {
                $values_2 = array();
                foreach ($value['fourreTout'] as $key_2 => $value_3) {
                    $values_2[$key_2] = $value_3;
                }
                $constructarg_8 = $values_2 ?? \AutoMapper\MapperContext::getConstructorArgument($context, 'AppBundle\\Entity\\Crawl\\Metadata\\Stats\\Stats', 'fourreTout');
            } else {
                $values_2 = array();
                foreach ($value['fourreTout'] as $key_2 => $value_3) {
                    $values_2[$key_2] = $value_3;
                }
                $constructarg_8 = $values_2 ?? array();
            }

After:

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

@Korbeil Korbeil force-pushed the fix/default-array-value-in-construct branch from 32a6689 to 103868d Compare July 29, 2024 14:50
@Korbeil Korbeil merged commit 5a09aa6 into main Aug 7, 2024
10 checks passed
@Korbeil Korbeil deleted the fix/default-array-value-in-construct branch August 7, 2024 08:07
Korbeil added a commit that referenced this pull request Sep 13, 2024
…ext (#185)

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

Before:
```PHP
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:
```PHP
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();
}
```
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

Successfully merging this pull request may close these issues.

1 participant