src/Export/SpecificField/SpecificFieldExporter.php line 87

Open in your IDE?
  1. <?php
  2. namespace App\Export\SpecificField;
  3. use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface;
  4. use ApiPlatform\Core\Exception\ResourceClassNotSupportedException;
  5. use App\Export\AbstractExporter;
  6. use App\Export\Builder\ExporterInterface;
  7. use App\Model\Field\Field;
  8. use App\Model\FieldType\FieldType;
  9. use App\Model\Listing\Listing;
  10. use App\Model\SpecificField\SpecificField;
  11. use App\Model\SpecificFieldsAwareInterface;
  12. use App\V4\Model\SpecificField\SpecificField as SpecificFieldV4;
  13. use App\V4\Security\AuthenticationManager;
  14. class SpecificFieldExporter extends AbstractExporter implements ExporterInterface
  15. {
  16.     /**
  17.      * @var CollectionDataProviderInterface
  18.      */
  19.     protected $collectionDataProvider;
  20.     /**
  21.      * @var Field[]|null
  22.      */
  23.     protected $fields;
  24.     /**
  25.      * @var AuthenticationManager
  26.      */
  27.     private $authManager;
  28.     public function __construct(CollectionDataProviderInterface $collectionDataProviderAuthenticationManager $authManager)
  29.     {
  30.         $this->collectionDataProvider $collectionDataProvider;
  31.         $this->authManager $authManager;
  32.     }
  33.     /**
  34.      * {@inheritdoc}
  35.      */
  36.     public function supports($entitystring $fieldNamestring $exportType): bool
  37.     {
  38.         $sfId str_replace(['sf_''specificFields_''sf.''specificFields.'], ''$fieldName);
  39.         return $entity instanceof SpecificFieldsAwareInterface
  40.             && (str_starts_with($fieldName'sf_') || str_starts_with($fieldName'specificFields'))
  41.             && ($entity->getSpecificFieldByFieldId($sfId) instanceof SpecificField
  42.                 || $entity->getSpecificFieldByFieldId($sfId) instanceof SpecificFieldV4)
  43.         ;
  44.     }
  45.     /**
  46.      * {@inheritdoc}
  47.      *
  48.      * @throws ResourceClassNotSupportedException
  49.      */
  50.     public function convert($entitystring $fieldName): ?string
  51.     {
  52.         $sfId str_replace(['sf_''specificFields_''sf.''specificFields.'], ''$fieldName);
  53.         /** @var SpecificField $specificField */
  54.         $specificField $entity->getSpecificFieldByFieldId($sfId);
  55.         $field $this->getField($specificField->getFieldId());
  56.         if (!$field instanceof Field) {
  57.             return $specificField->getValue();
  58.         }
  59.         switch ($field->getFieldType()->getType()) {
  60.             case FieldType::FIELD_TYPE_NUMBER:
  61.             case FieldType::FIELD_TYPE_INTEGER:
  62.                 return str_replace(',''.'$specificField->getValue());
  63.             case FieldType::FIELD_TYPE_TEXT:
  64.             default:
  65.                 return $specificField->getValue();
  66.         }
  67.     }
  68.     /**
  69.      * @param string $fieldId
  70.      *
  71.      * @return Field|null
  72.      *
  73.      * @throws ResourceClassNotSupportedException
  74.      */
  75.     protected function getFieldBySfId(string $fieldId): ?Field
  76.     {
  77.         $sfId str_replace(['sf_''specificFields_''sf.''specificFields.'], ''$fieldId);
  78.         return $this->getField($sfId);
  79.     }
  80.     /**
  81.      * @param string $fieldId
  82.      *
  83.      * @return Field|null
  84.      *
  85.      * @throws ResourceClassNotSupportedException
  86.      */
  87.     protected function getField(string $fieldId): ?Field
  88.     {
  89.         $customerId $this->authManager->getLoggedInUser()->getCustomerId();
  90.         if (!isset($this->fields[$customerId])) {
  91.             /** @var Field[] $fields */
  92.             $fields $this->collectionDataProvider->getCollection(Field::class);
  93.             $this->fields[$customerId] = [];
  94.             foreach ($fields as $field) {
  95.                 $this->fields[$customerId][$field->getId()] = $field;
  96.             }
  97.         }
  98.         return $this->fields[$customerId][$fieldId] ?? null;
  99.     }
  100.     /**
  101.      * @todo retyper $specificField une fois la transition V4 effectuĂ©e
  102.      *
  103.      * @param Field                         $field
  104.      * @param SpecificField|SpecificFieldV4 $specificField
  105.      *
  106.      * @return string
  107.      */
  108.     protected function getListingItemNames(Field $field$specificField): string
  109.     {
  110.         $allChoices = [];
  111.         if ($field->getListing() instanceof Listing) {
  112.             foreach ($field->getListing()->getListingItems() as $listingItem) {
  113.                 $allChoices[$listingItem->getId()] = $listingItem->getName();
  114.             }
  115.         }
  116.         if ([] !== $field->getOption('choices', [])) {
  117.             foreach ($field->getOption('choices', []) as $key => $value) {
  118.                 $allChoices[$key] = $value;
  119.             }
  120.         }
  121.         return implode(', 'array_filter($allChoices, function ($key$value) use ($specificField) {
  122.             return in_array($keyexplode(','$specificField->getValue()), true)
  123.                 || in_array($valueexplode(','$specificField->getValue()), true)
  124.             ;
  125.         }, ARRAY_FILTER_USE_BOTH));
  126.     }
  127. }