MatchStrategy map formula entity formula name to formula resource
This commit is contained in:
@@ -7,8 +7,11 @@ use ApiPlatform\Metadata as API;
|
||||
use App\ApiResource\Money;
|
||||
use App\Entity\Matchfunding\MatchAgainst;
|
||||
use App\Entity\Matchfunding\MatchStrategy;
|
||||
use App\Mapping\Transformer\MatchFormulaMapTransformer;
|
||||
use App\State\ApiResourceStateProcessor;
|
||||
use App\State\ApiResourceStateProvider;
|
||||
use AutoMapper\Attribute\MapFrom;
|
||||
use AutoMapper\Attribute\MapTo;
|
||||
use Symfony\Component\Validator\Constraints as Assert;
|
||||
|
||||
#[API\ApiResource(
|
||||
@@ -43,6 +46,8 @@ class MatchStrategyApiResource
|
||||
* The MatchFormula used to calculate matched funds.
|
||||
*/
|
||||
#[Assert\NotBlank()]
|
||||
#[MapTo(MatchStrategy::class, property: 'formulaName', transformer: 'source.formula.name')]
|
||||
#[MapFrom(MatchStrategy::class, property: 'formulaName', transformer: MatchFormulaMapTransformer::class)]
|
||||
public FormulaApiResource $formula;
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace App\Mapping\Transformer;
|
||||
|
||||
use App\ApiResource\Matchfunding\FormulaApiResource;
|
||||
use App\Matchfunding\Formula\FormulaLocator;
|
||||
use AutoMapper\Transformer\PropertyTransformer\PropertyTransformerInterface;
|
||||
|
||||
class MatchFormulaMapTransformer implements PropertyTransformerInterface
|
||||
{
|
||||
public function __construct(
|
||||
private FormulaLocator $formulaLocator,
|
||||
) {}
|
||||
|
||||
public function transform(mixed $value, object|array $source, array $context): mixed
|
||||
{
|
||||
$interface = $this->formulaLocator->get($value);
|
||||
|
||||
return FormulaApiResource::from($interface);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user