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