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

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