vendor/shopware/core/System/CustomField/CustomFieldService.php line 85

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\System\CustomField;
  3. use Doctrine\DBAL\Connection;
  4. use Shopware\Core\Framework\DataAbstractionLayer\Field\BoolField;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Field\DateTimeField;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Field\Field;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Field\Flag\AllowHtml;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Field\Flag\ApiAware;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Field\FloatField;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Field\IntField;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Field\JsonField;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Field\LongTextField;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Field\PriceField;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Symfony\Contracts\Service\ResetInterface;
  16. /**
  17.  * @deprecated tag:v6.5.0 - reason:becomes-internal - EventSubscribers will become internal in v6.5.0
  18.  */
  19. class CustomFieldService implements EventSubscriberInterfaceResetInterface
  20. {
  21.     /**
  22.      * @var array<string>|null
  23.      */
  24.     private ?array $customFields null;
  25.     private Connection $connection;
  26.     /**
  27.      * @internal
  28.      */
  29.     public function __construct(Connection $connection)
  30.     {
  31.         $this->connection $connection;
  32.     }
  33.     public function getCustomField(string $attributeName): ?Field
  34.     {
  35.         $type $this->getCustomFields()[$attributeName] ?? null;
  36.         if (!$type) {
  37.             return null;
  38.         }
  39.         switch ($type) {
  40.             case CustomFieldTypes::INT:
  41.                 return (new IntField($attributeName$attributeName))->addFlags(new ApiAware());
  42.             case CustomFieldTypes::FLOAT:
  43.                 return (new FloatField($attributeName$attributeName))->addFlags(new ApiAware());
  44.             case CustomFieldTypes::BOOL:
  45.                 return (new BoolField($attributeName$attributeName))->addFlags(new ApiAware());
  46.             case CustomFieldTypes::DATETIME:
  47.                 return (new DateTimeField($attributeName$attributeName))->addFlags(new ApiAware());
  48.             case CustomFieldTypes::TEXT:
  49.                 return (new LongTextField($attributeName$attributeName))->addFlags(new ApiAware());
  50.             case CustomFieldTypes::HTML:
  51.                 return (new LongTextField($attributeName$attributeName))->addFlags(new ApiAware(), new AllowHtml());
  52.             case CustomFieldTypes::PRICE:
  53.                 return (new PriceField($attributeName$attributeName))->addFlags(new ApiAware());
  54.             case CustomFieldTypes::JSON:
  55.             default:
  56.                 return (new JsonField($attributeName$attributeName))->addFlags(new ApiAware());
  57.         }
  58.     }
  59.     /**
  60.      * @return array<string, string>
  61.      */
  62.     public static function getSubscribedEvents(): array
  63.     {
  64.         return [
  65.             CustomFieldEvents::CUSTOM_FIELD_DELETED_EVENT => 'reset',
  66.             CustomFieldEvents::CUSTOM_FIELD_WRITTEN_EVENT => 'reset',
  67.         ];
  68.     }
  69.     public function reset(): void
  70.     {
  71.         $this->customFields null;
  72.     }
  73.     /**
  74.      * @return array<string>
  75.      */
  76.     private function getCustomFields(): array
  77.     {
  78.         if ($this->customFields !== null) {
  79.             return $this->customFields;
  80.         }
  81.         $this->customFields $this->connection->fetchAllKeyValue('SELECT `name`, `type` FROM `custom_field` WHERE `active` = 1');
  82.         return $this->customFields;
  83.     }
  84. }