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