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