vendor/easycorp/easyadmin-bundle/src/Dto/FieldDto.php line 25

Open in your IDE?
  1. <?php
  2. namespace EasyCorp\Bundle\EasyAdminBundle\Dto;
  3. use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
  4. use EasyCorp\Bundle\EasyAdminBundle\Config\KeyValueStore;
  5. use EasyCorp\Bundle\EasyAdminBundle\Form\Type\EaFormFieldsetType;
  6. use EasyCorp\Bundle\EasyAdminBundle\Form\Type\Layout\EaFormColumnCloseType;
  7. use EasyCorp\Bundle\EasyAdminBundle\Form\Type\Layout\EaFormColumnGroupCloseType;
  8. use EasyCorp\Bundle\EasyAdminBundle\Form\Type\Layout\EaFormColumnGroupOpenType;
  9. use EasyCorp\Bundle\EasyAdminBundle\Form\Type\Layout\EaFormColumnOpenType;
  10. use EasyCorp\Bundle\EasyAdminBundle\Form\Type\Layout\EaFormFieldsetCloseType;
  11. use EasyCorp\Bundle\EasyAdminBundle\Form\Type\Layout\EaFormFieldsetOpenType;
  12. use EasyCorp\Bundle\EasyAdminBundle\Form\Type\Layout\EaFormTabListType;
  13. use EasyCorp\Bundle\EasyAdminBundle\Form\Type\Layout\EaFormTabPaneCloseType;
  14. use EasyCorp\Bundle\EasyAdminBundle\Form\Type\Layout\EaFormTabPaneGroupCloseType;
  15. use EasyCorp\Bundle\EasyAdminBundle\Form\Type\Layout\EaFormTabPaneGroupOpenType;
  16. use EasyCorp\Bundle\EasyAdminBundle\Form\Type\Layout\EaFormTabPaneOpenType;
  17. use Symfony\Component\Uid\Ulid;
  18. use Symfony\Contracts\Translation\TranslatableInterface;
  19. /**
  20.  * @author Javier Eguiluz <[email protected]>
  21.  */
  22. final class FieldDto
  23. {
  24.     private ?string $fieldFqcn null;
  25.     private ?string $propertyName null;
  26.     private mixed $value null;
  27.     private mixed $formattedValue null;
  28.     private $formatValueCallable;
  29.     private $label;
  30.     private ?string $formType null;
  31.     private KeyValueStore $formTypeOptions;
  32.     private ?bool $sortable null;
  33.     private ?bool $virtual null;
  34.     private ?string $permission null;
  35.     private ?string $textAlign null;
  36.     private $help;
  37.     private string $cssClass '';
  38.     // how many columns the field takes when rendering
  39.     // (defined as Bootstrap 5 grid classes; e.g. 'col-md-6 col-xxl-3')
  40.     private ?string $columns null;
  41.     // same as $columns but used when the user doesn't define columns explicitly
  42.     private string $defaultColumns '';
  43.     private array $translationParameters = [];
  44.     private ?string $templateName 'crud/field/text';
  45.     private ?string $templatePath null;
  46.     private array $formThemePaths = [];
  47.     private AssetsDto $assets;
  48.     private KeyValueStore $customOptions;
  49.     private KeyValueStore $doctrineMetadata;
  50.     /** @internal */
  51.     private $uniqueId;
  52.     private KeyValueStore $displayedOn;
  53.     public function __construct()
  54.     {
  55.         $this->uniqueId = new Ulid();
  56.         $this->assets = new AssetsDto();
  57.         $this->formTypeOptions KeyValueStore::new();
  58.         $this->customOptions KeyValueStore::new();
  59.         $this->doctrineMetadata KeyValueStore::new();
  60.         $this->displayedOn KeyValueStore::new([
  61.             Crud::PAGE_INDEX => Crud::PAGE_INDEX,
  62.             Crud::PAGE_DETAIL => Crud::PAGE_DETAIL,
  63.             Crud::PAGE_EDIT => Crud::PAGE_EDIT,
  64.             Crud::PAGE_NEW => Crud::PAGE_NEW,
  65.         ]);
  66.     }
  67.     public function __clone()
  68.     {
  69.         $this->uniqueId = new Ulid();
  70.         $this->assets = clone $this->assets;
  71.         $this->formTypeOptions = clone $this->formTypeOptions;
  72.         $this->customOptions = clone $this->customOptions;
  73.         $this->doctrineMetadata = clone $this->doctrineMetadata;
  74.         $this->displayedOn = clone $this->displayedOn;
  75.     }
  76.     public function getUniqueId(): string
  77.     {
  78.         return $this->uniqueId;
  79.     }
  80.     public function setUniqueId(string $uniqueId): void
  81.     {
  82.         $this->uniqueId $uniqueId;
  83.     }
  84.     public function isFormDecorationField(): bool
  85.     {
  86.         trigger_deprecation(
  87.             'easycorp/easyadmin-bundle',
  88.             '4.8.0',
  89.             '"FieldDto::isFormDecorationField()" has been deprecated in favor of "FieldDto::isFormLayoutField()" and it will be removed in 5.0.0.',
  90.         );
  91.         return $this->isFormLayoutField();
  92.     }
  93.     public function isFormLayoutField(): bool
  94.     {
  95.         $formLayoutFieldClasses = [
  96.             EaFormTabListType::class,
  97.             EaFormTabPaneGroupOpenType::class,
  98.             EaFormTabPaneGroupCloseType::class,
  99.             EaFormTabPaneOpenType::class,
  100.             EaFormTabPaneCloseType::class,
  101.             EaFormColumnGroupOpenType::class,
  102.             EaFormColumnGroupCloseType::class,
  103.             EaFormColumnOpenType::class,
  104.             EaFormColumnCloseType::class,
  105.             EaFormFieldsetOpenType::class,
  106.             EaFormFieldsetCloseType::class,
  107.         ];
  108.         return \in_array($this->formType$formLayoutFieldClassestrue);
  109.     }
  110.     public function isFormFieldset(): bool
  111.     {
  112.         return \in_array($this->formType, [EaFormFieldsetType::class, EaFormFieldsetOpenType::class], true);
  113.     }
  114.     public function isFormTab(): bool
  115.     {
  116.         return EaFormTabPaneOpenType::class === $this->formType;
  117.     }
  118.     public function isFormColumn(): bool
  119.     {
  120.         return EaFormColumnOpenType::class === $this->formType;
  121.     }
  122.     public function getFieldFqcn(): ?string
  123.     {
  124.         return $this->fieldFqcn;
  125.     }
  126.     /**
  127.      * @internal Don't use this method yourself. EasyAdmin uses it internally
  128.      *           to set the field FQCN. It's OK to use getFieldFqcn() to get this value.
  129.      */
  130.     public function setFieldFqcn(string $fieldFqcn): void
  131.     {
  132.         $this->fieldFqcn $fieldFqcn;
  133.     }
  134.     public function getProperty(): string
  135.     {
  136.         return $this->propertyName;
  137.     }
  138.     public function setProperty(string $propertyName): void
  139.     {
  140.         $this->propertyName $propertyName;
  141.     }
  142.     /**
  143.      * Returns the original unmodified value stored in the entity field.
  144.      */
  145.     public function getValue(): mixed
  146.     {
  147.         return $this->value;
  148.     }
  149.     public function setValue(mixed $value): void
  150.     {
  151.         $this->value $value;
  152.     }
  153.     /**
  154.      * Returns the value to be displayed for the field (it could be the
  155.      * same as the value stored in the field or not).
  156.      */
  157.     public function getFormattedValue(): mixed
  158.     {
  159.         return $this->formattedValue;
  160.     }
  161.     public function setFormattedValue(mixed $formattedValue): void
  162.     {
  163.         $this->formattedValue $formattedValue;
  164.     }
  165.     public function getFormatValueCallable(): ?callable
  166.     {
  167.         return $this->formatValueCallable;
  168.     }
  169.     public function setFormatValueCallable(?callable $callable): void
  170.     {
  171.         $this->formatValueCallable $callable;
  172.     }
  173.     /**
  174.      * @return TranslatableInterface|string|false|null
  175.      */
  176.     public function getLabel()
  177.     {
  178.         return $this->label;
  179.     }
  180.     /**
  181.      * @param TranslatableInterface|string|false|null $label
  182.      */
  183.     public function setLabel($label): void
  184.     {
  185.         if (!\is_string($label) && !$label instanceof TranslatableInterface && false !== $label && null !== $label) {
  186.             trigger_deprecation(
  187.                 'easycorp/easyadmin-bundle',
  188.                 '4.0.5',
  189.                 'Argument "%s" for "%s" must be one of these types: %s. Passing type "%s" will cause an error in 5.0.0.',
  190.                 '$label',
  191.                 __METHOD__,
  192.                 '"TranslatableInterface", "string", "false" or "null"',
  193.                 \gettype($label)
  194.             );
  195.         }
  196.         $this->label $label;
  197.     }
  198.     public function getFormType(): ?string
  199.     {
  200.         return $this->formType;
  201.     }
  202.     public function setFormType(string $formTypeFqcn): void
  203.     {
  204.         $this->formType $formTypeFqcn;
  205.     }
  206.     public function getFormTypeOptions(): array
  207.     {
  208.         return $this->formTypeOptions->all();
  209.     }
  210.     public function getFormTypeOption(string $optionName)
  211.     {
  212.         return $this->formTypeOptions->get($optionName);
  213.     }
  214.     public function setFormTypeOptions(array $formTypeOptions): void
  215.     {
  216.         foreach ($formTypeOptions as $optionName => $optionValue) {
  217.             $this->setFormTypeOption($optionName$optionValue);
  218.         }
  219.     }
  220.     /**
  221.      * @param string $optionName You can use "dot" notation to set nested options (e.g. 'attr.class')
  222.      */
  223.     public function setFormTypeOption(string $optionNamemixed $optionValue): void
  224.     {
  225.         $this->formTypeOptions->set($optionName$optionValue);
  226.     }
  227.     /**
  228.      * @param string $optionName You can use "dot" notation to set nested options (e.g. 'attr.class')
  229.      */
  230.     public function setFormTypeOptionIfNotSet(string $optionNamemixed $optionValue): void
  231.     {
  232.         $this->formTypeOptions->setIfNotSet($optionName$optionValue);
  233.     }
  234.     public function isSortable(): ?bool
  235.     {
  236.         return $this->sortable;
  237.     }
  238.     public function setSortable(bool $isSortable): void
  239.     {
  240.         $this->sortable $isSortable;
  241.     }
  242.     public function isVirtual(): ?bool
  243.     {
  244.         return $this->virtual;
  245.     }
  246.     public function setVirtual(bool $isVirtual): void
  247.     {
  248.         $this->virtual $isVirtual;
  249.     }
  250.     public function getTextAlign(): ?string
  251.     {
  252.         return $this->textAlign;
  253.     }
  254.     public function setTextAlign(string $textAlign): void
  255.     {
  256.         $this->textAlign $textAlign;
  257.     }
  258.     public function getPermission(): ?string
  259.     {
  260.         return $this->permission;
  261.     }
  262.     public function setPermission(string $permission): void
  263.     {
  264.         $this->permission $permission;
  265.     }
  266.     public function getHelp(): TranslatableInterface|string|null
  267.     {
  268.         return $this->help;
  269.     }
  270.     public function setHelp(TranslatableInterface|string $help): void
  271.     {
  272.         $this->help $help;
  273.     }
  274.     public function getCssClass(): string
  275.     {
  276.         return $this->cssClass;
  277.     }
  278.     public function setCssClass(string $cssClass): void
  279.     {
  280.         $this->cssClass trim($cssClass);
  281.     }
  282.     public function getColumns(): ?string
  283.     {
  284.         return $this->columns;
  285.     }
  286.     public function setColumns(?string $columnCssClasses): void
  287.     {
  288.         $this->columns $columnCssClasses;
  289.     }
  290.     public function getDefaultColumns(): string
  291.     {
  292.         return $this->defaultColumns;
  293.     }
  294.     public function setDefaultColumns(string $columnCssClasses): void
  295.     {
  296.         $this->defaultColumns $columnCssClasses;
  297.     }
  298.     public function getTranslationParameters(): array
  299.     {
  300.         return $this->translationParameters;
  301.     }
  302.     public function setTranslationParameters(array $translationParameters): void
  303.     {
  304.         $this->translationParameters $translationParameters;
  305.     }
  306.     public function getTemplateName(): ?string
  307.     {
  308.         return $this->templateName;
  309.     }
  310.     public function setTemplateName(?string $templateName): void
  311.     {
  312.         $this->templateName $templateName;
  313.     }
  314.     public function getTemplatePath(): ?string
  315.     {
  316.         return $this->templatePath;
  317.     }
  318.     public function setTemplatePath(?string $templatePath): void
  319.     {
  320.         $this->templatePath $templatePath;
  321.     }
  322.     public function addFormTheme(string $formThemePath): void
  323.     {
  324.         $this->formThemePaths[] = $formThemePath;
  325.     }
  326.     public function getFormThemes(): array
  327.     {
  328.         return $this->formThemePaths;
  329.     }
  330.     public function setFormThemes(array $formThemePaths): void
  331.     {
  332.         $this->formThemePaths $formThemePaths;
  333.     }
  334.     public function getAssets(): AssetsDto
  335.     {
  336.         return $this->assets;
  337.     }
  338.     public function setAssets(AssetsDto $assets): void
  339.     {
  340.         $this->assets $assets;
  341.     }
  342.     public function addWebpackEncoreAsset(AssetDto $assetDto): void
  343.     {
  344.         $this->assets->addWebpackEncoreAsset($assetDto);
  345.     }
  346.     public function addCssAsset(AssetDto $assetDto): void
  347.     {
  348.         $this->assets->addCssAsset($assetDto);
  349.     }
  350.     public function addJsAsset(AssetDto $assetDto): void
  351.     {
  352.         $this->assets->addJsAsset($assetDto);
  353.     }
  354.     public function addHtmlContentToHead(string $htmlContent): void
  355.     {
  356.         $this->assets->addHtmlContentToHead($htmlContent);
  357.     }
  358.     public function addHtmlContentToBody(string $htmlContent): void
  359.     {
  360.         $this->assets->addHtmlContentToBody($htmlContent);
  361.     }
  362.     public function getCustomOptions(): KeyValueStore
  363.     {
  364.         return $this->customOptions;
  365.     }
  366.     public function getCustomOption(string $optionName): mixed
  367.     {
  368.         return $this->customOptions->get($optionName);
  369.     }
  370.     public function setCustomOptions(array $customOptions): void
  371.     {
  372.         $this->customOptions KeyValueStore::new($customOptions);
  373.     }
  374.     public function setCustomOption(string $optionNamemixed $optionValue): void
  375.     {
  376.         $this->customOptions->set($optionName$optionValue);
  377.     }
  378.     public function getDoctrineMetadata(): KeyValueStore
  379.     {
  380.         return $this->doctrineMetadata;
  381.     }
  382.     public function setDoctrineMetadata(array $metadata): void
  383.     {
  384.         $this->doctrineMetadata KeyValueStore::new($metadata);
  385.     }
  386.     public function getDisplayedOn(): KeyValueStore
  387.     {
  388.         return $this->displayedOn;
  389.     }
  390.     public function setDisplayedOn(KeyValueStore $displayedOn): void
  391.     {
  392.         $this->displayedOn $displayedOn;
  393.     }
  394.     public function isDisplayedOn(string $pageName): bool
  395.     {
  396.         return $this->displayedOn->has($pageName);
  397.     }
  398. }