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