Licitator 1.0
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
1.4 KiB

5 years ago
  1. <?php
  2. namespace Zitec\RuleEngineBundle\Autocomplete;
  3. /**
  4. * Class AutocompleteManager
  5. * A service that uses autocomplete data sources populated in a compiler pass to retrieve autocomplete results.
  6. */
  7. class AutocompleteManager
  8. {
  9. /**
  10. * @var AutocompleteInterface[]
  11. */
  12. protected $dataSources;
  13. /**
  14. * AutocompleteManager constructor.
  15. *
  16. * @param AutocompleteInterface[] $dataSources
  17. */
  18. public function __construct(array $dataSources = [])
  19. {
  20. $this->dataSources = $dataSources;
  21. }
  22. /**
  23. * @param mixed $key
  24. * The key declared by the data source service.
  25. * @param mixed $queryString
  26. * @param int $page
  27. * @throws \Exception
  28. *
  29. * @return array
  30. */
  31. public function getSuggestions($key, $queryString, $page = 1)
  32. {
  33. if (!isset($this->dataSources[$key])) {
  34. throw new \Exception('No data source found for the given key.');
  35. }
  36. return $this->dataSources[$key]->getSuggestions($queryString, $page);
  37. }
  38. /**
  39. * @param mixed $key
  40. * The key declared by the data source service.
  41. * @param array $ids
  42. * @return array
  43. * @throws \Exception
  44. */
  45. public function getLabels($key, array $ids)
  46. {
  47. if (!isset($this->dataSources[$key])) {
  48. throw new \Exception('No data source found for the given key.');
  49. }
  50. return $this->dataSources[$key]->getLabels($ids);
  51. }
  52. }