<?php
namespace App\Export\Csv\SpecificField;
use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface;
use ApiPlatform\Core\Exception\ResourceClassNotSupportedException;
use App\Export\Builder\ExporterInterface;
use App\Export\SpecificField\SpecificFieldExporter;
use App\Model\Field\Field;
use App\Model\FieldType\FieldType;
use App\Model\SpecificField\SpecificField;
use App\V4\Security\AuthenticationManager;
use DateTime;
use Exception;
class SpecificFieldDateCsvExporter extends SpecificFieldExporter
{
public function __construct(CollectionDataProviderInterface $collectionDataProvider, AuthenticationManager $authManager)
{
parent::__construct($collectionDataProvider, $authManager);
}
/**
* @return int
*/
public static function getDefaultPriority(): int
{
return 2000;
}
/**
* {@inheritdoc}
*
* @throws ResourceClassNotSupportedException
*/
public function supports($entity, string $fieldName, string $exportType): bool
{
$fieldObj = $this->getFieldBySfId($fieldName);
return parent::supports($entity, $fieldName, $exportType)
&& $fieldObj instanceof Field
&& FieldType::FIELD_TYPE_DATE === $fieldObj->getFieldType()->getType()
&& ExporterInterface::EXPORT_TYPE_CSV === $exportType
;
}
/**
* {@inheritdoc}
*
* @throws Exception
*/
public function convert($entity, string $fieldName): ?string
{
$sfId = str_replace(['sf_', 'specificFields_', 'sf.', 'specificFields.'], '', $fieldName);
/** @var SpecificField $specificField */
$specificField = $entity->getSpecificFieldByFieldId($sfId);
return null !== $specificField->getValue() ? (new DateTime($specificField->getValue()))->format('d/m/Y') : null;
}
}