Skip to content

Instantly share code, notes, and snippets.

@MarioBlazek
Created April 9, 2020 20:04
Show Gist options
  • Save MarioBlazek/3730ed9571abd09da130570716cb8d74 to your computer and use it in GitHub Desktop.
Save MarioBlazek/3730ed9571abd09da130570716cb8d74 to your computer and use it in GitHub Desktop.
<?php
namespace AppBundle\Layouts\QueryType;
use AppBundle\Integration\TheCatApi\Service;
use Netgen\Layouts\API\Values\Collection\Query;
use Netgen\Layouts\Collection\QueryType\QueryTypeHandlerInterface;
use Netgen\Layouts\Parameters\ParameterBuilderInterface;
use Netgen\Layouts\Parameters\ParameterType;
class TheCatApiQueryType implements QueryTypeHandlerInterface
{
private const SOURCE_BREEDS = 'breeds';
private const SOURCE_CATEGORIES = 'categories';
/**
* @var Service
*/
private $service;
public function __construct(Service $service)
{
$this->service = $service;
}
public function buildParameters(ParameterBuilderInterface $builder): void
{
$sources = [
'Get the Cat Breeds' => self::SOURCE_BREEDS,
'List the Categories' => self::SOURCE_CATEGORIES,
];
$builder->add(
'source',
ParameterType\ChoiceType::class,
[
'required' => true,
'options' => $sources,
]
);
}
public function getValues(Query $query, int $offset = 0, ?int $limit = null): iterable
{
if ($query->getParameter('source')->getValue() == self::SOURCE_BREEDS) {
return $this->service->getBreeds();
}
return $this->service->getCategories();
}
public function getCount(Query $query): int
{
if ($query->getParameter('source')->getValue() == self::SOURCE_BREEDS) {
return count($this->service->getBreeds());
}
return count($this->service->getCategories());
}
public function isContextual(Query $query): bool
{
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment