Deprecated: Constant E_STRICT is deprecated in /home/pastorz/old-espace-client/vendor/symfony/error-handler/ErrorHandler.php on line 58

Deprecated: Constant E_STRICT is deprecated in /home/pastorz/old-espace-client/vendor/symfony/error-handler/ErrorHandler.php on line 76
Symfony Profiler

vendor/easycorp/easyadmin-bundle/src/Config/Crud.php line 359

Open in your IDE?
  1. <?php
  2. namespace EasyCorp\Bundle\EasyAdminBundle\Config;
  3. use EasyCorp\Bundle\EasyAdminBundle\Config\Option\SortOrder;
  4. use EasyCorp\Bundle\EasyAdminBundle\Dto\CrudDto;
  5. use EasyCorp\Bundle\EasyAdminBundle\Dto\FilterConfigDto;
  6. use EasyCorp\Bundle\EasyAdminBundle\Dto\PaginatorDto;
  7. use EasyCorp\Bundle\EasyAdminBundle\Field\DateTimeField;
  8. use Symfony\Contracts\Translation\TranslatableInterface;
  9. /**
  10.  * @author Javier Eguiluz <javier.eguiluz@gmail.com>
  11.  */
  12. class Crud
  13. {
  14.     public const PAGE_DETAIL 'detail';
  15.     public const PAGE_EDIT 'edit';
  16.     public const PAGE_INDEX 'index';
  17.     public const PAGE_NEW 'new';
  18.     public const LAYOUT_CONTENT_DEFAULT 'normal';
  19.     public const LAYOUT_CONTENT_FULL 'full';
  20.     public const LAYOUT_SIDEBAR_DEFAULT 'normal';
  21.     public const LAYOUT_SIDEBAR_COMPACT 'compact';
  22.     private CrudDto $dto;
  23.     private int $paginatorPageSize 20;
  24.     private int $paginatorRangeSize 3;
  25.     private bool $paginatorFetchJoinCollection true;
  26.     private ?bool $paginatorUseOutputWalkers null;
  27.     private function __construct(CrudDto $crudDto)
  28.     {
  29.         $this->dto $crudDto;
  30.     }
  31.     public static function new(): self
  32.     {
  33.         $dto = new CrudDto();
  34.         return new self($dto);
  35.     }
  36.     /**
  37.      * @param TranslatableInterface|string|callable $label The callable signature is: fn ($entityInstance, $pageName): string
  38.      *
  39.      * @psalm-param mixed $label
  40.      */
  41.     public function setEntityLabelInSingular(/* @var TranslatableInterface|string|callable */ $label): self
  42.     {
  43.         if (!\is_string($label) && !$label instanceof TranslatableInterface && !\is_callable($label)) {
  44.             trigger_deprecation(
  45.                 'easycorp/easyadmin-bundle',
  46.                 '4.0.5',
  47.                 'Argument "%s" for "%s" must be one of these types: %s. Passing type "%s" will cause an error in 5.0.0.',
  48.                 '$label',
  49.                 __METHOD__,
  50.                 '"string" or "TranslatableInterface" or "callable"',
  51.                 \gettype($label)
  52.             );
  53.         }
  54.         $this->dto->setEntityLabelInSingular($label);
  55.         return $this;
  56.     }
  57.     /**
  58.      * @param TranslatableInterface|string|callable $label The callable signature is: fn ($entityInstance, $pageName): string
  59.      *
  60.      * @psalm-param mixed $label
  61.      */
  62.     public function setEntityLabelInPlural(/* @var TranslatableInterface|string|callable */ $label): self
  63.     {
  64.         if (!\is_string($label) && !$label instanceof TranslatableInterface && !\is_callable($label)) {
  65.             trigger_deprecation(
  66.                 'easycorp/easyadmin-bundle',
  67.                 '4.0.5',
  68.                 'Argument "%s" for "%s" must be one of these types: %s. Passing type "%s" will cause an error in 5.0.0.',
  69.                 '$label',
  70.                 __METHOD__,
  71.                 '"string" or "callable"',
  72.                 \gettype($label)
  73.             );
  74.         }
  75.         $this->dto->setEntityLabelInPlural($label);
  76.         return $this;
  77.     }
  78.     /**
  79.      * @param TranslatableInterface|string|callable $title The callable signature is: fn ($entityInstance): string
  80.      *
  81.      * @psalm-param mixed $title
  82.      */
  83.     public function setPageTitle(string $pageName/* @var TranslatableInterface|string|callable */ $title): self
  84.     {
  85.         if (!\is_string($title) && !$title instanceof TranslatableInterface && !\is_callable($title)) {
  86.             trigger_deprecation(
  87.                 'easycorp/easyadmin-bundle',
  88.                 '4.0.5',
  89.                 'Argument "%s" for "%s" must be one of these types: %s. Passing type "%s" will cause an error in 5.0.0.',
  90.                 '$title',
  91.                 __METHOD__,
  92.                 '"string" or "TranslatableInterface" or "callable"',
  93.                 \gettype($title)
  94.             );
  95.         }
  96.         if (!\in_array($pageName$this->getValidPageNames(), true)) {
  97.             throw new \InvalidArgumentException(sprintf('The first argument of the "%s()" method must be one of these valid page names: %s ("%s" given).'__METHOD__implode(', '$this->getValidPageNames()), $pageName));
  98.         }
  99.         $this->dto->setCustomPageTitle($pageName$title);
  100.         return $this;
  101.     }
  102.     public function setHelp(string $pageNameTranslatableInterface|string $helpMessage): self
  103.     {
  104.         if (!\in_array($pageName$this->getValidPageNames(), true)) {
  105.             throw new \InvalidArgumentException(sprintf('The first argument of the "%s()" method must be one of these valid page names: %s ("%s" given).'__METHOD__implode(', '$this->getValidPageNames()), $pageName));
  106.         }
  107.         $this->dto->setHelpMessage($pageName$helpMessage);
  108.         return $this;
  109.     }
  110.     /**
  111.      * @param string $formatOrPattern A format name ('short', 'medium', 'long', 'full') or a valid ICU Datetime Pattern (see https://unicode-org.github.io/icu/userguide/format_parse/datetime/)
  112.      */
  113.     public function setDateFormat(string $formatOrPattern): self
  114.     {
  115.         if (DateTimeField::FORMAT_NONE === $formatOrPattern || '' === trim($formatOrPattern)) {
  116.             $validDateFormatsWithoutNone array_filter(
  117.                 DateTimeField::VALID_DATE_FORMATS,
  118.                 static fn (string $format): bool => DateTimeField::FORMAT_NONE !== $format
  119.             );
  120.             throw new \InvalidArgumentException(sprintf('The first argument of the "%s()" method cannot be "%s" or an empty string. Use either the special date formats (%s) or a datetime Intl pattern.'__METHOD__DateTimeField::FORMAT_NONEimplode(', '$validDateFormatsWithoutNone)));
  121.         }
  122.         $datePattern DateTimeField::INTL_DATE_PATTERNS[$formatOrPattern] ?? $formatOrPattern;
  123.         $this->dto->setDatePattern($datePattern);
  124.         return $this;
  125.     }
  126.     /**
  127.      * @param string $formatOrPattern A format name ('short', 'medium', 'long', 'full') or a valid ICU Datetime Pattern (see https://unicode-org.github.io/icu/userguide/format_parse/datetime/)
  128.      */
  129.     public function setTimeFormat(string $formatOrPattern): self
  130.     {
  131.         if (DateTimeField::FORMAT_NONE === $formatOrPattern || '' === trim($formatOrPattern)) {
  132.             $validTimeFormatsWithoutNone array_filter(
  133.                 DateTimeField::VALID_DATE_FORMATS,
  134.                 static fn (string $format): bool => DateTimeField::FORMAT_NONE !== $format
  135.             );
  136.             throw new \InvalidArgumentException(sprintf('The first argument of the "%s()" method cannot be "%s" or an empty string. Use either the special time formats (%s) or a datetime Intl pattern.'__METHOD__DateTimeField::FORMAT_NONEimplode(', '$validTimeFormatsWithoutNone)));
  137.         }
  138.         $timePattern DateTimeField::INTL_TIME_PATTERNS[$formatOrPattern] ?? $formatOrPattern;
  139.         $this->dto->setTimePattern($timePattern);
  140.         return $this;
  141.     }
  142.     /**
  143.      * @param string $dateFormatOrPattern A format name ('none', 'short', 'medium', 'long', 'full') or a valid ICU Datetime Pattern (see https://unicode-org.github.io/icu/userguide/format_parse/datetime/)
  144.      * @param string $timeFormat          A format name ('none', 'short', 'medium', 'long', 'full')
  145.      */
  146.     public function setDateTimeFormat(string $dateFormatOrPatternstring $timeFormat DateTimeField::FORMAT_NONE): self
  147.     {
  148.         if ('' === trim($dateFormatOrPattern)) {
  149.             throw new \InvalidArgumentException(sprintf('The first argument of the "%s()" method cannot be an empty string. Use either a date format (%s) or a datetime Intl pattern.'__METHOD__implode(', 'DateTimeField::VALID_DATE_FORMATS)));
  150.         }
  151.         $datePatternIsEmpty DateTimeField::FORMAT_NONE === $dateFormatOrPattern;
  152.         $timePatternIsEmpty DateTimeField::FORMAT_NONE === $timeFormat || '' === trim($timeFormat);
  153.         if ($datePatternIsEmpty && $timePatternIsEmpty) {
  154.             throw new \InvalidArgumentException(sprintf('The values of the arguments of "%s()" cannot be "%s" or an empty string at the same time. Change any of them (or both).'__METHOD__DateTimeField::FORMAT_NONE));
  155.         }
  156.         // when date format/pattern is none and time format is a pattern,
  157.         // silently turn them into a datetime pattern
  158.         if (DateTimeField::FORMAT_NONE === $dateFormatOrPattern && !\in_array($timeFormatDateTimeField::VALID_DATE_FORMATStrue)) {
  159.             $dateFormatOrPattern $timeFormat;
  160.             $timeFormat DateTimeField::FORMAT_NONE;
  161.         }
  162.         $isDatePattern = !\in_array($dateFormatOrPatternDateTimeField::VALID_DATE_FORMATStrue);
  163.         if ($isDatePattern && DateTimeField::FORMAT_NONE !== $timeFormat) {
  164.             throw new \InvalidArgumentException(sprintf('When the first argument of "%s()" is a datetime pattern, you cannot set the time format in the second argument (define the time format inside the datetime pattern).'__METHOD__));
  165.         }
  166.         if (!$isDatePattern && !\in_array($timeFormatDateTimeField::VALID_DATE_FORMATStrue)) {
  167.             throw new \InvalidArgumentException(sprintf('The value of the time format can only be one of the following: %s (but "%s" was given).'implode(', 'DateTimeField::VALID_DATE_FORMATS), $timeFormat));
  168.         }
  169.         $this->dto->setDateTimePattern($dateFormatOrPattern$timeFormat);
  170.         return $this;
  171.     }
  172.     public function setDateIntervalFormat(string $format): self
  173.     {
  174.         $this->dto->setDateIntervalFormat($format);
  175.         return $this;
  176.     }
  177.     public function setTimezone(string $timezoneId): self
  178.     {
  179.         if (!\in_array($timezoneIdtimezone_identifiers_list(), true)) {
  180.             throw new \InvalidArgumentException(sprintf('The "%s" timezone is not a valid PHP timezone ID. Use any of the values listed at https://www.php.net/manual/en/timezones.php'$timezoneId));
  181.         }
  182.         $this->dto->setTimezone($timezoneId);
  183.         return $this;
  184.     }
  185.     public function setNumberFormat(string $format): self
  186.     {
  187.         $this->dto->setNumberFormat($format);
  188.         return $this;
  189.     }
  190.     /**
  191.      * @param $sortFieldsAndOrder ['fieldName' => 'ASC|DESC', ...]
  192.      */
  193.     public function setDefaultSort(array $sortFieldsAndOrder): self
  194.     {
  195.         $sortFieldsAndOrder array_map('strtoupper'$sortFieldsAndOrder);
  196.         foreach ($sortFieldsAndOrder as $sortField => $sortOrder) {
  197.             if (!\in_array($sortOrder, [SortOrder::ASCSortOrder::DESC], true)) {
  198.                 throw new \InvalidArgumentException(sprintf('The sort order can be only "%s" or "%s", "%s" given.'SortOrder::ASCSortOrder::DESC$sortOrder));
  199.             }
  200.             if (!\is_string($sortField)) {
  201.                 throw new \InvalidArgumentException(sprintf('The keys of the array that defines the default sort must be strings with the field names, but the given "%s" value is a "%s".'$sortField\gettype($sortField)));
  202.             }
  203.         }
  204.         $this->dto->setDefaultSort($sortFieldsAndOrder);
  205.         return $this;
  206.     }
  207.     public function setSearchFields(?array $fieldNames): self
  208.     {
  209.         $this->dto->setSearchFields($fieldNames);
  210.         return $this;
  211.     }
  212.     public function setAutofocusSearch(bool $autofocusSearch true): self
  213.     {
  214.         $this->dto->setAutofocusSearch($autofocusSearch);
  215.         return $this;
  216.     }
  217.     public function showEntityActionsInlined(bool $showInlined true): self
  218.     {
  219.         $this->dto->setShowEntityActionsAsDropdown(!$showInlined);
  220.         return $this;
  221.     }
  222.     public function setFilters(?FilterConfigDto $filters): self
  223.     {
  224.         $this->dto->setFiltersConfig($filters);
  225.         return $this;
  226.     }
  227.     public function setPaginatorPageSize(int $maxResultsPerPage): self
  228.     {
  229.         if ($maxResultsPerPage 1) {
  230.             throw new \InvalidArgumentException('The minimum value of paginator page size is 1.');
  231.         }
  232.         $this->paginatorPageSize $maxResultsPerPage;
  233.         return $this;
  234.     }
  235.     public function setPaginatorRangeSize(int $maxPagesOnEachSide): self
  236.     {
  237.         if ($maxPagesOnEachSide 0) {
  238.             throw new \InvalidArgumentException('The minimum value of paginator range size is 0.');
  239.         }
  240.         $this->paginatorRangeSize $maxPagesOnEachSide;
  241.         return $this;
  242.     }
  243.     public function setPaginatorFetchJoinCollection(bool $fetchJoinCollection): self
  244.     {
  245.         $this->paginatorFetchJoinCollection $fetchJoinCollection;
  246.         return $this;
  247.     }
  248.     public function setPaginatorUseOutputWalkers(bool $useOutputWalkers): self
  249.     {
  250.         $this->paginatorUseOutputWalkers $useOutputWalkers;
  251.         return $this;
  252.     }
  253.     public function overrideTemplate(string $templateNamestring $templatePath): self
  254.     {
  255.         $this->dto->overrideTemplate($templateName$templatePath);
  256.         return $this;
  257.     }
  258.     /**
  259.      * Format: ['templateName' => 'templatePath', ...].
  260.      */
  261.     public function overrideTemplates(array $templateNamesAndPaths): self
  262.     {
  263.         foreach ($templateNamesAndPaths as $templateName => $templatePath) {
  264.             $this->overrideTemplate($templateName$templatePath);
  265.         }
  266.         return $this;
  267.     }
  268.     public function addFormTheme(string $themePath): self
  269.     {
  270.         $this->dto->addFormTheme($themePath);
  271.         return $this;
  272.     }
  273.     public function setFormThemes(array $themePaths): self
  274.     {
  275.         foreach ($themePaths as $path) {
  276.             if (!\is_string($path)) {
  277.                 throw new \InvalidArgumentException(sprintf('All form theme paths passed to the "%s" method must be strings, but at least one of those values is of type "%s".'__METHOD__\gettype($path)));
  278.             }
  279.         }
  280.         $this->dto->setFormThemes($themePaths);
  281.         return $this;
  282.     }
  283.     public function setFormOptions(array $newFormOptions, array $editFormOptions null): self
  284.     {
  285.         $this->dto->setNewFormOptions(KeyValueStore::new($newFormOptions));
  286.         $this->dto->setEditFormOptions(KeyValueStore::new($editFormOptions ?? $newFormOptions));
  287.         return $this;
  288.     }
  289.     public function setEntityPermission(string $permission): self
  290.     {
  291.         $this->dto->setEntityPermission($permission);
  292.         return $this;
  293.     }
  294.     public function renderContentMaximized(bool $maximized true): self
  295.     {
  296.         $this->dto->setContentWidth($maximized self::LAYOUT_CONTENT_FULL self::LAYOUT_CONTENT_DEFAULT);
  297.         return $this;
  298.     }
  299.     public function renderSidebarMinimized(bool $minimized true): self
  300.     {
  301.         $this->dto->setSidebarWidth($minimized self::LAYOUT_SIDEBAR_COMPACT self::LAYOUT_SIDEBAR_DEFAULT);
  302.         return $this;
  303.     }
  304.     public function getAsDto(): CrudDto
  305.     {
  306.         $this->dto->setPaginator(new PaginatorDto($this->paginatorPageSize$this->paginatorRangeSize1$this->paginatorFetchJoinCollection$this->paginatorUseOutputWalkers));
  307.         return $this->dto;
  308.     }
  309.     private function getValidPageNames(): array
  310.     {
  311.         return [self::PAGE_DETAILself::PAGE_EDITself::PAGE_INDEXself::PAGE_NEW];
  312.     }
  313. }