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

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