vendor/easycorp/easyadmin-bundle/src/Dto/ActionDto.php line 11

Open in your IDE?
  1. <?php
  2. namespace EasyCorp\Bundle\EasyAdminBundle\Dto;
  3. use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
  4. use Symfony\Contracts\Translation\TranslatableInterface;
  5. /**
  6.  * @author Javier Eguiluz <javier.eguiluz@gmail.com>
  7.  */
  8. final class ActionDto
  9. {
  10.     private ?string $type null;
  11.     private ?string $name null;
  12.     /** @var TranslatableInterface|string|(callable(object): string)|null */
  13.     private mixed $label null;
  14.     private ?string $icon null;
  15.     private string $cssClass '';
  16.     private string $addedCssClass '';
  17.     private ?string $htmlElement null;
  18.     private array $htmlAttributes = [];
  19.     private ?string $linkUrl null;
  20.     private ?string $templatePath null;
  21.     private ?string $crudActionName null;
  22.     private ?string $routeName null;
  23.     private $routeParameters = [];
  24.     /* @var callable|string|null */
  25.     private $url;
  26.     private array $translationParameters = [];
  27.     private $displayCallable;
  28.     public function getType(): string
  29.     {
  30.         return $this->type;
  31.     }
  32.     public function setType(string $type): void
  33.     {
  34.         $this->type $type;
  35.     }
  36.     public function isEntityAction(): bool
  37.     {
  38.         return Action::TYPE_ENTITY === $this->type;
  39.     }
  40.     public function isGlobalAction(): bool
  41.     {
  42.         return Action::TYPE_GLOBAL === $this->type;
  43.     }
  44.     public function isBatchAction(): bool
  45.     {
  46.         return Action::TYPE_BATCH === $this->type;
  47.     }
  48.     public function getName(): string
  49.     {
  50.         return $this->name;
  51.     }
  52.     public function setName(string $name): void
  53.     {
  54.         $this->name $name;
  55.     }
  56.     public function isDynamicLabel(): bool
  57.     {
  58.         return \is_callable($this->label);
  59.     }
  60.     public function getLabel(): TranslatableInterface|string|callable|false|null
  61.     {
  62.         return $this->label;
  63.     }
  64.     /**
  65.      * @param TranslatableInterface|string|(callable(object $entity): string)|false|null $label
  66.      */
  67.     public function setLabel(TranslatableInterface|string|callable|false|null $label): void
  68.     {
  69.         $this->label $label;
  70.     }
  71.     public function getIcon(): ?string
  72.     {
  73.         return $this->icon;
  74.     }
  75.     public function setIcon(?string $icon): void
  76.     {
  77.         $this->icon $icon;
  78.     }
  79.     public function getCssClass(): string
  80.     {
  81.         return trim($this->cssClass);
  82.     }
  83.     public function setCssClass(string $cssClass): void
  84.     {
  85.         $this->cssClass $cssClass;
  86.     }
  87.     public function getAddedCssClass(): string
  88.     {
  89.         return trim($this->addedCssClass);
  90.     }
  91.     public function setAddedCssClass(string $cssClass): void
  92.     {
  93.         $this->addedCssClass .= ' '.$cssClass;
  94.     }
  95.     public function getHtmlElement(): string
  96.     {
  97.         return $this->htmlElement;
  98.     }
  99.     public function setHtmlElement(string $htmlElement): void
  100.     {
  101.         $this->htmlElement $htmlElement;
  102.     }
  103.     public function getHtmlAttributes(): array
  104.     {
  105.         return $this->htmlAttributes;
  106.     }
  107.     public function addHtmlAttributes(array $htmlAttributes): void
  108.     {
  109.         $this->htmlAttributes array_merge($this->htmlAttributes$htmlAttributes);
  110.     }
  111.     public function setHtmlAttributes(array $htmlAttributes): void
  112.     {
  113.         $this->htmlAttributes $htmlAttributes;
  114.     }
  115.     public function setHtmlAttribute(string $attributeNamestring $attributeValue): void
  116.     {
  117.         $this->htmlAttributes[$attributeName] = $attributeValue;
  118.     }
  119.     public function getTemplatePath(): ?string
  120.     {
  121.         return $this->templatePath;
  122.     }
  123.     public function setTemplatePath(string $templatePath): void
  124.     {
  125.         $this->templatePath $templatePath;
  126.     }
  127.     public function getLinkUrl(): string
  128.     {
  129.         return $this->linkUrl;
  130.     }
  131.     public function setLinkUrl(string $linkUrl): void
  132.     {
  133.         $this->linkUrl $linkUrl;
  134.     }
  135.     public function getCrudActionName(): ?string
  136.     {
  137.         return $this->crudActionName;
  138.     }
  139.     public function setCrudActionName(string $crudActionName): void
  140.     {
  141.         $this->crudActionName $crudActionName;
  142.     }
  143.     public function getRouteName(): ?string
  144.     {
  145.         return $this->routeName;
  146.     }
  147.     public function setRouteName(string $routeName): void
  148.     {
  149.         $this->routeName $routeName;
  150.     }
  151.     /**
  152.      * @return array|callable
  153.      */
  154.     public function getRouteParameters()/* : array|callable */
  155.     {
  156.         return $this->routeParameters;
  157.     }
  158.     /**
  159.      * @param array|callable $routeParameters
  160.      */
  161.     public function setRouteParameters($routeParameters): void
  162.     {
  163.         if (!\is_array($routeParameters) && !\is_callable($routeParameters)) {
  164.             trigger_deprecation(
  165.                 'easycorp/easyadmin-bundle',
  166.                 '4.0.5',
  167.                 'Argument "%s" for "%s" must be one of these types: %s. Passing type "%s" will cause an error in 5.0.0.',
  168.                 '$routeParameters',
  169.                 __METHOD__,
  170.                 '"array" or "callable"',
  171.                 \gettype($routeParameters)
  172.             );
  173.         }
  174.         $this->routeParameters $routeParameters;
  175.     }
  176.     /**
  177.      * @return string|callable|null
  178.      */
  179.     public function getUrl()
  180.     {
  181.         return $this->url;
  182.     }
  183.     /**
  184.      * @param string|callable $url
  185.      */
  186.     public function setUrl($url): void
  187.     {
  188.         if (!\is_string($url) && !\is_callable($url)) {
  189.             trigger_deprecation(
  190.                 'easycorp/easyadmin-bundle',
  191.                 '4.0.5',
  192.                 'Argument "%s" for "%s" must be one of these types: %s. Passing type "%s" will cause an error in 5.0.0.',
  193.                 '$url',
  194.                 __METHOD__,
  195.                 '"string" or "callable"',
  196.                 \gettype($url)
  197.             );
  198.         }
  199.         $this->url $url;
  200.     }
  201.     public function getTranslationParameters(): array
  202.     {
  203.         return $this->translationParameters;
  204.     }
  205.     public function setTranslationParameters(array $translationParameters): void
  206.     {
  207.         $this->translationParameters $translationParameters;
  208.     }
  209.     public function shouldBeDisplayedFor(EntityDto $entityDto): bool
  210.     {
  211.         trigger_deprecation(
  212.             'easycorp/easyadmin-bundle',
  213.             '4.9.4',
  214.             'The "%s" method is deprecated and it will be removed in 5.0.0 because it\'s been replaced by the method "isDisplayed()" of the same class.',
  215.             __METHOD__,
  216.         );
  217.         return $this->isDisplayed($entityDto);
  218.     }
  219.     public function isDisplayed(?EntityDto $entityDto null): bool
  220.     {
  221.         return null === $this->displayCallable || (bool) \call_user_func($this->displayCallable$entityDto?->getInstance());
  222.     }
  223.     public function setDisplayCallable(callable $displayCallable): void
  224.     {
  225.         $this->displayCallable $displayCallable;
  226.     }
  227.     /**
  228.      * @internal
  229.      */
  230.     public function getAsConfigObject(): Action
  231.     {
  232.         $action Action::new($this->name$this->label$this->icon);
  233.         $action->setCssClass($this->cssClass);
  234.         $action->addCssClass($this->addedCssClass);
  235.         $action->setHtmlAttributes($this->htmlAttributes);
  236.         $action->setTranslationParameters($this->translationParameters);
  237.         if (null !== $this->templatePath) {
  238.             $action->setTemplatePath($this->templatePath);
  239.         }
  240.         if ($this->isGlobalAction()) {
  241.             $action->createAsGlobalAction();
  242.         } elseif ($this->isBatchAction()) {
  243.             $action->createAsBatchAction();
  244.         }
  245.         if ('a' === $this->htmlElement) {
  246.             $action->displayAsLink();
  247.         } else {
  248.             $action->displayAsButton();
  249.         }
  250.         if (null !== $this->crudActionName) {
  251.             $action->linkToCrudAction($this->crudActionName);
  252.         }
  253.         if (null !== $this->routeName) {
  254.             $action->linkToRoute($this->routeName$this->routeParameters);
  255.         }
  256.         if (null !== $this->displayCallable) {
  257.             $action->displayIf($this->displayCallable);
  258.         }
  259.         return $action;
  260.     }
  261. }