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

fix: source and context not passed to callable transformer #192

Merged
merged 1 commit into from
Oct 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- [GH#184](https://github.com/jolicode/automapper/pull/184) Fix error when mapping from stdClass to constructor with nullable/optional arguments
- [GH#185](https://github.com/jolicode/automapper/pull/185) Fix constructor with default parameter array does not work with constructor_arguments context
- [GH#187](https://github.com/jolicode/automapper/pull/187) Fix regression after [GH#184](https://github.com/jolicode/automapper/pull/184)
- [GH#192](https://github.com/jolicode/automapper/pull/192) Fix source and context not passed to callable transformer

## [9.1.2] - 2024-09-03
### Fixed
Expand Down
39 changes: 11 additions & 28 deletions src/Transformer/CallableTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,37 +24,20 @@ public function __construct(

public function transform(Expr $input, Expr $target, PropertyMetadata $propertyMapping, UniqueVariableScope $uniqueVariableScope, Expr\Variable $source): array
{
if ($this->callableIsMethodFromSource) {
$newInput = new Expr\MethodCall(
$source,
if ($this->callableIsMethodFromSource || $this->callableIsMethodFromTarget) {
return [new Expr\MethodCall(
$this->callableIsMethodFromSource ? $source : new Expr\Variable('result'),
$this->callable,
[
new Arg($input),
]
);

return [$newInput, []];
}

if ($this->callableIsMethodFromTarget) {
$newInput = new Expr\MethodCall(
new Expr\Variable('result'),
$this->callable,
[
new Arg($input),
]
);

return [$newInput, []];
[new Arg($input), new Arg($source), new Arg(new Expr\Variable('context'))],
), []];
}

$newInput = new Expr\FuncCall(
return [new Expr\FuncCall(
new Scalar\String_($this->callable),
[
new Arg($input),
]
);

return [$newInput, []];
// Internal functions throws ArgumentCountError when too many arguments are passed
\function_exists($this->callable) && (new \ReflectionFunction($this->callable))->isInternal()
? [new Arg($input)]
: [new Arg($input), new Arg($source), new Arg(new Expr\Variable('context'))]
), []];
}
}
6 changes: 3 additions & 3 deletions tests/Fixtures/MapTo/Bar.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Bar
public function __construct(
public string $bar,
public string $baz,
#[MapFrom(property: 'foo')]
#[MapFrom(property: 'foo', transformer: 'htmlspecialchars')]
public string $from,
) {
}
Expand All @@ -36,12 +36,12 @@ public function getB(): string
return $this->b;
}

public function transformC(string $c): string
public function transformC(string $c, array $source, array $context): string
{
return 'transformC_' . $c;
}

public static function transformDStatic(string $c): string
public static function transformDStatic(string $c, array $source, array $context): string
{
return 'transformDStatic_' . $c;
}
Expand Down
Loading