QueryBuilder.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace Elastica;
  3. use Elastica\Exception\QueryBuilderException;
  4. use Elastica\QueryBuilder\DSL;
  5. use Elastica\QueryBuilder\Facade;
  6. use Elastica\QueryBuilder\Version;
  7. /**
  8. * Query Builder.
  9. *
  10. * @author Manuel Andreo Garcia <andreo.garcia@googlemail.com>
  11. */
  12. class QueryBuilder
  13. {
  14. /**
  15. * @var Version
  16. */
  17. private $_version;
  18. /**
  19. * @var Facade[]
  20. */
  21. private $_facades = [];
  22. /**
  23. * Constructor.
  24. *
  25. * @param Version $version
  26. */
  27. public function __construct(Version $version = null)
  28. {
  29. $this->_version = $version ?: new Version\Latest();
  30. $this->addDSL(new DSL\Query());
  31. $this->addDSL(new DSL\Aggregation());
  32. $this->addDSL(new DSL\Suggest());
  33. }
  34. /**
  35. * Returns Facade for custom DSL object.
  36. *
  37. * @param $dsl
  38. * @param array $arguments
  39. *
  40. * @throws QueryBuilderException
  41. *
  42. * @return Facade
  43. */
  44. public function __call($dsl, array $arguments)
  45. {
  46. if (false === isset($this->_facades[$dsl])) {
  47. throw new QueryBuilderException('DSL "'.$dsl.'" not supported');
  48. }
  49. return $this->_facades[$dsl];
  50. }
  51. /**
  52. * Adds a new DSL object.
  53. *
  54. * @param DSL $dsl
  55. */
  56. public function addDSL(DSL $dsl)
  57. {
  58. $this->_facades[$dsl->getType()] = new Facade($dsl, $this->_version);
  59. }
  60. /*
  61. * convenience methods
  62. */
  63. /**
  64. * Query DSL.
  65. *
  66. * @return DSL\Query
  67. */
  68. public function query()
  69. {
  70. return $this->_facades[DSL::TYPE_QUERY];
  71. }
  72. /**
  73. * Aggregation DSL.
  74. *
  75. * @return DSL\Aggregation
  76. */
  77. public function aggregation()
  78. {
  79. return $this->_facades[DSL::TYPE_AGGREGATION];
  80. }
  81. /**
  82. * Suggest DSL.
  83. *
  84. * @return DSL\Suggest
  85. */
  86. public function suggest()
  87. {
  88. return $this->_facades[DSL::TYPE_SUGGEST];
  89. }
  90. }