Skip to content

Commit

Permalink
[TASK] Describe methods generateLinkWithout and generateLinkWith used…
Browse files Browse the repository at this point in the history
… in twig templates
  • Loading branch information
oskardydo committed Oct 29, 2024
1 parent c5f599d commit 8684715
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions src/Twig/AppExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
namespace App\Twig;

use App\Config\Labels;
use App\Dto\SearchDemand;
use App\Helper\VersionFilter;
use App\Helper\VersionSorter;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Twig\Environment;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
Expand All @@ -15,6 +17,7 @@ class AppExtension extends AbstractExtension
public function __construct(
private readonly ParameterBagInterface $parameterBag,
private readonly Environment $twigEnvironment,
private readonly UrlGeneratorInterface $urlGenerator,
) {
}

Expand All @@ -24,6 +27,8 @@ public function getFunctions(): array
new TwigFunction('render_assets', $this->renderAssets(...)),
new TwigFunction('render_single_asset', $this->renderSingleAsset(...)),
new TwigFunction('aggregationBucket', $this->aggregationBucket(...), ['is_safe' => ['html']]),
new TwigFunction('generateLinkWithout', $this->generateLinkWithout(...)),
new TwigFunction('generateLinkWith', $this->generateLinkWith(...)),
new TwigFunction('getLabelForFilter', $this->getLabelForFilter(...)),
new TwigFunction('sortVersions', VersionSorter::sortVersions(...)),
new TwigFunction('filterVersions', VersionFilter::filterVersions(...)),
Expand Down Expand Up @@ -95,6 +100,70 @@ private function fixWording($in, bool $lowerCase = true)
return $in;
}

/**
* Generates a frontend link to be used in Twig with the current demand filters, excluding a specific value
* in the specified filter
*
* Example:
* Suppose we have the following query parameters:
* - filters[vendor]=typo3
* - filters[sversion]=main
*
* If we want to generate a link that excludes vendor=typo3, we can use this method by passing:
* - $key = vendor
* - $value = typo3
*
* This will generate a link that includes only filters[sversion]=main.
*
* @param SearchDemand $demand current search demand object
* @param string $key name of the filter (e.g. vendor in filters[vendor]=typo3)
* @param mixed $value value of the filter (e.g. typo3 in filters[vendor]=typo3)
* @param bool $removeQuery whether to keep or remove 'q' from query parameters when generating the link
* @return string generated link without specific query param if exists
*/
private function generateLinkWithout(SearchDemand $demand, string $key, mixed $value, bool $removeQuery = true): string
{
$filters['filters'] = $demand->withFilterValueForLinkGeneration($key, $value);

if ($removeQuery === false) {
$filters['q'] = $demand->getQuery();
}

return $this->urlGenerator->generate('search-with-suggest', $filters);
}


/**
* Generates a frontend link to be used in Twig with the current demand filters, including a specific value
* in the specified filter
*
* Example:
* Suppose we have the following query parameter:
* - filters[sversion]=main
*
* If we want to generate a link that includes vendor = typo3, we can use this method by passing:
* - $key = vendor
* - $value = typo3
*
* This will generate a link that includes both filters[sversion]=main and filters[vendor]=typo3
*
* @param SearchDemand $demand current search demand object
* @param string $key name of the filter (e.g. vendor in filters[vendor]=typo3)
* @param mixed $value value of the filter (e.g. typo3 to add to the query string)
* @param bool $addQuery whether to include or exclude 'q' from query parameters when generating the link
* @return string generates a link with a specific query parameter. If an invalid filter is provided, the new filters[$key]=$value will be ignored
*/
private function generateLinkWith(SearchDemand $demand, string $key, mixed $value, bool $removeQuery = true): string
{
$filters['filters'] = $demand->withoutFilterValueForLinkGeneration($key, $value);

if ($removeQuery === false) {
$filters['q'] = $demand->getQuery();
}

return $this->urlGenerator->generate('search-with-suggest', $filters);
}

private function getLabelForFilter(string $filter): string
{
return Labels::getLabelForEsColumn($filter);
Expand Down

0 comments on commit 8684715

Please sign in to comment.