You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I try us mapper with symfony discriminator, but it's not what i expected,
For example
// I have Source Interface or Abstract classinterface SourceInterface
{
}
/// And his implfinalreadonlyclass SourceImpl implements SourceInterface
{
}
/// And i have target class for mapping, its Interface or Abstract toointerface TargetInterface
{
}
finalreadonlyclass TargetImpl implements TargetInterface
{
}
In usage something like
$source = newSource();
// For interface$result = $this->mapper->map($source, TargetInterface::class);
// And for abstract$result = $this->mapper->map($source, AbstractTarget::class, (newMapperContext())->setConstructorArgument(AbstractTarget::class, 'property', 'value')->toArray();
I try imp with event listener and replace target
#[\Attribute(\Attribute::TARGET_CLASS)]
finalreadonlyclass Discriminator
{
/** * @param class-string $baseClass * @param array<class-string, class-string> $map */publicfunction__construct(
publicstring$baseClass,
publicarray$map,
) { }
}
#[AsEventListener(GenerateMapperEvent::class)]
finalreadonlyclass DiscriminatorListener
{
publicfunction__invoke(GenerateMapperEvent$event): void
{
if (!$event->mapperMetadata->targetReflectionClass) {
return;
}
$attributes = $event->mapperMetadata
->targetReflectionClass
->getAttributes(Discriminator::class);
if (count($attributes) === 0) {
return;
}
foreach ($attributesas$attr) {
$discriminatorAttribute = $attr->newInstance();
$baseClassRef = newReflectionClass($discriminatorAttribute->baseClass);
if (!$event->mapperMetadata->sourceReflectionClass->isSubclassOf($baseClassRef)) {
continue;
}
if (!$baseClassRef->isInterface() && !$baseClassRef->isAbstract()) {
thrownewBadMapDefinitionException(sprintf(
'Required `baseClass` should be abstract or interface in "%s" attribute on "%s" class.',
Discriminator::class,
$event->mapperMetadata->targetReflectionClass->getName(),
));
}
foreach ($discriminatorAttribute->mapas$source => $target) {
$ref = newReflectionClass($source);
if (!$ref->isSubclassOf($baseClassRef->getName())) {
thrownewBadMapDefinitionException(sprintf(
'Required value of `map` should be subclass of %s in "%s" attribute on "%s" class.',
$baseClassRef->getName(),
Discriminator::class,
$event->mapperMetadata->targetReflectionClass->getName(),
));
}
if ($ref->getName() === $event->mapperMetadata->sourceReflectionClass->getName()) {
$event->mapperMetadata->target = $target;
}
}
}
}
}
And it's work, but i still can't get default construct argument from context, because it's by from context by class name
RFC for example
interface SourceInterface
{
}
final readonly class SourceImpl implements SourceInterface
{
}
#[Discriminator(
SourceInterface::class, //Base class for source
[
SourceImpl::class => TargetImpl::class, // In mapping get check source class and his mapping
]
)]
interface TargetInterface
{
}
final readonly class TargetImpl implements TargetInterface
{
}
The text was updated successfully, but these errors were encountered:
I try us mapper with symfony discriminator, but it's not what i expected,
For example
In usage something like
I try imp with event listener and replace target
And it's work, but i still can't get default construct argument from context, because it's by from context by class name
RFC for example
The text was updated successfully, but these errors were encountered: