<?php
namespace App\Export\SpecificField;
use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface;
use ApiPlatform\Core\Exception\ResourceClassNotSupportedException;
use App\Export\AbstractExporter;
use App\Export\Builder\ExporterInterface;
use App\Model\Field\Field;
use App\Model\FieldType\FieldType;
use App\Model\Listing\Listing;
use App\Model\SpecificField\SpecificField;
use App\Model\SpecificFieldsAwareInterface;
use App\V4\Model\SpecificField\SpecificField as SpecificFieldV4;
use App\V4\Security\AuthenticationManager;
class SpecificFieldExporter extends AbstractExporter implements ExporterInterface
{
/**
* @var CollectionDataProviderInterface
*/
protected $collectionDataProvider;
/**
* @var Field[]|null
*/
protected $fields;
/**
* @var AuthenticationManager
*/
private $authManager;
public function __construct(CollectionDataProviderInterface $collectionDataProvider, AuthenticationManager $authManager)
{
$this->collectionDataProvider = $collectionDataProvider;
$this->authManager = $authManager;
}
/**
* {@inheritdoc}
*/
public function supports($entity, string $fieldName, string $exportType): bool
{
$sfId = str_replace(['sf_', 'specificFields_', 'sf.', 'specificFields.'], '', $fieldName);
return $entity instanceof SpecificFieldsAwareInterface
&& (str_starts_with($fieldName, 'sf_') || str_starts_with($fieldName, 'specificFields'))
&& ($entity->getSpecificFieldByFieldId($sfId) instanceof SpecificField
|| $entity->getSpecificFieldByFieldId($sfId) instanceof SpecificFieldV4)
;
}
/**
* {@inheritdoc}
*
* @throws ResourceClassNotSupportedException
*/
public function convert($entity, string $fieldName): ?string
{
$sfId = str_replace(['sf_', 'specificFields_', 'sf.', 'specificFields.'], '', $fieldName);
/** @var SpecificField $specificField */
$specificField = $entity->getSpecificFieldByFieldId($sfId);
$field = $this->getField($specificField->getFieldId());
if (!$field instanceof Field) {
return $specificField->getValue();
}
switch ($field->getFieldType()->getType()) {
case FieldType::FIELD_TYPE_NUMBER:
case FieldType::FIELD_TYPE_INTEGER:
return str_replace(',', '.', $specificField->getValue());
case FieldType::FIELD_TYPE_TEXT:
default:
return $specificField->getValue();
}
}
/**
* @param string $fieldId
*
* @return Field|null
*
* @throws ResourceClassNotSupportedException
*/
protected function getFieldBySfId(string $fieldId): ?Field
{
$sfId = str_replace(['sf_', 'specificFields_', 'sf.', 'specificFields.'], '', $fieldId);
return $this->getField($sfId);
}
/**
* @param string $fieldId
*
* @return Field|null
*
* @throws ResourceClassNotSupportedException
*/
protected function getField(string $fieldId): ?Field
{
$customerId = $this->authManager->getLoggedInUser()->getCustomerId();
if (!isset($this->fields[$customerId])) {
/** @var Field[] $fields */
$fields = $this->collectionDataProvider->getCollection(Field::class);
$this->fields[$customerId] = [];
foreach ($fields as $field) {
$this->fields[$customerId][$field->getId()] = $field;
}
}
return $this->fields[$customerId][$fieldId] ?? null;
}
/**
* @todo retyper $specificField une fois la transition V4 effectuée
*
* @param Field $field
* @param SpecificField|SpecificFieldV4 $specificField
*
* @return string
*/
protected function getListingItemNames(Field $field, $specificField): string
{
$allChoices = [];
if ($field->getListing() instanceof Listing) {
foreach ($field->getListing()->getListingItems() as $listingItem) {
$allChoices[$listingItem->getId()] = $listingItem->getName();
}
}
if ([] !== $field->getOption('choices', [])) {
foreach ($field->getOption('choices', []) as $key => $value) {
$allChoices[$key] = $value;
}
}
return implode(', ', array_filter($allChoices, function ($key, $value) use ($specificField) {
return in_array($key, explode(',', $specificField->getValue()), true)
|| in_array($value, explode(',', $specificField->getValue()), true)
;
}, ARRAY_FILTER_USE_BOTH));
}
}