<?php
namespace App\EventSubscriber\Import\EntityMapper\VOD\Title;
use App\Application\EntityImportBundle\Event\EmbeddableMapperEvent;
use App\Application\EntityImportBundle\ValueObject\ConfigItemPropertiesValue;
use App\Entity\VOD\Title;
use App\Entity\VOD\TitleLocalizedText;
use App\Enum\VOD\SubtitleTypeEnum;
use App\EventSubscriber\Import\EntityMapper\BaseEmbeddedSubscriber;
/**
* Class EmbeddedEntitySubscriber.
*/
class EmbeddedEntitySubscriber extends BaseEmbeddedSubscriber
{
public function onEntityPrePersist(EmbeddableMapperEvent $event): void
{
if (!$event->getEntity() instanceof Title) {
return;
}
$this->handleTitleLocalizedText($event->getEntity(), $event->getEmbeddedProperties(), $event->getData());
}
/**
* Responsible for providing data for required subtitles json field.
*
* @param \App\Entity\VOD\TitleLocalizedText $entity
* The instance of the managed localized text entity
* @param \App\Application\EntityImportBundle\ValueObject\ConfigItemPropertiesValue $property
* The property to map
* @param array $data
* The parsed data of the current row
*/
protected function handleRequiredSubtitles(
TitleLocalizedText $entity,
ConfigItemPropertiesValue $property,
array $data
): void {
$value = $this->getPropertyValue($property, $data);
if ($this->getAllowedNullableCharacter() === $value) {
$this->setPropertyValue($entity, $property, []);
return;
}
if (empty($value)) {
return;
}
$subtitleTypes = [];
foreach (array_map('trim', explode(',', $value)) as $item) {
if (SubtitleTypeEnum::accepts($item)) {
$subtitleTypes[] = $item;
}
}
$entity->setRequiredSubtitles($subtitleTypes);
}
}