|
21 | 21 | use Symfony\Contracts\Translation\TranslatorInterface; |
22 | 22 | use Webmozart\Assert\Assert; |
23 | 23 |
|
24 | | -class BlogListUrlProvider extends AbstractUrlProvider |
25 | | -{ |
26 | | - public const PROVIDER_CODE = 'blog_list'; |
| 24 | +if (class_exists(AbstractUrlProvider::class)) { |
| 25 | + class BlogListUrlProvider extends AbstractUrlProvider |
| 26 | + { |
| 27 | + public const PROVIDER_CODE = 'blog_list'; |
27 | 28 |
|
28 | | - protected string $code = self::PROVIDER_CODE; |
| 29 | + protected string $code = self::PROVIDER_CODE; |
29 | 30 |
|
30 | | - protected string $icon = 'tabler:news'; |
| 31 | + protected string $icon = 'tabler:news'; |
31 | 32 |
|
32 | | - protected int $priority = 35; |
| 33 | + protected int $priority = 35; |
33 | 34 |
|
34 | | - public function __construct( |
35 | | - RouterInterface $router, |
36 | | - private TagRepositoryInterface $tagRepository, |
37 | | - private TranslatorInterface $translator |
38 | | - ) { |
39 | | - parent::__construct($router); |
40 | | - } |
| 35 | + public function __construct( |
| 36 | + RouterInterface $router, |
| 37 | + private TagRepositoryInterface $tagRepository, |
| 38 | + private TranslatorInterface $translator |
| 39 | + ) { |
| 40 | + parent::__construct($router); |
| 41 | + } |
41 | 42 |
|
42 | | - protected function getResults(string $locale, string $search = ''): iterable |
43 | | - { |
44 | | - $queryBuilder = $this->tagRepository->createEnabledListQueryBuilderByType($locale, ArticleInterface::BLOG_TYPE); |
| 43 | + protected function getResults(string $locale, string $search = ''): iterable |
| 44 | + { |
| 45 | + $queryBuilder = $this->tagRepository->createEnabledListQueryBuilderByType($locale, ArticleInterface::BLOG_TYPE); |
| 46 | + |
| 47 | + if (!empty($search)) { |
| 48 | + $queryBuilder |
| 49 | + ->andWhere('translation.name LIKE :search OR translation.slug LIKE :search') |
| 50 | + ->setParameter('search', '%' . $search . '%') |
| 51 | + ; |
| 52 | + } |
| 53 | + |
| 54 | + $queryBuilder->setMaxResults($this->getMaxResults()); |
| 55 | + |
| 56 | + /** @phpstan-ignore-next-line */ |
| 57 | + return $queryBuilder->getQuery()->getResult(); |
| 58 | + } |
45 | 59 |
|
46 | | - if (!empty($search)) { |
47 | | - $queryBuilder |
48 | | - ->andWhere('translation.name LIKE :search OR translation.slug LIKE :search') |
49 | | - ->setParameter('search', '%' . $search . '%') |
50 | | - ; |
| 60 | + protected function addItemFromResult(object $result, string $locale): void |
| 61 | + { |
| 62 | + Assert::isInstanceOf($result, TagInterface::class); |
| 63 | + /** @var TagInterface $result */ |
| 64 | + $result->setCurrentLocale($locale); |
| 65 | + $this->addItem( |
| 66 | + (string) $result->getName(), |
| 67 | + $this->router->generate('monsieurbiz_blog_tag_show', ['slug' => $result->getSlug(), '_locale' => $locale]) |
| 68 | + ); |
51 | 69 | } |
52 | 70 |
|
53 | | - $queryBuilder->setMaxResults($this->getMaxResults()); |
| 71 | + public function getItems(string $locale, string $search = ''): array |
| 72 | + { |
| 73 | + parent::getItems($locale, $search); |
54 | 74 |
|
55 | | - /** @phpstan-ignore-next-line */ |
56 | | - return $queryBuilder->getQuery()->getResult(); |
57 | | - } |
| 75 | + // Add item to link to all articles |
| 76 | + $firstItemLabel = $this->translator->trans('sylius.ui.all', [], null, $locale); |
| 77 | + if (empty($search) || false !== strpos($search, $firstItemLabel)) { |
| 78 | + $this->addItem($firstItemLabel, $this->router->generate('monsieurbiz_blog_index', ['_locale' => $locale])); |
| 79 | + // Add this last element to the beginning of the array |
| 80 | + $lastElement = array_pop($this->items); |
| 81 | + array_unshift($this->items, $lastElement); |
| 82 | + } |
58 | 83 |
|
59 | | - protected function addItemFromResult(object $result, string $locale): void |
60 | | - { |
61 | | - Assert::isInstanceOf($result, TagInterface::class); |
62 | | - /** @var TagInterface $result */ |
63 | | - $result->setCurrentLocale($locale); |
64 | | - $this->addItem( |
65 | | - (string) $result->getName(), |
66 | | - $this->router->generate('monsieurbiz_blog_tag_show', ['slug' => $result->getSlug(), '_locale' => $locale]) |
67 | | - ); |
| 84 | + return $this->items; |
| 85 | + } |
68 | 86 | } |
69 | | - |
70 | | - public function getItems(string $locale, string $search = ''): array |
| 87 | +} else { |
| 88 | + class BlogListUrlProvider |
71 | 89 | { |
72 | | - parent::getItems($locale, $search); |
73 | | - |
74 | | - // Add item to link to all articles |
75 | | - $firstItemLabel = $this->translator->trans('sylius.ui.all', [], null, $locale); |
76 | | - if (empty($search) || false !== strpos($search, $firstItemLabel)) { |
77 | | - $this->addItem($firstItemLabel, $this->router->generate('monsieurbiz_blog_index', ['_locale' => $locale])); |
78 | | - // Add this last element to the beginning of the array |
79 | | - $lastElement = array_pop($this->items); |
80 | | - array_unshift($this->items, $lastElement); |
81 | | - } |
82 | | - |
83 | | - return $this->items; |
84 | 90 | } |
85 | 91 | } |
0 commit comments