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.     public function setRouteParameters(/* @var array|callable */ $routeParameters): void
  142.     {
  143.         if (!\is_array($routeParameters) && !\is_callable($routeParameters)) {
  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.                 '$routeParameters',
  149.                 __METHOD__,
  150.                 '"array" or "callable"',
  151.                 \gettype($routeParameters)
  152.             );
  153.         }
  154.         $this->routeParameters $routeParameters;
  155.     }
  156.     /**
  157.      * @return string|callable|null
  158.      */
  159.     public function getUrl()/* : string|callable */
  160.     {
  161.         return $this->url;
  162.     }
  163.     public function setUrl(/* @var string|callable */ $url): void
  164.     {
  165.         if (!\is_string($url) && !\is_callable($url)) {
  166.             trigger_deprecation(
  167.                 'easycorp/easyadmin-bundle',
  168.                 '4.0.5',
  169.                 'Argument "%s" for "%s" must be one of these types: %s. Passing type "%s" will cause an error in 5.0.0.',
  170.                 '$url',
  171.                 __METHOD__,
  172.                 '"string" or "callable"',
  173.                 \gettype($url)
  174.             );
  175.         }
  176.         $this->url $url;
  177.     }
  178.     public function getTranslationParameters(): array
  179.     {
  180.         return $this->translationParameters;
  181.     }
  182.     public function setTranslationParameters(array $translationParameters): void
  183.     {
  184.         $this->translationParameters $translationParameters;
  185.     }
  186.     public function shouldBeDisplayedFor(EntityDto $entityDto): bool
  187.     {
  188.         return null === $this->displayCallable || (bool) \call_user_func($this->displayCallable$entityDto->getInstance());
  189.     }
  190.     public function setDisplayCallable(callable $displayCallable): void
  191.     {
  192.         $this->displayCallable $displayCallable;
  193.     }
  194.     /**
  195.      * @internal
  196.      */
  197.     public function getAsConfigObject(): Action
  198.     {
  199.         $action Action::new($this->name$this->label$this->icon);
  200.         $action->setCssClass($this->cssClass);
  201.         $action->setHtmlAttributes($this->htmlAttributes);
  202.         $action->setTranslationParameters($this->translationParameters);
  203.         if (null !== $this->templatePath) {
  204.             $action->setTemplatePath($this->templatePath);
  205.         }
  206.         if ($this->isGlobalAction()) {
  207.             $action->createAsGlobalAction();
  208.         } elseif ($this->isBatchAction()) {
  209.             $action->createAsBatchAction();
  210.         }
  211.         if ('a' === $this->htmlElement) {
  212.             $action->displayAsLink();
  213.         } else {
  214.             $action->displayAsButton();
  215.         }
  216.         if (null !== $this->crudActionName) {
  217.             $action->linkToCrudAction($this->crudActionName);
  218.         }
  219.         if (null !== $this->routeName) {
  220.             $action->linkToRoute($this->routeName$this->routeParameters);
  221.         }
  222.         if (null !== $this->displayCallable) {
  223.             $action->displayIf($this->displayCallable);
  224.         }
  225.         return $action;
  226.     }
  227. }