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 null !== 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()/* : TranslatableInterface|string|false|null */
  132.     {
  133.         return $this->label;
  134.     }
  135.     /**
  136.      * @param TranslatableInterface|string|false|null $label
  137.      */
  138.     public function setLabel(/* TranslatableInterface|string|false|null */ $label): void
  139.     {
  140.         if (!\is_string($label)
  141.             && !$label instanceof TranslatableInterface
  142.             && false !== $label
  143.             && null !== $label) {
  144.             trigger_deprecation(
  145.                 'easycorp/easyadmin-bundle',
  146.                 '4.0.5',
  147.                 'Argument "%s" for "%s" must be one of these types: %s. Passing type "%s" will cause an error in 5.0.0.',
  148.                 '$label',
  149.                 __METHOD__,
  150.                 '"TranslatableInterface", "string", "false" or "null"',
  151.                 \gettype($label)
  152.             );
  153.         }
  154.         $this->label $label;
  155.     }
  156.     public function getFormType(): ?string
  157.     {
  158.         return $this->formType;
  159.     }
  160.     public function setFormType(string $formTypeFqcn): void
  161.     {
  162.         $this->formType $formTypeFqcn;
  163.     }
  164.     public function getFormTypeOptions(): array
  165.     {
  166.         return $this->formTypeOptions->all();
  167.     }
  168.     public function getFormTypeOption(string $optionName)
  169.     {
  170.         return $this->formTypeOptions->get($optionName);
  171.     }
  172.     public function setFormTypeOptions(array $formTypeOptions): void
  173.     {
  174.         foreach ($formTypeOptions as $optionName => $optionValue) {
  175.             $this->setFormTypeOption($optionName$optionValue);
  176.         }
  177.     }
  178.     /**
  179.      * @param $optionName You can use "dot" notation to set nested options (e.g. 'attr.class')
  180.      */
  181.     public function setFormTypeOption(string $optionNamemixed $optionValue): void
  182.     {
  183.         $this->formTypeOptions->set($optionName$optionValue);
  184.     }
  185.     /**
  186.      * @param $optionName You can use "dot" notation to set nested options (e.g. 'attr.class')
  187.      */
  188.     public function setFormTypeOptionIfNotSet(string $optionNamemixed $optionValue): void
  189.     {
  190.         $this->formTypeOptions->setIfNotSet($optionName$optionValue);
  191.     }
  192.     public function isSortable(): ?bool
  193.     {
  194.         return $this->sortable;
  195.     }
  196.     public function setSortable(bool $isSortable): void
  197.     {
  198.         $this->sortable $isSortable;
  199.     }
  200.     public function isVirtual(): ?bool
  201.     {
  202.         return $this->virtual;
  203.     }
  204.     public function setVirtual(bool $isVirtual): void
  205.     {
  206.         $this->virtual $isVirtual;
  207.     }
  208.     public function getTextAlign(): ?string
  209.     {
  210.         return $this->textAlign;
  211.     }
  212.     public function setTextAlign(string $textAlign): void
  213.     {
  214.         $this->textAlign $textAlign;
  215.     }
  216.     public function getPermission(): ?string
  217.     {
  218.         return $this->permission;
  219.     }
  220.     public function setPermission(string $permission): void
  221.     {
  222.         $this->permission $permission;
  223.     }
  224.     public function getHelp(): TranslatableInterface|string|null
  225.     {
  226.         return $this->help;
  227.     }
  228.     public function setHelp(TranslatableInterface|string $help): void
  229.     {
  230.         $this->help $help;
  231.     }
  232.     public function getCssClass(): string
  233.     {
  234.         return $this->cssClass;
  235.     }
  236.     public function setCssClass(string $cssClass): void
  237.     {
  238.         $this->cssClass trim($cssClass);
  239.     }
  240.     public function getColumns(): ?string
  241.     {
  242.         return $this->columns;
  243.     }
  244.     public function setColumns(?string $columnCssClasses): void
  245.     {
  246.         $this->columns $columnCssClasses;
  247.     }
  248.     public function getDefaultColumns(): string
  249.     {
  250.         return $this->defaultColumns;
  251.     }
  252.     public function setDefaultColumns(string $columnCssClasses): void
  253.     {
  254.         $this->defaultColumns $columnCssClasses;
  255.     }
  256.     public function getTranslationParameters(): array
  257.     {
  258.         return $this->translationParameters;
  259.     }
  260.     public function setTranslationParameters(array $translationParameters): void
  261.     {
  262.         $this->translationParameters $translationParameters;
  263.     }
  264.     public function getTemplateName(): ?string
  265.     {
  266.         return $this->templateName;
  267.     }
  268.     public function setTemplateName(?string $templateName): void
  269.     {
  270.         $this->templateName $templateName;
  271.     }
  272.     public function getTemplatePath(): ?string
  273.     {
  274.         return $this->templatePath;
  275.     }
  276.     public function setTemplatePath(?string $templatePath): void
  277.     {
  278.         $this->templatePath $templatePath;
  279.     }
  280.     public function addFormTheme(string $formThemePath): void
  281.     {
  282.         $this->formThemePaths[] = $formThemePath;
  283.     }
  284.     public function getFormThemes(): array
  285.     {
  286.         return $this->formThemePaths;
  287.     }
  288.     public function setFormThemes(array $formThemePaths): void
  289.     {
  290.         $this->formThemePaths $formThemePaths;
  291.     }
  292.     public function getAssets(): AssetsDto
  293.     {
  294.         return $this->assets;
  295.     }
  296.     public function setAssets(AssetsDto $assets): void
  297.     {
  298.         $this->assets $assets;
  299.     }
  300.     public function addWebpackEncoreAsset(AssetDto $assetDto): void
  301.     {
  302.         $this->assets->addWebpackEncoreAsset($assetDto);
  303.     }
  304.     public function addCssAsset(AssetDto $assetDto): void
  305.     {
  306.         $this->assets->addCssAsset($assetDto);
  307.     }
  308.     public function addJsAsset(AssetDto $assetDto): void
  309.     {
  310.         $this->assets->addJsAsset($assetDto);
  311.     }
  312.     public function addHtmlContentToHead(string $htmlContent): void
  313.     {
  314.         $this->assets->addHtmlContentToHead($htmlContent);
  315.     }
  316.     public function addHtmlContentToBody(string $htmlContent): void
  317.     {
  318.         $this->assets->addHtmlContentToBody($htmlContent);
  319.     }
  320.     public function getCustomOptions(): KeyValueStore
  321.     {
  322.         return $this->customOptions;
  323.     }
  324.     public function getCustomOption(string $optionName): mixed
  325.     {
  326.         return $this->customOptions->get($optionName);
  327.     }
  328.     public function setCustomOptions(array $customOptions): void
  329.     {
  330.         $this->customOptions KeyValueStore::new($customOptions);
  331.     }
  332.     public function setCustomOption(string $optionNamemixed $optionValue): void
  333.     {
  334.         $this->customOptions->set($optionName$optionValue);
  335.     }
  336.     public function getDoctrineMetadata(): KeyValueStore
  337.     {
  338.         return $this->doctrineMetadata;
  339.     }
  340.     public function setDoctrineMetadata(array $metadata): void
  341.     {
  342.         $this->doctrineMetadata KeyValueStore::new($metadata);
  343.     }
  344.     public function getDisplayedOn(): KeyValueStore
  345.     {
  346.         return $this->displayedOn;
  347.     }
  348.     public function setDisplayedOn(KeyValueStore $displayedOn): void
  349.     {
  350.         $this->displayedOn $displayedOn;
  351.     }
  352.     public function isDisplayedOn(string $pageName): bool
  353.     {
  354.         return $this->displayedOn->has($pageName);
  355.     }
  356. }