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